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 |
ejabberd_sup:create_ets_table(?MODULE, [named_table, public, {read_concurrency, true}]). |
24 |
|
|
25 |
|
-spec start(mongooseim:host_type_or_global(), mongoose_wpool:tag(), |
26 |
|
mongoose_wpool:pool_opts(), mongoose_wpool:conn_opts()) -> {ok, pid()} | {error, any()}. |
27 |
|
start(HostType, Tag, WpoolOptsIn, ConnOpts) -> |
28 |
21 |
Name = mongoose_wpool:make_pool_name(http, HostType, Tag), |
29 |
21 |
WpoolOpts = wpool_spec(WpoolOptsIn, ConnOpts), |
30 |
21 |
#{path_prefix := PathPrefix, request_timeout := RequestTimeout} = ConnOpts, |
31 |
21 |
case mongoose_wpool:start_sup_pool(http, Name, WpoolOpts) of |
32 |
|
{ok, Pid} -> |
33 |
21 |
ets:insert(?MODULE, {{HostType, Tag}, PathPrefix, RequestTimeout}), |
34 |
21 |
{ok, Pid}; |
35 |
|
Other -> |
36 |
:-( |
Other |
37 |
|
end. |
38 |
|
|
39 |
|
-spec stop(mongooseim:host_type_or_global(), mongoose_wpool:tag()) -> ok. |
40 |
|
stop(HostType, Tag) -> |
41 |
21 |
true = ets:delete(?MODULE, {HostType, Tag}), |
42 |
21 |
ok. |
43 |
|
|
44 |
|
%% -------------------------------------------------------------- |
45 |
|
%% Other API functions |
46 |
|
-spec get_params(HostType :: mongooseim:host_type_or_global(), |
47 |
|
Tag :: mongoose_wpool:tag()) -> |
48 |
|
{ok, PathPrefix :: path_prefix(), RequestTimeout :: request_timeout()} |
49 |
|
| {error, pool_not_started}. |
50 |
|
get_params(HostType, Tag) -> |
51 |
286 |
case {ets:lookup(?MODULE, {HostType, Tag}), HostType} of |
52 |
:-( |
{[], global} -> {error, pool_not_started}; |
53 |
134 |
{[], _} -> get_params(global, Tag); |
54 |
152 |
{[{_, PathPrefix, RequestTimeout}], _} -> {ok, PathPrefix, RequestTimeout} |
55 |
|
end. |
56 |
|
|
57 |
|
%% -------------------------------------------------------------- |
58 |
|
%% Internal functions |
59 |
|
|
60 |
|
wpool_spec(WpoolOptsIn, #{host := Host} = ConnOpts) -> |
61 |
21 |
HTTPOpts = http_opts(ConnOpts), |
62 |
21 |
Worker = {fusco, {Host, [{connect_options, HTTPOpts}]}}, |
63 |
21 |
[{worker, Worker} | WpoolOptsIn]. |
64 |
|
|
65 |
|
http_opts(#{tls := TLSOpts}) -> |
66 |
1 |
just_tls:make_ssl_opts(TLSOpts); |
67 |
|
http_opts(#{}) -> |
68 |
20 |
[]. |