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 |
|
init() -> |
22 |
21 |
case ets:info(?MODULE) of |
23 |
|
undefined -> |
24 |
1 |
Heir = case whereis(ejabberd_sup) of |
25 |
:-( |
undefined -> []; |
26 |
1 |
Pid -> [{heir, Pid, undefined}] |
27 |
|
end, |
28 |
1 |
ets:new(?MODULE, |
29 |
|
[named_table, public, {read_concurrency, true} | Heir]), |
30 |
1 |
ok; |
31 |
|
_ -> |
32 |
20 |
ok |
33 |
|
end. |
34 |
|
|
35 |
|
start(HostType, Tag, WpoolOptsIn, ConnOpts) -> |
36 |
21 |
Name = mongoose_wpool:make_pool_name(http, HostType, Tag), |
37 |
21 |
WpoolOpts = wpool_spec(WpoolOptsIn, ConnOpts), |
38 |
21 |
PathPrefix = list_to_binary(maps:get(path_prefix, ConnOpts)), |
39 |
21 |
RequestTimeout = maps:get(request_timeout, ConnOpts), |
40 |
21 |
case mongoose_wpool:start_sup_pool(http, Name, WpoolOpts) of |
41 |
|
{ok, Pid} -> |
42 |
21 |
ets:insert(?MODULE, {{HostType, Tag}, PathPrefix, RequestTimeout}), |
43 |
21 |
{ok, Pid}; |
44 |
|
Other -> |
45 |
:-( |
Other |
46 |
|
end. |
47 |
|
|
48 |
|
stop(HostType, Tag) -> |
49 |
21 |
true = ets:delete(?MODULE, {HostType, Tag}), |
50 |
21 |
ok. |
51 |
|
|
52 |
|
%% -------------------------------------------------------------- |
53 |
|
%% Other API functions |
54 |
|
-spec get_params(HostType :: mongooseim:host_type_or_global(), |
55 |
|
Tag :: mongoose_wpool:tag()) -> |
56 |
|
{ok, PathPrefix :: path_prefix(), RequestTimeout :: request_timeout()} |
57 |
|
| {error, pool_not_started}. |
58 |
|
get_params(HostType, Tag) -> |
59 |
178 |
case {ets:lookup(?MODULE, {HostType, Tag}), HostType} of |
60 |
:-( |
{[], global} -> {error, pool_not_started}; |
61 |
80 |
{[], _} -> get_params(global, Tag); |
62 |
98 |
{[{_, PathPrefix, RequestTimeout}], _} -> {ok, PathPrefix, RequestTimeout} |
63 |
|
end. |
64 |
|
|
65 |
|
%% -------------------------------------------------------------- |
66 |
|
%% Internal functions |
67 |
|
|
68 |
|
wpool_spec(WpoolOptsIn, ConnOpts) -> |
69 |
21 |
TargetServer = maps:get(server, ConnOpts), |
70 |
21 |
HttpOpts = maps:get(http_opts, ConnOpts, []), |
71 |
21 |
Worker = {fusco, {TargetServer, [{connect_options, HttpOpts}]}}, |
72 |
21 |
[{worker, Worker} | WpoolOptsIn]. |
73 |
|
|