Skip to content

Commit 0359e50

Browse files
joshthowardOxyjun
andauthored
Address confusing comment around input and output gates (cloudflare#19058)
* Address confusing comment around input and output gates * Update src/content/docs/durable-objects/api/storage-api.mdx Co-authored-by: Jun Lee <[email protected]> --------- Co-authored-by: Jun Lee <[email protected]>
1 parent e1083ff commit 0359e50

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

src/content/docs/durable-objects/api/storage-api.mdx

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ sidebar:
55
order: 6
66
---
77

8-
import { Render, Type, MetaInfo, GlossaryTooltip, TypeScriptExample } from "~/components";
8+
import {
9+
Render,
10+
Type,
11+
MetaInfo,
12+
GlossaryTooltip,
13+
TypeScriptExample,
14+
} from "~/components";
915

1016
The Durable Object Storage API allows <GlossaryTooltip term="Durable Object">Durable Objects</GlossaryTooltip> to access transactional and strongly consistent storage. A Durable Object's attached storage is private to its unique instance and cannot be accessed by other objects.
1117

@@ -18,9 +24,9 @@ Note that Durable Object storage is scoped by individual <GlossaryTooltip term="
1824
However, storage is scoped per individual Durable Object.
1925
:::
2026

21-
Durable Objects gain access to a persistent Durable Object Storage API via `ctx.storage`, on the `ctx` parameter passed to the Durable Object constructor.
27+
Durable Objects gain access to a persistent Durable Object Storage API via the `DurableObjectStorage` interface and accessed by the `DurableObjectState::storage` property. This is frequently accessed via `this.ctx.storage` when the `ctx` parameter passed to the Durable Object constructor.
2228

23-
While access to a Durable Object is single-threaded, request executions can still interleave with each other when they wait on I/O, such as when waiting on the promises returned by persistent storage methods or `fetch()` requests.
29+
JavaScript is a single-threaded and event-driven programming language. This means that JavaScript runtimes, by default, allow requests to interleave with each other which can lead to concurrency bugs. The Durable Objects runtime uses a combination of <GlossaryTooltip term="input gate">input gates</GlossaryTooltip> and <GlossaryTooltip term="output gate">output gates</GlossaryTooltip> to avoid this type of concurrency bug when performing storage operations.
2430

2531
The following code snippet shows you how to store and retrieve data using the Durable Object Storage API.
2632

@@ -31,13 +37,15 @@ export class Counter extends DurableObject {
3137
super(ctx, env);
3238
}
3339

34-
async increment(): Promise<number> {
35-
let value: number = (await this.ctx.storage.get('value')) || 0;
36-
value += 1;
37-
await this.ctx.storage.put('value', value);
38-
return value;
39-
}
40+
async increment(): Promise<number> {
41+
let value: number = (await this.ctx.storage.get('value')) || 0;
42+
value += 1;
43+
await this.ctx.storage.put('value', value);
44+
return value;
45+
}
46+
4047
}
48+
4149
```
4250
</TypeScriptExample>
4351

@@ -255,3 +263,4 @@ The `put()` method returns a `Promise`, but most applications can discard this p
255263

256264
- [Durable Objects: Easy, Fast, Correct – Choose Three](https://blog.cloudflare.com/durable-objects-easy-fast-correct-choose-three/)
257265
- [WebSockets API](/durable-objects/best-practices/websockets/)
266+
```

src/content/glossary/durable-objects.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,11 @@ entries:
6363
- term: "event context"
6464
general_definition: |-
6565
The duration of time that a Durable Object is processing an event, such as a remote procedure call. [Compute duration charges](/durable-objects/platform/pricing) are incurred for the duration of the event context.
66+
67+
- term: "input gate"
68+
general_definition: |-
69+
While a storage operation is executing, no events shall be delivered to a Durable Object except for storage completion events. Any other events will be deferred until such a time as the object is no longer executing JavaScript code and is no longer waiting for any storage operations. We say that these events are waiting for the "input gate" to open.
70+
71+
- term: "output gate"
72+
general_definition: |-
73+
When a storage write operation is in progress, any new outgoing network messages will be held back until the write has completed. We say that these messages are waiting for the "output gate" to open. If the write ultimately fails, the outgoing network messages will be discarded and replaced with errors, while the Durable Object will be shut down and restarted from scratch.

0 commit comments

Comments
 (0)