remove_conn_and_wait_protocol.erl 505 B

123456789101112131415161718192021
  1. -module(remove_conn_and_wait_protocol).
  2. -behaviour(ranch_protocol).
  3. -export([start_link/3]).
  4. -export([init/3]).
  5. start_link(Ref, _, [{remove, MaybeRemove, Timeout}]) ->
  6. Pid = spawn_link(?MODULE, init, [Ref, MaybeRemove, Timeout]),
  7. {ok, Pid}.
  8. init(Ref, MaybeRemove, Timeout) ->
  9. {ok, _} = ranch:handshake(Ref),
  10. _ = case MaybeRemove of
  11. true ->
  12. ranch:remove_connection(Ref);
  13. false ->
  14. ok;
  15. N ->
  16. [ranch:remove_connection(Ref) || _ <- lists:seq(1, N)]
  17. end,
  18. receive after Timeout -> ok end.