Skip to content

Commit 6a48149

Browse files
committed
Fix Durable Objects TTL example where long TTLs lead to infinite alarm invocations
1 parent b2a797c commit 6a48149

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

src/content/docs/durable-objects/examples/durable-object-ttl.mdx

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,30 @@ import { TabItem, Tabs, GlossaryTooltip, WranglerConfig } from "~/components";
1212

1313
A common feature request for Durable Objects is a Time To Live (TTL) for Durable Object instances. Durable Objects give developers the tools to implement a custom TTL in only a few lines of code. This example demonstrates how to implement a TTL making use of <GlossaryTooltip term="alarm">`alarms`</GlossaryTooltip>. While this TTL will be extended upon every new request to the Durable Object, this can be customized based on a particular use case.
1414

15+
:::caution[Not available in local development]
16+
17+
The TTL is extended upon every new fetch request to the Durable Object. It might be tempting to instead extend the TTL in the constructor of the Durable Object. This is not advised because the Durable Object's constructor will be called before invoking the alarm handler if the alarm wakes the Durable Object up from hibernation. This approach will naively result in the constructor continually extending the TTL without running the alarm handler.
18+
19+
:::
20+
1521
<Tabs> <TabItem label="JavaScript" icon="seti:javascript">
1622

1723
```js
1824
import { DurableObject } from "cloudflare:workers";
1925

2026
// Durable Object
2127
export class MyDurableObject extends DurableObject {
22-
// Time To Live (TTL) in milliseconds
28+
// Time To Live (TTL) in milliseconds
2329
timeToLiveMs = 1000;
2430

25-
constructor(ctx, env) {
31+
constructor(ctx, env) {
2632
super(ctx, env);
27-
28-
this.ctx.blockConcurrencyWhile(async () => {
29-
await this.ctx.storage.setAlarm(Date.now() + this.timeToLiveMs);
30-
});
3133
}
3234

3335
async fetch(_request) {
34-
// Increment the TTL immediately following every request to a Durable Object
36+
// Extend the TTL immediately following every fetch request to a Durable Object.
3537
await this.ctx.storage.setAlarm(Date.now() + this.timeToLiveMs);
36-
...
38+
...
3739
}
3840

3941
async alarm() {
@@ -62,21 +64,17 @@ export interface Env {
6264

6365
// Durable Object
6466
export class MyDurableObject extends DurableObject {
65-
// Time To Live (TTL) in milliseconds
67+
// Time To Live (TTL) in milliseconds
6668
timeToLiveMs = 1000;
6769

68-
constructor(ctx: DurableObjectState, env: Env) {
70+
constructor(ctx: DurableObjectState, env: Env) {
6971
super(ctx, env);
70-
71-
this.ctx.blockConcurrencyWhile(async () => {
72-
await this.ctx.storage.setAlarm(Date.now() + this.timeToLiveMs);
73-
});
7472
}
7573

7674
async fetch(_request: Request) {
77-
// Increment the TTL immediately following every request to a Durable Object
75+
// Extend the TTL immediately following every fetch request to a Durable Object.
7876
await this.ctx.storage.setAlarm(Date.now() + this.timeToLiveMs);
79-
...
77+
...
8078
}
8179

8280
async alarm() {

0 commit comments

Comments
 (0)