默认情况下,每次调用 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(...);