Diffinery

homeflare/celldpublic

celld/docs/services/workers.md
83 lines · 4.1 KBRaw History
1# Workers
2
3A Worker is a stateless request handler that uses the Cloudflare Workers API.
4An application uses a Worker for the code that answers an HTTP request, such as
5an API endpoint, a router, a webhook receiver, or a front end for a binding.
6celld runs the deployment in a V8 isolate on a node of the fleet, so a Worker
7keeps the persistent state of an application in a binding instead. Read the
8[Cloudflare Workers documentation](https://developers.cloudflare.com/workers/runtime-apis/)
9for the standard API behavior.
10
11## Example
12
13The [hello example](../../examples/hello) returns a text response from a
14`fetch` handler.
15
16<!-- celld-example: hello -->
17
18## The request path
19
20An operator puts an ingress proxy or a load balancer in front of the fleet.
21The ingress terminates TLS, because celld serves plain HTTP on the public
22listener of a node. The load balancer can send a request to any node in its
23rotation, and every node embeds V8 and holds the current deployment. The node
24that accepts the request therefore runs the handler itself, and it forwards
25only a binding call that a different node owns.
26
27Cloudflare instead routes a request through its own global network and starts
28the Worker in a nearby data center. A celld fleet has only the machines that
29you run, so you control the placement and the routing. Read
30[How Workers works](https://developers.cloudflare.com/workers/reference/how-workers-works/)
31for the Cloudflare model.
32
33![An ingress terminates TLS and sends a request to any celld node, which runs the Worker in a V8 isolate, while a binding call goes to the node that owns the cell or to the fleet bucket](workers-flow.svg)
34
35## The handler
36
37An HTTP request invokes the `fetch(request, env, ctx)` handler that the module
38exports as its default export. The handler receives the inbound `Request`, and
39it must return a `Response`. A module can export more handlers, such as
40`scheduled` for a Cron Trigger or `queue` for a Queues consumer. Read the
41[handlers documentation](https://developers.cloudflare.com/workers/runtime-apis/handlers/)
42for the complete list, and the
43[fetch handler documentation](https://developers.cloudflare.com/workers/runtime-apis/handlers/fetch/)
44for the parameters.
45
46The `ctx` argument controls the lifetime of the invocation. `ctx.waitUntil()`
47keeps the isolate alive for a promise after the response goes to the client.
48`ctx.passThroughOnException()` has no effect on celld, because celld has no CDN
49that can answer in place of a failed Worker.
50
51## State and bindings
52
53The `env` argument contains the bindings that the Wrangler configuration
54declares. A binding gives the Worker a permission and an API together, so the
55code needs no credential for the target service. Read the
56[bindings documentation](https://developers.cloudflare.com/workers/runtime-apis/bindings/)
57for the configuration syntax.
58
59A binding call leaves the isolate, and the target decides where the call goes.
60A KV namespace, a D1 database, a queue, and a Durable Object are each a cell,
61and one node owns a cell at a time, so celld routes the call to that owner. An
62R2 binding has no cell, and it reads and writes the fleet bucket directly.
63
64celld runs many requests in one isolate, so a value in the module scope can
65still be present for a later request. That value is not a safe place for
66application state. The load balancer can send the next request to a different
67node, and celld retires an isolate when the request load falls or when a new
68deployment supersedes it, so the value can also disappear. A Worker must
69therefore not depend on mutable in-memory state, and it must write the state
70that it must keep into a binding. The Cloudflare
71[best practices](https://developers.cloudflare.com/workers/best-practices/workers-best-practices/)
72give the same rule.
73
74## Differences from Cloudflare
75
76- celld does not manage a custom domain or terminate TLS. Terminate TLS in the
77 ingress proxy.
78- celld does not supply a Workers AI binding. Call an AI provider from the
79 application. The process rejects `CELLD_AI_BINDING` and `CELLD_AI_URL`, and a
80 deployment rejects an `ai` declaration.
81
82The [Cloudflare compatibility](../cloudflare-compat.md#services) page lists the
83runtime APIs and the unsupported services.