Diffinery

homeflare/celldpublic

celld/docs/services/workers.md
86 lines · 4.3 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. Each export of
41the main module must be a handler object or a class, as in workerd. A Worker
42whose main module exports a string or a number therefore fails to start. Read
43the
44[handlers documentation](https://developers.cloudflare.com/workers/runtime-apis/handlers/)
45for the complete list, and the
46[fetch handler documentation](https://developers.cloudflare.com/workers/runtime-apis/handlers/fetch/)
47for the parameters.
48
49The `ctx` argument controls the lifetime of the invocation. `ctx.waitUntil()`
50keeps the isolate alive for a promise after the response goes to the client.
51`ctx.passThroughOnException()` has no effect on celld, because celld has no CDN
52that can answer in place of a failed Worker.
53
54## State and bindings
55
56The `env` argument contains the bindings that the Wrangler configuration
57declares. A binding gives the Worker a permission and an API together, so the
58code needs no credential for the target service. Read the
59[bindings documentation](https://developers.cloudflare.com/workers/runtime-apis/bindings/)
60for the configuration syntax.
61
62A binding call leaves the isolate, and the target decides where the call goes.
63A KV namespace, a D1 database, a queue, and a Durable Object are each a cell,
64and one node owns a cell at a time, so celld routes the call to that owner. An
65R2 binding has no cell, and it reads and writes the fleet bucket directly.
66
67celld runs many requests in one isolate, so a value in the module scope can
68still be present for a later request. That value is not a safe place for
69application state. The load balancer can send the next request to a different
70node, and celld retires an isolate when the request load falls or when a new
71deployment supersedes it, so the value can also disappear. A Worker must
72therefore not depend on mutable in-memory state, and it must write the state
73that it must keep into a binding. The Cloudflare
74[best practices](https://developers.cloudflare.com/workers/best-practices/workers-best-practices/)
75give the same rule.
76
77## Differences from Cloudflare
78
79- celld does not manage a custom domain or terminate TLS. Terminate TLS in the
80 ingress proxy.
81- celld does not supply a Workers AI binding. Call an AI provider from the
82 application. The process rejects `CELLD_AI_BINDING` and `CELLD_AI_URL`, and a
83 deployment rejects an `ai` declaration.
84
85The [Cloudflare compatibility](../cloudflare-compat.md#services) page lists the
86runtime APIs and the unsupported services.