/
eredis_pool_sup.erl
84 lines (63 loc) · 2.95 KB
/
eredis_pool_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
-module(eredis_pool_sup).
-behaviour(supervisor).
%% Include
-include_lib("eunit/include/eunit.hrl").
%% API
-export([start_link/0, start_link/2]).
-export([create_pool/3, delete_pool/1]).
%% Supervisor callbacks
-export([init/1]).
%% Helper macro for declaring children of supervisor
-define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 5000, Type, [I]}).
%% ===================================================================
%% API functions
%% ===================================================================
start_link() ->
{ok, Pools} = application:get_env(eredis_pool, pools),
{ok, GlobalOrLocal} = application:get_env(eredis_pool, global_or_local),
start_link(Pools, GlobalOrLocal).
start_link(Pools, GlobalOrLocal) ->
supervisor:start_link({local, ?MODULE}, ?MODULE, [Pools, GlobalOrLocal]).
%% ===================================================================
%% @doc create new pool.
%% @end
%% ===================================================================
-spec(create_pool(PoolName::atom(), Size::integer(), Options::[tuple()]) ->
{ok, pid()} | {error,{already_started, pid()}}).
create_pool(PoolName, Size, Options) ->
create_pool(local, PoolName, Size, Options).
%% ===================================================================
%% @doc create new pool, selectable name zone global or local.
%% @end
%% ===================================================================
-spec(create_pool(GlobalOrLocal::atom(), PoolName::atom(), Size::integer(), Options::[tuple()]) ->
{ok, pid()} | {error,{already_started, pid()}}).
create_pool(GlobalOrLocal, PoolName, Size, Options)
when GlobalOrLocal =:= local;
GlobalOrLocal =:= global ->
SizeArgs = [{size, Size}, {max_overflow, 10}],
PoolArgs = [{name, {GlobalOrLocal, PoolName}}, {worker_module, eredis}],
PoolSpec = poolboy:child_spec(PoolName, PoolArgs ++ SizeArgs, Options),
supervisor:start_child(?MODULE, PoolSpec).
%% ===================================================================
%% @doc delet pool and disconnected to Redis.
%% @end
%% ===================================================================
-spec(delete_pool(PoolName::atom()) -> ok | {error,not_found}).
delete_pool(PoolName) ->
supervisor:terminate_child(?MODULE, PoolName),
supervisor:delete_child(?MODULE, PoolName).
%% ===================================================================
%% Supervisor callbacks
%% ===================================================================
init([Pools, GlobalOrLocal]) ->
RestartStrategy = one_for_one,
MaxRestarts = 10,
MaxSecondsBetweenRestarts = 10,
SupFlags = {RestartStrategy, MaxRestarts, MaxSecondsBetweenRestarts},
PoolSpecs = lists:map(fun({Name, SizeArgs, WorkerArgs}) ->
PoolArgs = [{name, {GlobalOrLocal, Name}},
{worker_module, eredis}] ++ SizeArgs,
poolboy:child_spec(Name, PoolArgs, WorkerArgs)
end, Pools),
{ok, {SupFlags, PoolSpecs}}.