跳转到内容
搜索文档

自定义限制

最后更新 查看 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(...);

这篇文档对您有帮助吗?