Cloudflare Durable Objects:代码更新默认改为延迟生效
Durable Objects, Workers - Durable Objects now defer code updates by default
Durable Objects 开发者必读:默认行为变更可能导致旧逻辑失效。请检查你的长连接与 Agent 实现,确认是否需要调整 Wrangler 配置或部署策略以适配新的延迟更新机制。
Durable Objects can stay active across requests, allowing you to run long-running agents, maintain long-lived WebSocket connections and coordinate state across many clients. This is possible because each Durable Object is globally unique, and this means it must run only one version of your code at a time. Previously, when you deployed a change to your Durable Objects code, that change would be applied immediately, which could interrupt in-flight work and terminate open connections.
Durable Objects 可以在请求之间保持活跃状态,允许你运行长时间运行的代理、维护长期存在的 WebSocket 连接以及协调多个客户端之间的状态。这是可能的,因为每个 Durable Object 都是全局唯一的,这意味着它一次只能运行你代码的一个版本。以前,当你部署对 Durable Objects 代码的更改时,该更改会立即应用,这可能会中断正在进行的任务并终止打开的连接。
Durable Objects now defer code updates by default. When you deploy, an active Durable Object can keep running its current code for up to 5 minutes (300 seconds) while it waits to hibernate. This gives in-flight work time to finish. WebSocket connections that use the Hibernation API remain connected when an object adopts new code after hibernating.
Durable Objects 现在默认延迟代码更新。当你部署时,活跃的 Durable Object 可以继续运行其当前代码长达 5 分钟(300 秒),同时等待休眠。这给了正在进行的任务完成的时间。使用 Hibernation API 的 WebSocket 连接在对象休眠后采用新代码时仍保持连接。
For an active Durable Object, a code update follows this flow:
对于活跃的 Durable Object,代码更新遵循以下流程:
flowchart TD
accTitle: Code updates for an active Durable Object
accDescr: By default, an active object can keep running its current code until it hibernates or the delay ends. After hibernating, it starts on new code on the next request. If the delay ends first, or immediate mode is selected, it resets and adopts the new code. Deferral is best effort.
A["You deploy new code"] -->|"Default: deferred"| B["The object can keep running<br/>its current code"]
B --> C{"Hibernates before<br/>the delay ends?"}
C -->|Yes| D["Starts on the new code<br/>on the next request"]
C -->|No| E["The object resets<br/>and adopts the new code"]
A -.->|"Override: immediate"| ETo change the delay, add code_update_strategy to durable_objects in your Wrangler configuration:
要更改延迟时间,请在你的 Wrangler 配置中的 durable_objects 部分添加 code_update_strategy:
{
"durable_objects": {
"bindings": [
{
"name": "CHAT_ROOM",
"class_name": "ChatRoom",
},
],
"code_update_strategy": {
"mode": "deferred",
"max_delay": 60,
},
},
}[[durable_objects.bindings]]
name = "CHAT_ROOM"
class_name = "ChatRoom"
[durable_objects.code_update_strategy]
mode = "deferred"
max_delay = 60max_delay is in seconds, from 0 to 86400 (24 hours), with millisecond precision. Omit it to use the default of 300 seconds.
max_delay 以秒为单位,范围从 0 到 86400(24 小时),精度为毫秒级。省略它以使用默认的 300 秒。
To set the strategy for a single deployment, pass --durable-objects-code-update-mode with immediate or deferred <duration>. For example, apply an update right away during an incident, or when your deployment tooling calls a Durable Object right after deploying and needs the new code:
要为单个部署设置策略,请传递 --durable-objects-code-update-mode 参数,值为 immediate 或 deferred <duration>。例如,在事件期间立即应用更新,或者当你的部署工具在部署后立即调用 Durable Object 并且需要新代码时:
npmyarnpnpm
npm yarn npm
npx wrangler versions deploy <VERSION_ID>@100% --durable-objects-code-update-mode immediateyarn wrangler versions deploy <VERSION_ID>@100% --durable-objects-code-update-mode immediatepnpm wrangler versions deploy <VERSION_ID>@100% --durable-objects-code-update-mode immediateA deferred code update is best effort and does not guarantee that every operation finishes. The object can still reset before max_delay is reached, for example during a Cloudflare runtime update.
延迟的代码更新是尽力而为的,并不保证每个操作都能完成。对象仍可能在达到 max_delay 之前重置,例如在 Cloudflare 运行时更新期间。
For more information, including how this interacts with gradual deployments and the REST API, refer to Durable Objects code updates.
有关更多信息,包括这与渐进式部署和 REST API 的交互方式,请参阅 Durable Objects 代码更新。
更进一步:量化金融体系
看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力