diff options
author | juhlig <[email protected]> | 2019-07-11 13:09:47 +0200 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2019-07-16 15:47:11 +0200 |
commit | 91468482f4318abe644fa389e84865fcf46548c8 (patch) | |
tree | 0e15742465b8f09f02e3ee3782049e5079adb86d /doc/src | |
parent | c7485760d3868219ea2406f225c0c21d6b753ec0 (diff) | |
download | ranch-91468482f4318abe644fa389e84865fcf46548c8.tar.gz ranch-91468482f4318abe644fa389e84865fcf46548c8.tar.bz2 ranch-91468482f4318abe644fa389e84865fcf46548c8.zip |
Document connection draining on application shutdown
Diffstat (limited to 'doc/src')
-rw-r--r-- | doc/src/guide/connection_draining.asciidoc | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/doc/src/guide/connection_draining.asciidoc b/doc/src/guide/connection_draining.asciidoc index cee4dfa..2ccdbc8 100644 --- a/doc/src/guide/connection_draining.asciidoc +++ b/doc/src/guide/connection_draining.asciidoc @@ -6,7 +6,7 @@ a listener in a graceful fashion, ie by not accepting any new connections, but allowing the existing connection processes to exit by themselves instead of being killed. -For this purpose, you should first suspend the listeners you wish to +For this purpose, you should first suspend the listener you wish to stop gracefully, and then wait for its connection count to drop to zero. @@ -67,3 +67,32 @@ after DrainTimeout -> end, ok = ranch:stop_listener(Ref). ---- + +To drain listeners automatically as part of your application shutdown routine, +use the `prep_stop/1` function of your application module. + +.Draining listeners automatically on application shutdown + +[source,erlang] +---- +-module(my_app). + +-behavior(application). + +-export([start/2]). +-export([prep_stop/1]). +-export([stop/1]). + +start(_StartType, _StartArgs) -> + {ok, _} = ranch:start_listener(my_listener, ranch_tcp, #{}, my_protocol, []), + my_app_sup:start_link(). + +prep_stop(State) -> + ok = ranch:suspend_listener(my_listener), + ok = ranch:wait_for_connections(my_listener, '==', 0), + ok = ranch:stop_listener(my_listener), + State. + +stop(_State) -> + ok. +---- |