livery_drain (livery v0.1.0)
View SourceGraceful shutdown.
drain/1,2 stops a running service the polite way: it stops the
listeners from accepting new connections, waits for the requests
already in flight to finish within a configurable window, then
stops the service. Returns ok once fully drained or
{error, timeout} if the window elapsed with requests still
running (the service is stopped either way).
In-flight requests are counted node-wide via the global
livery_req_sup — every request, on every protocol, runs in a
livery_req_proc child of it. A single-service node drains
exactly its own requests; on a multi-service node drain/2 waits
for all of them.
Stopping acceptance closes the listen socket (no new connections); it does not send GOAWAY on existing keep-alive connections.
{ok, Pid} = livery:start_service(#{http => #{port => 8080}, router => R}),
%% ... serve ...
ok = livery:drain(Pid, #{timeout => 30000}).
Summary
Functions
Wait for in-flight requests to finish, default 30s window.
Wait until no requests are in flight, or the timeout elapses.
Gracefully drain and stop a service with default options.
Gracefully drain and stop a service.
Number of requests currently in flight (0 if the app is down).
Types
-type opts() :: #{timeout => timeout(), poll_interval => non_neg_integer()}.
Functions
-spec await() -> ok | {error, timeout}.
Wait for in-flight requests to finish, default 30s window.
-spec await(opts()) -> ok | {error, timeout}.
Wait until no requests are in flight, or the timeout elapses.
Opts: timeout (default 30000 ms; infinity allowed) and
poll_interval (default 100 ms).
-spec drain(pid()) -> ok | {error, timeout}.
Gracefully drain and stop a service with default options.
Gracefully drain and stop a service.
Stops accepting new connections, waits up to timeout (default
30s) for in-flight requests to finish, then stops the service.
The service is stopped regardless of whether the drain completed.
-spec in_flight() -> non_neg_integer().
Number of requests currently in flight (0 if the app is down).