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 |
:-( |
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 |
:-( |
Name = mongoose_wpool:make_pool_name(http, HostType, Tag), |
29 |
:-( |
WpoolOpts = wpool_spec(WpoolOptsIn, ConnOpts), |
30 |
:-( |
#{path_prefix := PathPrefix, request_timeout := RequestTimeout} = ConnOpts, |
31 |
:-( |
case mongoose_wpool:start_sup_pool(http, Name, WpoolOpts) of |
32 |
|
{ok, Pid} -> |
33 |
:-( |
ets:insert(?MODULE, {{HostType, Tag}, PathPrefix, RequestTimeout}), |
34 |
:-( |
{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 |
:-( |
true = ets:delete(?MODULE, {HostType, Tag}), |
42 |
:-( |
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 |
:-( |
case {ets:lookup(?MODULE, {HostType, Tag}), HostType} of |
52 |
:-( |
{[], global} -> {error, pool_not_started}; |
53 |
:-( |
{[], _} -> get_params(global, Tag); |
54 |
:-( |
{[{_, PathPrefix, RequestTimeout}], _} -> {ok, PathPrefix, RequestTimeout} |
55 |
|
end. |
56 |
|
|
57 |
|
%% -------------------------------------------------------------- |
58 |
|
%% Internal functions |
59 |
|
|
60 |
|
wpool_spec(WpoolOptsIn, #{host := Host} = ConnOpts) -> |
61 |
:-( |
HTTPOpts = http_opts(ConnOpts), |
62 |
:-( |
Worker = {fusco, {Host, [{connect_options, HTTPOpts}]}}, |
63 |
:-( |
[{worker, Worker} | WpoolOptsIn]. |
64 |
|
|
65 |
|
http_opts(#{tls := TLSOpts}) -> |
66 |
:-( |
just_tls:make_ssl_opts(TLSOpts); |
67 |
|
http_opts(#{}) -> |
68 |
:-( |
[]. |