Skip to content

Allow endpoints (and the authentication handler) to answer 503 Service Unavailable with Retry-After #5985

Open

Description

@t-kietzmann

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 TimeoutException from 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, initializeAuthentication at :723-730), the error passes the _reportException middleware (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 else ServerpodClientUnknownHttpException, without looking at headers (serverpod_client_io.dart:70-76).
  • Endpoints with sendAsRaw can return a relic Response with any status (server.dart:604-613, _toResponse at :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 to 503 with a Retry-After header.
  • Optionally an opt-in mapping of the pool's acquire timeout to that exception.
  • On the client a ServerpodClientServiceUnavailable with the parsed retryAfter, also for method stream opens (a new OpenMethodStreamResponseType).

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-After every client picks its own retry interval, and the server cannot spread the load.

Alternatives

  • A SerializableException of our own with a retryAfter field (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.0 and serverpod_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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions