Repository navigation
Description
Problem to Solve
Server._toErrorResponse (serverpod/lib/src/server/server.dart:232-286) maps errors of a method call to exactly these status codes: MaxBodySizeExceeded → 413 (:239-244), EndpointNotFoundException → 404 (:247-249), NotAuthorizedException → 401/403 (:250-255), other EndpointDispatchExceptions → 400 (:256-260), every SerializableException → 400 (:262-268), header errors → 400 (:269-276), and 500 for everything else (:277-285). There is no way to signal "temporarily overloaded, retry after n seconds":
- An exhausted database pool surfaces as a
TimeoutExceptionfrom the postgres pool and becomes a 500. If it happens in the authentication handler, which runs while the session is created, before the endpoint (session.dart:660,initializeAuthenticationat:723-730), the error passes the_reportExceptionmiddleware (server.dart:152,:217-224) and is a 500 as well. session.setResponseHeader('retry-after', ...)(session.dart:47-50, new in 4.1.0) is applied to error responses (server.dart:626-637), but the status stays 400/500, the header is lost when the failure happens before the endpoint runs, and the client ignores response headers anyway:getExceptionFrom(serverpod_client/lib/src/serverpod_client_shared_private.dart:26-50) gives 400, 401, 403, 404 and 500 their own classes and everything elseServerpodClientUnknownHttpException, without looking at headers (serverpod_client_io.dart:70-76).- Endpoints with
sendAsRawcan return a relicResponsewith any status (server.dart:604-613,_toResponseat:645-657), but that is meant for files (cloud_storage/public_endpoint.dart:31) and does not help generated clients.
A client therefore cannot tell "safe to retry later" from "the server failed", and a server cannot spread retries of many clients after an overload.
Proposal
- A framework exception, e.g.
ServiceUnavailableException(retryAfter: Duration?), that an endpoint or the authentication handler can throw and that maps to503with aRetry-Afterheader. - Optionally an opt-in mapping of the pool's acquire timeout to that exception.
- On the client a
ServerpodClientServiceUnavailablewith the parsedretryAfter, also for method stream opens (a newOpenMethodStreamResponseType).
Implementation: add the exception and its case in _toErrorResponse, give getExceptionFrom access to the response headers, and add the client exception class.
Use Case
- Our client data layer must separate "no answer, safe to retry" from "failed". Because a pool timeout arrives as a 500, it treats every 500 of an idempotent operation (acquire a lease, save with an operation id, renew) as "no answer" and retries.
- After a deploy, many clients reconnect at once. Without
Retry-Afterevery client picks its own retry interval, and the server cannot spread the load.
Alternatives
- A
SerializableExceptionof our own with aretryAfterfield (planned). It arrives as a 400 with a payload, so it works only once the endpoint runs; a pool timeout during authentication stays a 500 without payload. - Treating every 500 of an idempotent operation as "no answer". That also retries real server errors.
Additional context
- Checked against
serverpod 4.1.0andserverpod_client 4.1.0; unchanged in 4.1.1.
How experienced are you with this library?
Expert - Experienced and comfortable with using this library in complex projects
Are you interested in working on a PR for this?
- I want to work on this
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsInbox 📫
Activity