Cloudflare Durable Objectsalpha
Run Motoko actors as Cloudflare Durable Objects, with Durable Object storage and alarms.
Each actor key is a Durable Object. Cloudflare delivers its messages one at a time and stores its state; timers are Durable Object alarms. run.sh uses wrangler dev --local.
You also need the moxzi SDK and hosts bundles; see Actor platforms.
Prerequisites#
- Node.js and npm.
run.shinstalls Wrangler locally
Create and run a project#
moxzi host new cloudflare hello
cd hello
ONCE=1 ./run.sh
actor.mo -> actor.wasm + actor.did (245563 bytes, 1 file(s), 50 steps, 0.76s)
Counter/1.inc() -> [1]
Run it a second time. The host is a new process, and the actor's state comes back from the platform's store:
ONCE=1 ./run.sh
actor.mo -> actor.wasm + actor.did (245563 bytes, 1 file(s), 50 steps, 0.77s)
Counter/1.inc() -> [2]
ONCE=1 exits after the demo. Without it, the host keeps running on port 8090 (set PORT to change it), and you can call any key:
curl -X POST localhost:8090/actors/Counter/2/method/inc \
-H 'content-type: application/json' -d '[]'
--example ledger and --example saga work on Cloudflare Durable Objects too; see Examples.
The project#
| Path | Contents |
|---|---|
actor.mo | The Counter actor type. Each key Counter/ID is a separate actor |
host/ | moxzi's Cloudflare Durable Objects adapter. It's yours to change |
run.sh | Builds the actor and the host, starts what Cloudflare Durable Objects needs, and runs the demo |
Change actor.mo and run ./run.sh again. New keys use the new code. Existing keys keep their code until they're upgraded; host/README.md describes upgrades.
How it maps#
| Handled by | |
|---|---|
| One message at a time per key | The Durable Object |
| Actor state | Durable Object storage (wrangler-state/ locally) |
| Durable timers | Durable Object alarms |
Workers can't compile wasm at run time, so the adapter bundles every actor module ahead of time. Build actors with --compact-rts to keep memory inside the 128 MB isolate limit. Locally, wrangler dev runs every Durable Object in one isolate.
Configuration#
| Variable | Default | Effect |
|---|---|---|
PORT | 8090 | The host's HTTP port |
MOXZI_HOST_TOKEN | random | Token for the host's control endpoints (/_moxzi/*) |