Skip to content

Commit

Permalink
Revamp Service Bindings docs for RPC (#13252)
Browse files Browse the repository at this point in the history
* [do not merge] Revamp Service Bindings docs for RPC

- Consolidates Service binding documentation to be within the Runtime APIs section of the docs. Currently docs for configuring a Service binding, and docs for how to write code around Service bindings are in separate sections of the docs, which makes getting started hard, requires jumping back and forth between pages. Relevant content from [the configuration section](https://github.com/cloudflare/cloudflare-docs/blob/production/content/workers/configuration/bindings/about-service-bindings.md) has been moved here, and will be removed.
- Explains what Service bindings are and what to use them for.
- Provides separate code examples RPC and HTTP modes of working with Service bindings.

refs cloudflare/workerd#1658, cloudflare/workerd#1692, cloudflare/workerd#1729, cloudflare/workerd#1756, cloudflare/workerd#1757

* Remove Service bindings config page, update links, redirects

* Apply suggestions from code review

* Further consolidate bindings content within Runtime APIs, link from config section

* Redirect from config bindings to Runtime APIs bindings

* Update links to point to Runtime APIs bindings section

* Fix redirects

* Fix linter warnings

* Bold bullet points for Service Bindings explainer

* Add missing bindings to /runtime-apis/bindings

* Add env vars and secrets links to /runtime-apis/bindings/ section

* Update content/workers/runtime-apis/bindings/ai.md

* Update content/workers/runtime-apis/bindings/service-bindings.md

* Apply suggestions from code review

Co-authored-by: Matt Silverlock <msilverlock@cloudflare.com>

* Break docs into RPC and HTTP sections

* Moving over more docs

* Fix titles

* Fixes

* More docs

* More, need to break apart into pages

* more

* fixup

* Apply suggestions from code review

Co-authored-by: Michael Hart <michael.hart.au@gmail.com>
Co-authored-by: Kenton Varda <kenton@cloudflare.com>

* Remove unnecessary changes

* Create RPC and Context sections

* Rename to /visibility

* Edits

* Fix naming

* Edits

* Add note about Queues to context docs

* Clarify language in RPC example

* Clarify service binding performance

* Link to fetch handler in describing HTTP service bindings

* Move remaining content over from tour doc

* Add limits section, note about Smart Placement

* Edits

* WorkerB => MyWorker

* Edits plus partial

* Update content/workers/runtime-apis/bindings/service-bindings/rpc.md

* Edits

* Making sure internal doc covered, minus Durable Objects docs

* Remove extraneous section

* Call out RPC lifecycle docs in Service Bindings section

* Update content/workers/runtime-apis/rpc/lifecycle.md

* Edits to JSRPC API docs (#13801)

* Clarify structured clonability.

- Let's not talk about class instances being an exception to structured clone early on. Instead, we can have an aside later down the page. Most people probably wouldn't even expect structured clone to treat classes this way anyway, so telling the about it just to tell them that we don't do that is distracting.

- Adjust the wording in anticipation of the fact that we're likely to add many more types that can be serialized, and this list will likely not keep up. The important thing here is to explain the types that have special behavior (they aren't just data structures treated in the obivous way).

- Briefly describe these special semantics in the list, to get people excited to read more.

* Minor wording clarification.

It was confusing whether "object" here meant a plain object or a class instance.

* Clarify garbage collection discussion.

The language here was not very precise, and would have confused people who have a deep understanding of garbage collectors.

* Better link for V8 explicit resource management.

The previous link pointed to a mailing list thread of messages generated by the bug tracker. Let's link directly to the bug tracker.

* Fix typo.

* Clarify language about disposers.

The language here said that stubs "can be disposed" at the end of a using block, which implies that they might not be, or that this is some sort of hint to the GC. It's more accurate to say that they *will* be disposed, that is, their disposer *will* be called, completely independent of GC.

The advice about when to use `using` was unclear. I've changed the advice to simply say that the return value of any call should be stored into `using`, which is an easy rule to follow.

* Remove "Sessions" section from lifecycle.

This section was placed under "automatic disposal" but doesn't seem to belong here.

I don't think it's really necessary to define a "session" unless we are then going to say something about sessions, but the word doesn't appear anywhere else on the page. Sessions are closely related to execution contexts, but execution contexts were already described earlier.

* Clarify section on automatic disposal.

* Correct docs on promise pipelining.

The previous language incorrectly suggested that promise pipelining would kick in even if the caller awaited each promise. In fact, it is necessary to remove the `await` in order to get the benefits.

* Fix reserved methods doc.

The doc incorrectly stated that `fetch` and `connect` were special on `RpcTarget` in addition to `WorkerEntrypoint` and `DurableObject`. This is not correct.

The doc didn't cover the numerous other reserved method names.

* elide -> omit

Co-authored-by: Brendan Irvine-Broque <birvine-broque@cloudflare.com>

---------

Co-authored-by: Brendan Irvine-Broque <birvine-broque@cloudflare.com>

* Apply suggestions from code review

Co-authored-by: Greg Brimble <gbrimble@cloudflare.com>
Co-authored-by: Kenton Varda <kenton@cloudflare.com>

* Apply suggestions from code review

Co-authored-by: Greg Brimble <gbrimble@cloudflare.com>

* More RPC doc tweaks: Move stuff around! (#13808)

* Move section on proxying stubs to compatible-types.

This isn't really lifecycle-related. But it is another kind of thing that can be sent over RPC.

* Move "promise pipelining" to compatible-types under "class instances".

Promise pipelining isn't really about lifecycle. I think it fits under "class instances" because it is motivated by class instances.

* Merge compatible-types into RPC root doc.

The compatible-types list ends up highlighting the key exciting features of the RPC system. This should be at the root.

* Tweak RPC root page.

I'm removing "How it Works" with the function example because:

1. The example itself doesn't really explain "how it works".
2. We now present this same example down the page under "Functions".

* Add changelog entry

* Update content/workers/runtime-apis/rpc/lifecycle.md

Co-authored-by: Greg Brimble <gbrimble@cloudflare.com>

* More more JSRPC doc tweaks (#13840)

* Add documentation for `rpc` compat flag.

* Update links to about-service-bindings.

* Update content/workers/_partials/_platform-compatibility-dates/rpc.md

* Update content/workers/runtime-apis/rpc/_index.md

Co-authored-by: James M Snell <jasnell@gmail.com>

* Update content/workers/_partials/_platform-compatibility-dates/rpc.md

Co-authored-by: James M Snell <jasnell@gmail.com>

* Named entrypoints (#13861)

* Named entrypoint configuration in `wrangler.toml`

* Named entrypoints example

* Apply suggestions from code review

* Apply suggestions from code review

---------

Co-authored-by: Brendan Irvine-Broque <birvine-broque@cloudflare.com>

* Apply suggestions from code review

* Clarify RPC unsupported errors (#13863)

* * Add Durable Objects RPC docs (#13765)

* Update DO counter example with RPC
* Clarify RPC pricing
* Rename "Configuration" to "Best Practices" section

* Fix some redirects (#13865)

* Order the RPC docs sections in nav (#13866)

* Fix links

* Fix more redirects

* Fix DO redirect in Versions & Deployments

* fix merge conflict

---------

Co-authored-by: Matt Silverlock <msilverlock@cloudflare.com>
Co-authored-by: Michael Hart <michael.hart.au@gmail.com>
Co-authored-by: Kenton Varda <kenton@cloudflare.com>
Co-authored-by: Greg Brimble <gbrimble@cloudflare.com>
Co-authored-by: James M Snell <jasnell@gmail.com>
Co-authored-by: Vy Ton <vy@cloudflare.com>
Co-authored-by: Rita Kozlov <rita@cloudflare.com>
Co-authored-by: Rita Kozlov <2414910+rita3ko@users.noreply.github.com>
  • Loading branch information
9 people committed Apr 5, 2024
1 parent 97559d0 commit 3165b2d
Show file tree
Hide file tree
Showing 51 changed files with 1,529 additions and 438 deletions.
16 changes: 10 additions & 6 deletions content/_redirects
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,11 @@
/durable-objects/learning/durable-objects-migrations/ /durable-objects/reference/durable-objects-migrations/ 301
/durable-objects/learning/websockets/ /durable-objects/reference/websockets/ 301
/durable-objects/examples/durable-object-location-example/ /durable-objects/examples/durable-object-in-memory-state/ 301
/durable-objects/how-to/ /durable-objects/configuration/ 301
/durable-objects/how-to/access-durable-object-from-a-worker/ /durable-objects/configuration/access-durable-object-from-a-worker/ 301
/durable-objects/how-to/create-durable-object-stubs/ /durable-objects/configuration/create-durable-object-stubs/ 301
/durable-objects/how-to/ /durable-objects/best-practices/ 301
/durable-objects/how-to/access-durable-object-from-a-worker/ /durable-objects/best-practices/access-durable-objects-from-a-worker/ 301
/durable-objects/how-to/create-durable-object-stubs/ /durable-objects/best-practices/create-durable-object-stubs-and-send-requests/ 301
/durable-objects/configuration/access-durable-object-from-a-worker/ /durable-objects/best-practices/access-durable-objects-from-a-worker/ 301
/durable-objects/configuration/create-durable-object-stubs/ /durable-objects/best-practices/create-durable-object-stubs-and-send-requests/ 301

# email-routing
/email-routing/enable-email-routing/ /email-routing/get-started/enable-email-routing/ 301
Expand Down Expand Up @@ -1123,7 +1125,7 @@
/workers/learning/getting-started/ /workers/get-started/guide/ 301
/workers/learning/profiling-workers/ /workers/reference/how-workers-works/ 301
/workers/platform/scripts/ /api/operations/worker-script-list-workers 301
/workers/platform/services/ /workers/configuration/bindings/about-service-bindings/ 301
/workers/platform/services/ /workers/runtime-apis/bindings/service-bindings/ 301
/workers/platform/web-assembly/ /workers/platform/webassembly/ 301
/workers/platform/web-assembly/rust/ /workers/platform/webassembly/rust/ 301
/workers/quickstart/ /workers/get-started/quickstarts/ 301
Expand Down Expand Up @@ -1245,8 +1247,9 @@
/workers/platform/protocols/ /workers/reference/protocols/ 301
/workers/learning/protocols/ /workers/reference/protocols/ 301
/workers/platform/workers-with-page-rules/ /workers/configuration/workers-with-page-rules/ 301
/workers/platform/bindings/ /workers/configuration/bindings/ 301
/workers/platform/bindings/about-service-bindings/ /workers/configuration/bindings/about-service-bindings/ 301
/workers/platform/bindings/ /workers/runtime-apis/bindings/ 301
/workers/configuration/bindings/ /workers/runtime-apis/bindings/ 301
/workers/platform/bindings/about-service-bindings/ /workers/runtime-apis/bindings/service-bindings/ 301
/workers/platform/triggers/cron-triggers/ /workers/configuration/cron-triggers/ 301
/workers/platform/cron-triggers/ /workers/configuration/cron-triggers/ 301
/workers/platform/compatibility-dates/ /workers/configuration/compatibility-dates/ 301
Expand Down Expand Up @@ -1289,6 +1292,7 @@
/workers/observability/local-development-and-testing/ /workers/testing/local-development/ 301
/workers/configuration/deployments/ /workers/configuration/versions-and-deployments/ 301
/workers/tutorials/hello-world-rust/ /workers/tutorials/ 301
/workers/configuration/bindings/about-service-bindings/ /workers/runtime-apis/bindings/service-bindings/ 301

# workers ai
/workers-ai/models/llm/ /workers-ai/models/#text-generation 301
Expand Down
2 changes: 1 addition & 1 deletion content/durable-objects/api/_index.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: API
pcx_content_type: navigation
weight: 3
weight: 2
---

# API
Expand Down
9 changes: 9 additions & 0 deletions content/durable-objects/best-practices/_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: Best practices
pcx_content_type: navigation
weight: 1
---

# Best Practices

{{<directory-listing>}}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
---
title: Access a Durable Object from a Worker
pcx_content_type: concept
weight: 16
weight: 1
---

# Access a Durable Object from a Worker
# Access Durable Objects from a Worker

To access a Durable Object from a Worker, you must first create a [Durable Object binding](/workers/configuration/bindings/#durable-object-bindings) in your Worker project's [`wrangler.toml`](/workers/wrangler/configuration/#durable-objects) file. The binding is configured to use a particular class and controls access to instances of that class.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
---
title: Create Durable Object stubs
title: Create Durable Object stubs and send requests
pcx_content_type: concept
weight: 16
weight: 2
---

# Create Durable Object stubs
# Create Durable Object stubs and send requests

A Durable Object stub is a client Object used to send requests to a remote Durable Object.

`OBJECT_NAMESPACE.get(id)` creates a Durable Object.

Durable Objects implement E-order semantics. When you make multiple calls to the same Durable Object, it is guaranteed that the calls will be delivered to the remote Durable Object in the order in which you made them. E-order semantics makes many distributed programming problems easier.
Durable Objects implement E-order semantics. When you make multiple calls to the same Durable Object, it is guaranteed that the calls will be delivered to the remote Durable Object in the order in which you made them. E-order semantics makes many distributed programming problems easier.

However, due to random network disruptions or other transient issues, a Durable Object stub may become disconnected from its remote Durable Object. A disconnected stub is permanently broken. In this scenario, all in-flight calls and future calls will fail with [exceptions](/durable-objects/reference/troubleshooting/).
However, due to random network disruptions or other transient issues, a Durable Object stub may become disconnected from its remote Durable Object. A disconnected stub is permanently broken. In this scenario, all in-flight calls and future calls will fail with [exceptions](/durable-objects/reference/troubleshooting/).

To make new requests to the Durable Object, you must call `OBJECT_NAMESPACE.get(id)` again to get a new Durable Object stub. There are no ordering guarantees between requests to the new stub compared to the old one. If ordering is not a concern, you can create a new Durable Object for every request.

Expand All @@ -22,7 +22,7 @@ E-order is a concept deriving from the [E distributed programming language](<htt

{{</Aside>}}

## 1. Obtain a Durable Object stub
## Get a Durable Object stub

```js
let durableObjectStub = OBJECT_NAMESPACE.get(id);
Expand All @@ -33,7 +33,7 @@ let durableObjectStub = OBJECT_NAMESPACE.get(id);
{{<definitions>}}

- `id` {{<type>}}DurableObjectId{{</type>}}
- An ID constructed using `newUniqueId()`, `idFromName()`, or `idFromString()` on this Durable Object namespace.
- An ID constructed using `newUniqueId()`, `idFromName()`, or `idFromString()` on this Durable Object namespace. For details, refer to [Access a Durable Object](/durable-objects/best-practices/access-durable-objects-from-a-worker/#generate-ids-randomly) .

- This method constructs an Object, which is a local client that provides access to a remote Object.

Expand All @@ -43,7 +43,51 @@ let durableObjectStub = OBJECT_NAMESPACE.get(id);

{{</definitions>}}

## 2. Send HTTP requests
## Call a Durable Object

You can call a Durable Object by:

- [Sending HTTP requests](/durable-objects/best-practices/create-durable-object-stubs-and-send-requests/#send-http-requests) to a Durable Object's `fetch()` handler.
- Invoking [Remote Procedure Call (RPC)](/workers/runtime-apis/rpc/) methods defined on a Durable Object class (available for Workers with a [compatibility date greater than or equal to `2024-04-03`](/workers/configuration/compatibility-dates/#remote-procedure-call-rpc)).
- Using the [WebSocket API](/durable-objects/reference/websockets/).

### Call RPC methods

To use RPC, a Durable Objects class must extend the built-in type `DurableObject`. Then, public methods on a Durable Objects class are exposed as [RPC methods](/workers/runtime-apis/rpc/), which you can call from a Durable Object stub in a Worker. All RPC calls are [asynchronous](/workers/runtime-apis/rpc/lifecycle/), accept and return [serializable types](/workers/runtime-apis/rpc/), and [propagate exceptions](/workers/runtime-apis/rpc/error-handling/) to the caller without a stack trace. Refer to [Workers RPC](/workers/runtime-apis/rpc/) for complete details.

```ts
import { DurableObject } from "cloudflare:workers";

export class Counter extends DurableObject {

async increment(amount = 1) {
let value: number = (await this.ctx.storage.get("value")) || 0;
value += amount;
this.ctx.storage.put("value", value);
return value;
}
}
```

```ts
// A Worker calling a Durable Object
let durableObjectStub = env.COUNTERS.get(id);
let response = await durableObjectStub.increment();
```

{{<Aside type="note">}}

When you write a Worker that does not extend the [`DurableObject` class](/workers/configuration/compatibility-dates/#remote-procedure-call-rpc), the `ctx` object in your Durable Object is instead called `state`, and is provided as the first argument to the constructor, but not set as a property of your class.

With RPC, the `DurableObject` superclass defines `ctx` and `env` as class properties. What was previously called `state` is now called `ctx` when you extend the `DurableObject` class.

This naming change from `state` to `ctx` was made both to ensure consistency between `DurableObject` and `WorkerEntrypoint`, and to avoid setting the false expectation that directly mutating the value of `state` would persist data.

{{</Aside>}}

Refer to [Build a Counter](/durable-objects/examples/build-a-counter/) for a full example.

### Send HTTP requests

```js
let response = await durableObjectStub.fetch(request);
Expand Down Expand Up @@ -74,7 +118,7 @@ To understand how exceptions are thrown from within a Durable Object, refer to t

{{</Aside>}}

## 3. List Durable Objects
## List Durable Objects

The Cloudflare REST API supports retrieving a [list of Durable Objects](/api/operations/durable-objects-namespace-list-objects) within a Durable Object namespace and a [list of namespaces](/api/operations/durable-objects-namespace-list-namespaces) associated with an account.

9 changes: 0 additions & 9 deletions content/durable-objects/configuration/_index.md

This file was deleted.

0 comments on commit 3165b2d

Please sign in to comment.