| 1 |
== Connection draining |
| 2 |
|
| 3 |
Stopping a Ranch listener via `ranch:stop_listener/1` will invariably kill |
| 4 |
all connection processes the listener hosts. However, you may want to stop |
| 5 |
a listener in a graceful fashion, ie by not accepting any new connections, |
| 6 |
but allowing the existing connection processes to exit by themselves instead |
| 7 |
of being killed. |
| 8 |
|
| 9 |
For this purpose, you should first suspend the listener you wish to |
| 10 |
stop gracefully, and then wait for its connection count to drop to |
| 11 |
zero. |
| 12 |
|
| 13 |
.Draining a single listener |
| 14 |
|
| 15 |
[source,erlang] |
| 16 |
---- |
| 17 |
ok = ranch:suspend_listener(Ref), |
| 18 |
ok = ranch:wait_for_connections(Ref, '==', 0), |
| 19 |
ok = ranch:stop_listener(Ref). |
| 20 |
---- |
| 21 |
|
| 22 |
If you want to drain more than just one listener, it may be important to first suspend |
| 23 |
them all before beginning to wait for their connection counts to reach zero. Otherwise, |
| 24 |
the not yet suspended listeners will still be accepting connections while you wait for |
| 25 |
the suspended ones to be drained. |
| 26 |
|
| 27 |
.Draining multiple listeners |
| 28 |
|
| 29 |
[source,erlang] |
| 30 |
---- |
| 31 |
lists:foreach( |
| 32 |
fun (Ref) -> |
| 33 |
ok = ranch:suspend_listener(Ref) |
| 34 |
end, |
| 35 |
Refs |
| 36 |
), |
| 37 |
lists:foreach( |
| 38 |
fun (Ref) -> |
| 39 |
ok = ranch:wait_for_connections(Ref, '==', 0), |
| 40 |
ok = ranch:stop_listener(Ref) |
| 41 |
end, |
| 42 |
Refs |
| 43 |
). |
| 44 |
---- |
| 45 |
|
| 46 |
If you have long-running connection processes hosted by the listener you want to stop |
| 47 |
gracefully, draining may take a long time, possibly forever. If you just want to give |
| 48 |
the connection processes a chance to finish, but are not willing to wait for infinity, |
| 49 |
the waiting part could be handled in a separate process. |
| 50 |
|
| 51 |
.Draining a listener with a timeout |
| 52 |
|
| 53 |
[source,erlang] |
| 54 |
---- |
| 55 |
ok = ranch:suspend_listener(Ref), |
| 56 |
{DrainPid, DrainRef} = spawn_monitor( |
| 57 |
fun () -> |
| 58 |
ok = ranch:wait_for_connections(Ref, '==', 0) |
| 59 |
end |
| 60 |
), |
| 61 |
receive |
| 62 |
{'DOWN', DrainRef, process, DrainPid, _} -> |
| 63 |
ok |
| 64 |
after DrainTimeout -> |
| 65 |
exit(DrainPid, kill), |
| 66 |
ok |
| 67 |
end, |
| 68 |
ok = ranch:stop_listener(Ref). |
| 69 |
---- |
| 70 |
|
| 71 |
To drain listeners automatically as part of your application shutdown routine, |
| 72 |
use the `prep_stop/1` function of your application module. |
| 73 |
|
| 74 |
.Draining listeners automatically on application shutdown |
| 75 |
|
| 76 |
[source,erlang] |
| 77 |
---- |
| 78 |
-module(my_app). |
| 79 |
|
| 80 |
-behavior(application). |
| 81 |
|
| 82 |
-export([start/2]). |
| 83 |
-export([prep_stop/1]). |
| 84 |
-export([stop/1]). |
| 85 |
|
| 86 |
start(_StartType, _StartArgs) -> |
| 87 |
{ok, _} = ranch:start_listener(my_listener, ranch_tcp, #{}, my_protocol, []), |
| 88 |
my_app_sup:start_link(). |
| 89 |
|
| 90 |
prep_stop(State) -> |
| 91 |
ok = ranch:suspend_listener(my_listener), |
| 92 |
ok = ranch:wait_for_connections(my_listener, '==', 0), |
| 93 |
ok = ranch:stop_listener(my_listener), |
| 94 |
State. |
| 95 |
|
| 96 |
stop(_State) -> |
| 97 |
ok. |
| 98 |
---- |