1 |
|
-module(mongoose_wpool_rdbms). |
2 |
|
-behaviour(mongoose_wpool). |
3 |
|
|
4 |
|
-export([init/0]). |
5 |
|
-export([start/4]). |
6 |
|
-export([stop/2]). |
7 |
|
|
8 |
|
%% -------------------------------------------------------------- |
9 |
|
%% mongoose_wpool callbacks |
10 |
|
-spec init() -> ok. |
11 |
|
init() -> |
12 |
106 |
ejabberd_sup:create_ets_table( |
13 |
|
prepared_statements, [named_table, public, {read_concurrency, true}]). |
14 |
|
|
15 |
|
-spec start(mongooseim:host_type_or_global(), mongoose_wpool:tag(), |
16 |
|
mongoose_wpool:pool_opts(), mongoose_wpool:conn_opts()) -> {ok, pid()} | {error, any()}. |
17 |
|
start(HostType, Tag, WpoolOpts, RdbmsOpts) -> |
18 |
106 |
try do_start(HostType, Tag, WpoolOpts, RdbmsOpts) |
19 |
|
catch |
20 |
:-( |
Err -> {error, Err} |
21 |
|
end. |
22 |
|
|
23 |
|
-spec stop(mongooseim:host_type_or_global(), mongoose_wpool:tag()) -> ok. |
24 |
|
stop(_, _) -> |
25 |
103 |
ok. |
26 |
|
|
27 |
|
%% -------------------------------------------------------------- |
28 |
|
%% Helper functions |
29 |
|
do_start(HostType, Tag, WpoolOpts0, RdbmsOpts) when is_list(WpoolOpts0), is_map(RdbmsOpts) -> |
30 |
106 |
#{driver := BackendName} = RdbmsOpts, |
31 |
106 |
mongoose_backend:init(global, mongoose_rdbms, [query, execute], #{backend => BackendName}), |
32 |
106 |
mongoose_metrics:ensure_db_pool_metric({rdbms, HostType, Tag}), |
33 |
106 |
WpoolOpts = make_wpool_opts(WpoolOpts0, RdbmsOpts), |
34 |
106 |
ProcName = mongoose_wpool:make_pool_name(rdbms, HostType, Tag), |
35 |
106 |
mongoose_wpool:start_sup_pool(rdbms, ProcName, WpoolOpts). |
36 |
|
|
37 |
|
make_wpool_opts(WpoolOpts0, RdbmsOpts) -> |
38 |
106 |
Worker = {mongoose_rdbms, RdbmsOpts}, |
39 |
106 |
[{worker, Worker}, {pool_sup_shutdown, infinity} | WpoolOpts0]. |