./ct_report/coverage/mongoose_wpool_http.COVER.html

1 %%% @doc
2 %% options and defaults:
3 %% * server - (required)
4 %% * path_prefix - ""
5 %% * request_timeout - 2000,
6 %% * http_opts - [] % passed to fusco
7 %%%
8 %%% @end
9 -module(mongoose_wpool_http).
10 -behaviour(mongoose_wpool).
11
12 -export([init/0]).
13 -export([start/4, stop/2]).
14 -export([get_params/2]).
15
16 -type path_prefix() :: binary().
17 -type request_timeout() :: non_neg_integer().
18
19 %% --------------------------------------------------------------
20 %% mongoose_wpool callbacks
21 -spec init() -> ok.
22 init() ->
23 21 case ets:info(?MODULE) of
24 undefined ->
25 1 Heir = case whereis(ejabberd_sup) of
26
:-(
undefined -> [];
27 1 Pid -> [{heir, Pid, undefined}]
28 end,
29 1 ets:new(?MODULE,
30 [named_table, public, {read_concurrency, true} | Heir]),
31 1 ok;
32 _ ->
33 20 ok
34 end.
35
36 -spec start(mongooseim:host_type_or_global(), mongoose_wpool:tag(),
37 mongoose_wpool:pool_opts(), mongoose_wpool:conn_opts()) -> {ok, pid()} | {error, any()}.
38 start(HostType, Tag, WpoolOptsIn, ConnOpts) ->
39 21 Name = mongoose_wpool:make_pool_name(http, HostType, Tag),
40 21 WpoolOpts = wpool_spec(WpoolOptsIn, ConnOpts),
41 21 #{path_prefix := PathPrefix, request_timeout := RequestTimeout} = ConnOpts,
42 21 case mongoose_wpool:start_sup_pool(http, Name, WpoolOpts) of
43 {ok, Pid} ->
44 21 ets:insert(?MODULE, {{HostType, Tag}, PathPrefix, RequestTimeout}),
45 21 {ok, Pid};
46 Other ->
47
:-(
Other
48 end.
49
50 -spec stop(mongooseim:host_type_or_global(), mongoose_wpool:tag()) -> ok.
51 stop(HostType, Tag) ->
52 21 true = ets:delete(?MODULE, {HostType, Tag}),
53 21 ok.
54
55 %% --------------------------------------------------------------
56 %% Other API functions
57 -spec get_params(HostType :: mongooseim:host_type_or_global(),
58 Tag :: mongoose_wpool:tag()) ->
59 {ok, PathPrefix :: path_prefix(), RequestTimeout :: request_timeout()}
60 | {error, pool_not_started}.
61 get_params(HostType, Tag) ->
62 178 case {ets:lookup(?MODULE, {HostType, Tag}), HostType} of
63
:-(
{[], global} -> {error, pool_not_started};
64 80 {[], _} -> get_params(global, Tag);
65 98 {[{_, PathPrefix, RequestTimeout}], _} -> {ok, PathPrefix, RequestTimeout}
66 end.
67
68 %% --------------------------------------------------------------
69 %% Internal functions
70
71 wpool_spec(WpoolOptsIn, #{host := Host} = ConnOpts) ->
72 21 HTTPOpts = http_opts(ConnOpts),
73 21 Worker = {fusco, {Host, [{connect_options, HTTPOpts}]}},
74 21 [{worker, Worker} | WpoolOptsIn].
75
76 http_opts(#{tls := TLSOpts}) ->
77
:-(
just_tls:make_ssl_opts(TLSOpts);
78 http_opts(#{}) ->
79 21 [].
Line Hits Source