-
-
Notifications
You must be signed in to change notification settings - Fork 453
Expand file tree
/
Copy pathhackney_conn_sup.erl
More file actions
88 lines (76 loc) · 2.37 KB
/
Copy pathhackney_conn_sup.erl
File metadata and controls
88 lines (76 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
%%% -*- erlang -*-
%%%
%%% This file is part of hackney released under the Apache 2 license.
%%% See the NOTICE for more information.
%%%
%%% Copyright (c) 2024 Benoit Chesneau
%%%
%%% @doc Supervisor for hackney_conn connection processes.
%%%
%%% This is a simple_one_for_one supervisor that dynamically starts
%%% hackney_conn gen_statem processes for each connection.
-module(hackney_conn_sup).
-behaviour(supervisor).
%% API
-export([start_link/0]).
-export([start_conn/1]).
-export([stop_conn/1]).
-export([stop_all/0]).
%% Supervisor callbacks
-export([init/1]).
-define(SERVER, ?MODULE).
%%====================================================================
%% API functions
%%====================================================================
%% @doc Start the supervisor.
-spec start_link() -> {ok, pid()} | {error, term()}.
start_link() ->
supervisor:start_link({local, ?SERVER}, ?MODULE, []).
%% @doc Start a new connection process.
%% Opts is a map with at least host, port, and transport.
-spec start_conn(map()) -> {ok, pid()} | {error, term()}.
start_conn(Opts) ->
supervisor:start_child(?SERVER, [Opts]).
%% @doc Stop a connection process gracefully.
%% Tolerates a connection that is already gone or that dies while stopping:
%% callers are only asking for it to be off.
-spec stop_conn(pid()) -> ok.
stop_conn(Pid) ->
try hackney_conn:stop(Pid) catch _:_ -> ok end.
%% @doc Stop all connection processes gracefully.
%% Useful for test cleanup.
-spec stop_all() -> ok.
stop_all() ->
try
Children = supervisor:which_children(?SERVER),
lists:foreach(fun({_, Pid, _, _}) when is_pid(Pid) ->
try
hackney_conn:stop(Pid)
catch
_:_ -> ok
end;
(_) -> ok
end, Children),
ok
catch
exit:{noproc, _} -> ok;
_:_ -> ok
end.
%%====================================================================
%% Supervisor callbacks
%%====================================================================
init([]) ->
SupFlags = #{
strategy => simple_one_for_one,
intensity => 10,
period => 10
},
ChildSpec = #{
id => hackney_conn,
start => {hackney_conn, start_link, []},
restart => temporary,
shutdown => 5000,
type => worker,
modules => [hackney_conn]
},
{ok, {SupFlags, [ChildSpec]}}.