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 |
42 |
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 |
42 |
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 |
39 |
ok. |
26 |
|
|
27 |
|
%% -------------------------------------------------------------- |
28 |
|
%% Helper functions |
29 |
|
do_start(HostType, Tag, WpoolOpts0, RdbmsOpts) when is_list(WpoolOpts0), is_map(RdbmsOpts) -> |
30 |
42 |
#{driver := BackendName} = RdbmsOpts, |
31 |
42 |
mongoose_backend:init(global, mongoose_rdbms, [query, execute], #{backend => BackendName}), |
32 |
42 |
mongoose_metrics:ensure_db_pool_metric({rdbms, HostType, Tag}), |
33 |
42 |
WpoolOpts = make_wpool_opts(WpoolOpts0, RdbmsOpts), |
34 |
42 |
ProcName = mongoose_wpool:make_pool_name(rdbms, HostType, Tag), |
35 |
42 |
mongoose_wpool:start_sup_pool(rdbms, ProcName, WpoolOpts). |
36 |
|
|
37 |
|
make_wpool_opts(WpoolOpts0, RdbmsOpts) -> |
38 |
42 |
Worker = {mongoose_rdbms, RdbmsOpts}, |
39 |
42 |
[{worker, Worker}, {pool_sup_shutdown, infinity} | WpoolOpts0]. |