forked from helium/blockchain-http
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bh_sup.erl
109 lines (93 loc) · 2.98 KB
/
bh_sup.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
-module(bh_sup).
-behaviour(supervisor).
-include("bh_db_worker.hrl").
%% API
-export([start_link/0]).
%% Supervisor callbacks
-export([init/1]).
-define(SERVER, ?MODULE).
-define(WORKER(I, Args), #{
id => I,
start => {I, start_link, Args},
restart => permanent,
shutdown => 5000,
type => worker,
modules => [I]
}).
start_link() ->
supervisor:start_link({local, ?SERVER}, ?MODULE, []).
init([]) ->
SupFlags = #{
strategy => rest_for_one,
intensity => 10,
period => 10
},
PoolNames =
case os:getenv("DATABASE_RW_URL") of
false -> [ro_pool];
_ -> [ro_pool, rw_pool]
end,
lists:foreach(
fun(PoolName) ->
{ok, {PoolOpts, DBOpts, DBHandlers}} = pool_opts(PoolName),
init_pool(PoolName, PoolOpts, DBOpts, DBHandlers)
end,
PoolNames
),
{ok, ListenPort} = application:get_env(blockchain_http, port),
lager:info("Starting http listener on ~p", [ListenPort]),
ElliConfig = [
{mods, [
{bh_middleware_cors, []},
{bh_routes, []}
]}
],
ChildSpecs =
[
?WORKER(bh_cache, []),
?WORKER(bh_pool_watcher, [PoolNames]),
?WORKER(elli, [
[
{callback, elli_middleware},
{callback_args, ElliConfig},
{port, ListenPort}
]
])
],
{ok, {SupFlags, ChildSpecs}}.
pool_opts(ro_pool) ->
{ok, DBOpts} = psql_migration:connection_opts([], {env, "DATABASE_RO_URL"}),
{ok, DBHandlers} = application:get_env(blockchain_http, db_ro_handlers),
{ok, PoolOpts0} = application:get_env(blockchain_http, db_ro_pool),
PoolSize =
case os:getenv("DATABASE_RO_POOL_SIZE") of
false -> proplists:get_value(size, PoolOpts0, 100);
SizeStr -> list_to_integer(SizeStr)
end,
PoolOpts = lists:keystore(size, 1, PoolOpts0, {size, PoolSize}),
{ok, {PoolOpts, DBOpts, DBHandlers}};
pool_opts(rw_pool) ->
{ok, PoolOpts} = application:get_env(blockchain_http, db_rw_pool),
{ok, DBOpts} = psql_migration:connection_opts([], {env, "DATABASE_RW_URL"}),
{ok, DBHandlers} = application:get_env(blockchain_http, db_rw_handlers),
{ok, {PoolOpts, DBOpts, DBHandlers}}.
init_pool(Name, PoolOpts, DBOpts, DBHandlers) ->
ok = dispcount:start_dispatch(
Name,
{bh_db_worker, [
{db_opts, DBOpts},
{db_handlers, DBHandlers}
]},
[
{restart, permanent},
{shutdown, 4000},
{dispatch_mechanism, proplists:get_value(dispatch_mechanism, PoolOpts, hash)},
{watcher_type, proplists:get_value(watcher_type, PoolOpts, ets)},
{maxr, 10},
{maxt, 60},
{resources, proplists:get_value(size, PoolOpts, 5)}
]
),
{ok, PoolInfo} = dispcount:dispatcher_info(Name),
persistent_term:put(Name, PoolInfo),
ok.