From 91468482f4318abe644fa389e84865fcf46548c8 Mon Sep 17 00:00:00 2001 From: juhlig Date: Thu, 11 Jul 2019 13:09:47 +0200 Subject: Document connection draining on application shutdown --- doc/src/guide/connection_draining.asciidoc | 31 +++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) 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. +---- -- cgit v1.2.3