-
Notifications
You must be signed in to change notification settings - Fork 20
/
ecpool.erl
163 lines (140 loc) · 5.7 KB
/
ecpool.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
%%--------------------------------------------------------------------
%% Copyright (c) 2019 EMQ Technologies Co., Ltd. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%--------------------------------------------------------------------
-module(ecpool).
-export([ pool_spec/4
, start_pool/3
, start_sup_pool/3
, stop_sup_pool/1
, get_client/1
, get_client/2
, pick_and_do/3
, name/1
, workers/1
]).
-export([set_reconnect_callback/2, add_reconnect_callback/2]).
%% NOTE: Obsolete APIs.
%% Use pick_and_do/3 APIs instead
-export([ with_client/2
, with_client/3
]).
-export_type([
pool_name/0,
callback/0,
action/0,
apply_mode/0,
pool_type/0,
conn_callback/0,
option/0
]).
%% cannot be tuple
-type pool_name() :: atom() | binary().
-type callback() :: {module(), atom(), [any()]} | fun((any()) -> any()).
-type action(Result) :: {module(), atom(), [any()]} | fun((_Client :: pid()) -> Result).
-type action() :: action(any()).
-type apply_mode() :: handover
| handover_async
| {handover, timeout()}
| {handover_async, callback()}
| no_handover.
-type pool_type() :: random | hash | direct | round_robin.
-type conn_callback() :: {module(), atom(), [any()]}.
-type option() :: {pool_size, pos_integer()}
| {pool_type, pool_type()}
| {auto_reconnect, false | pos_integer()}
| {on_reconnect, conn_callback()}
| {on_disconnect, conn_callback()}
| tuple().
-define(IS_ACTION(ACTION), ((is_tuple(ACTION) andalso tuple_size(ACTION) == 3) orelse is_function(ACTION, 1))).
pool_spec(ChildId, Pool, Mod, Opts) ->
#{id => ChildId,
start => {?MODULE, start_pool, [Pool, Mod, Opts]},
restart => permanent,
shutdown => 5000,
type => supervisor,
modules => [ecpool_pool_sup]}.
%% @doc Start the pool sup.
-spec(start_pool(pool_name(), atom(), [option()]) -> {ok, pid()} | {error, term()}).
start_pool(Pool, Mod, Opts) ->
ecpool_pool_sup:start_link(Pool, Mod, Opts).
%% @doc Start the pool supervised by ecpool_sup
start_sup_pool(Pool, Mod, Opts) ->
ecpool_sup:start_pool(Pool, Mod, Opts).
%% @doc Start the pool supervised by ecpool_sup
stop_sup_pool(Pool) ->
ecpool_sup:stop_pool(Pool).
%% @doc Get client/connection
-spec(get_client(pool_name()) -> pid() | false).
get_client(Pool) ->
gproc_pool:pick_worker(name(Pool)).
%% @doc Get client/connection with hash key.
-spec(get_client(pool_name(), any()) -> pid() | false).
get_client(Pool, Key) ->
gproc_pool:pick_worker(name(Pool), Key).
-spec(set_reconnect_callback(pool_name(), conn_callback()) -> ok).
set_reconnect_callback(Pool, Callback) ->
[ecpool_worker:set_reconnect_callback(Worker, Callback)
|| {_WorkerName, Worker} <- ecpool:workers(Pool)],
ok.
-spec(add_reconnect_callback(pool_name(), conn_callback()) -> ok).
add_reconnect_callback(Pool, Callback) ->
[ecpool_worker:add_reconnect_callback(Worker, Callback)
|| {_WorkerName, Worker} <- ecpool:workers(Pool)],
ok.
%% NOTE: Use pick_and_do/3 instead of with_client/2,3
%% to avoid applying action failure with 'badfun'.
%%
%% @doc Call the fun with client/connection
-spec with_client(pool_name(), action(Result)) ->
Result | {error, disconnected | ecpool_empty}.
with_client(Pool, Fun) when ?IS_ACTION(Fun) ->
with_worker(get_client(Pool), Fun, no_handover).
%% @doc Call the fun with client/connection
-spec with_client(pool_name(), any(), action(Result)) ->
Result | {error, disconnected | ecpool_empty}.
with_client(Pool, Key, Fun) when ?IS_ACTION(Fun) ->
with_worker(get_client(Pool, Key), Fun, no_handover).
-spec pick_and_do({pool_name(), term()} | pool_name(), action(Result), apply_mode()) ->
Result | {error, disconnected | ecpool_empty}.
pick_and_do({Pool, KeyOrNum}, Action, ApplyMode) when ?IS_ACTION(Action) ->
with_worker(get_client(Pool, KeyOrNum), Action, ApplyMode);
pick_and_do(Pool, Action, ApplyMode) when ?IS_ACTION(Action) ->
with_worker(get_client(Pool), Action, ApplyMode).
-spec with_worker(pid() | false, action(Result), apply_mode()) ->
Result | {error, disconnected | ecpool_empty}.
with_worker(false, Action, _Mode) when ?IS_ACTION(Action) ->
{error, ecpool_empty};
with_worker(Worker, Action, no_handover) when ?IS_ACTION(Action) ->
case ecpool_worker:client(Worker) of
{ok, Client} -> exec(Action, Client);
{error, Reason} -> {error, Reason}
end;
with_worker(Worker, Action, handover) when ?IS_ACTION(Action) ->
ecpool_worker:exec(Worker, Action, infinity);
with_worker(Worker, Action, {handover, Timeout}) when is_integer(Timeout) andalso ?IS_ACTION(Action) ->
ecpool_worker:exec(Worker, Action, Timeout);
with_worker(Worker, Action, handover_async) when ?IS_ACTION(Action) ->
ecpool_worker:exec_async(Worker, Action);
with_worker(Worker, Action, {handover_async, CallbackFun = {_,_,_}}) when ?IS_ACTION(Action) ->
ecpool_worker:exec_async(Worker, Action, CallbackFun).
%% @doc Pool workers
workers(Pool) ->
gproc_pool:active_workers(name(Pool)).
%% @doc ecpool name
name(Pool) -> {?MODULE, Pool}.
exec({M, F, A}, Client) ->
erlang:apply(M, F, [Client]++A);
exec(Action, Client) when is_function(Action) ->
Action(Client).