跳转到内容
本站为社区维护的非官方中文翻译,与 Cloudflare, Inc. 无关联。本站不提供账户登录,请勿在任何非官方站点输入您的 Cloudflare 账户信息。官方文档:developers.cloudflare.com

自定义资源限制

最后更新 查看 MarkdownAgent 设置

默认情况下,每次调用 Dynamic Worker 都会使用你的 Workers 套餐对 CPU 时间和子请求的限制。自定义限制允许你以编程方式对 Dynamic Worker 的资源使用实施更低的限制。

你可以设置每次调用的最长 CPU 时间和最大子请求数。如果 Dynamic Worker 达到任一限制,它会立即抛出异常。

设置自定义限制

将自定义限制指定为 Worker 代码的一部分:

const worker = env.LOADER.get("my-worker", async () => {
  return {
    compatibilityDate: "$today",
    mainModule: "index.js",
    modules: { "index.js": code },
    limits: { cpuMs: 10, subRequests: 5 },
  };
});

你也可以在 getEntrypoint() 调用中指定限制:

// Get the Worker's default entrypoint with custom limits.
// If limits were already specified as part of the Worker code,
// the lower of the two limits is used.
const entrypoint = worker.getEntrypoint(null, { limits: { cpuMs: 10, subRequests: 5 } });
await entrypoint.fetch(...);

这篇文档对您有帮助吗?