Best practices
Manage service lifetimes
Choose owners for request, shared, and background-work resources, with cleanup timed to their use.
Let Effront manage resources needed by one request. Give shared resources and background work separate owners.
Choose the resource owner
| Resource use | Owner | Release point |
|---|---|---|
| One request | Request Scope | Streamed-body completion, failure, or cancellation |
| Several requests, such as a database pool | Host owner outside the application Layer | After every response that uses the resource has ended |
| Work after the response ends | Background work, via a host-supported mechanism | When that work finishes |
For a shared database pool, register each borrowed connection's return with the request Scope. Leave pool disposal to the host owner. See the external-service ownership contract for host-provided services.
Acquire request resources in the application Layer
Effront builds the Layer passed to EFFRONT.make({ routes, layer }) for each request.
Place request-specific acquisition in Layer.effect and register cleanup with Effect.acquireRelease.
The service setup example shows how to declare a service and register its Layer.
Keep resources alive through the response body
A delayed component can still use request services after the handler returns a Response.
Let the request Scope run the registered cleanup when the streamed body ends.
For buffered or empty responses, or if response creation fails, Effront releases acquired resources without waiting for body consumption.