Update 'ecpool.app.src' and adopt new exports syntax
This commit is contained in:
parent
448820e709
commit
36fb68e5ed
|
@ -1,15 +1,14 @@
|
||||||
{application, ecpool,
|
{application, ecpool,
|
||||||
[
|
[{description, "Erlang Client/Connection Pool"},
|
||||||
{description, "Erlang Client/Connection Pool"},
|
|
||||||
{vsn, "git"},
|
{vsn, "git"},
|
||||||
{registered, []},
|
{registered, []},
|
||||||
{applications, [
|
{applications, [kernel,
|
||||||
kernel,
|
|
||||||
stdlib,
|
stdlib,
|
||||||
gproc
|
gproc
|
||||||
]},
|
]},
|
||||||
{mod, { ecpool_app, []}},
|
{mod, {ecpool_app, []}},
|
||||||
{env, []},
|
{env, []},
|
||||||
{licenses,["Apache-2.0"]},
|
{licenses,["Apache-2.0"]},
|
||||||
|
{maintainers, ["Feng Lee <feng@emqx.io>"]},
|
||||||
{links,[{"Github","https://github.com/emqx/ecpool"}]}
|
{links,[{"Github","https://github.com/emqx/ecpool"}]}
|
||||||
]}.
|
]}.
|
||||||
|
|
|
@ -16,27 +16,36 @@
|
||||||
|
|
||||||
-module(ecpool).
|
-module(ecpool).
|
||||||
|
|
||||||
-export([pool_spec/4, start_pool/3, start_sup_pool/3, stop_sup_pool/1,
|
-export([ pool_spec/4
|
||||||
get_client/1, get_client/2, with_client/2, with_client/3,
|
, start_pool/3
|
||||||
set_reconnect_callback/2,
|
, start_sup_pool/3
|
||||||
name/1, workers/1]).
|
, stop_sup_pool/1
|
||||||
|
, get_client/1
|
||||||
|
, get_client/2
|
||||||
|
, with_client/2
|
||||||
|
, with_client/3
|
||||||
|
, name/1
|
||||||
|
, workers/1
|
||||||
|
]).
|
||||||
|
|
||||||
-export_type([pool_name/0,
|
-export([set_reconnect_callback/2]).
|
||||||
pool_type/0,
|
|
||||||
option/0
|
-export_type([ pool_name/0
|
||||||
|
, pool_type/0
|
||||||
|
, option/0
|
||||||
]).
|
]).
|
||||||
|
|
||||||
-type pool_name() :: term().
|
-type(pool_name() :: term()).
|
||||||
|
|
||||||
-type pool_type() :: random | hash | round_robin.
|
-type(pool_type() :: random | hash | round_robin).
|
||||||
|
|
||||||
-type reconn_callback() :: {fun((pid()) -> term())}.
|
-type(reconn_callback() :: {fun((pid()) -> term())}).
|
||||||
|
|
||||||
-type option() :: {pool_size, pos_integer()}
|
-type(option() :: {pool_size, pos_integer()}
|
||||||
| {pool_type, pool_type()}
|
| {pool_type, pool_type()}
|
||||||
| {auto_reconnect, false | pos_integer()}
|
| {auto_reconnect, false | pos_integer()}
|
||||||
| {on_reconnect, reconn_callback()}
|
| {on_reconnect, reconn_callback()}
|
||||||
| tuple().
|
| tuple()).
|
||||||
|
|
||||||
pool_spec(ChildId, Pool, Mod, Opts) ->
|
pool_spec(ChildId, Pool, Mod, Opts) ->
|
||||||
#{id => ChildId,
|
#{id => ChildId,
|
||||||
|
|
|
@ -18,22 +18,22 @@
|
||||||
|
|
||||||
-behaviour(gen_server).
|
-behaviour(gen_server).
|
||||||
|
|
||||||
-import(proplists, [get_value/3]).
|
|
||||||
|
|
||||||
%% API Function Exports
|
%% API Function Exports
|
||||||
-export([start_link/2]).
|
-export([start_link/2]).
|
||||||
|
|
||||||
-export([info/1]).
|
-export([info/1]).
|
||||||
|
|
||||||
%% gen_server Function Exports
|
%% gen_server Function Exports
|
||||||
-export([init/1,
|
-export([ init/1
|
||||||
handle_call/3,
|
, handle_call/3
|
||||||
handle_cast/2,
|
, handle_cast/2
|
||||||
handle_info/2,
|
, handle_info/2
|
||||||
terminate/2,
|
, terminate/2
|
||||||
code_change/3
|
, code_change/3
|
||||||
]).
|
]).
|
||||||
|
|
||||||
|
-import(proplists, [get_value/3]).
|
||||||
|
|
||||||
-record(state, {name, size, type}).
|
-record(state, {name, size, type}).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
|
@ -21,9 +21,9 @@
|
||||||
-export([start_link/0]).
|
-export([start_link/0]).
|
||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([start_pool/3,
|
-export([ start_pool/3
|
||||||
stop_pool/1,
|
, stop_pool/1
|
||||||
get_pool/1
|
, get_pool/1
|
||||||
]).
|
]).
|
||||||
|
|
||||||
-export([pools/0]).
|
-export([pools/0]).
|
||||||
|
|
|
@ -21,18 +21,30 @@
|
||||||
-export([start_link/4]).
|
-export([start_link/4]).
|
||||||
|
|
||||||
%% API Function Exports
|
%% API Function Exports
|
||||||
-export([client/1, is_connected/1, set_reconnect_callback/2]).
|
-export([ client/1
|
||||||
|
, is_connected/1
|
||||||
%% gen_server Function Exports
|
, set_reconnect_callback/2
|
||||||
-export([init/1,
|
|
||||||
handle_call/3,
|
|
||||||
handle_cast/2,
|
|
||||||
handle_info/2,
|
|
||||||
terminate/2,
|
|
||||||
code_change/3
|
|
||||||
]).
|
]).
|
||||||
|
|
||||||
-record(state, {pool, id, client, mod, on_reconnect, on_disconnect, supervisees = [], opts}).
|
%% gen_server Function Exports
|
||||||
|
-export([ init/1
|
||||||
|
, handle_call/3
|
||||||
|
, handle_cast/2
|
||||||
|
, handle_info/2
|
||||||
|
, terminate/2
|
||||||
|
, code_change/3
|
||||||
|
]).
|
||||||
|
|
||||||
|
-record(state, {
|
||||||
|
pool :: ecpool:poo_name(),
|
||||||
|
id :: pos_integer(),
|
||||||
|
client :: pid(),
|
||||||
|
mod :: module(),
|
||||||
|
on_reconnect :: ecpool:reconn_callback(),
|
||||||
|
on_disconnect :: ecpool:reconn_callback(),
|
||||||
|
supervisees = [],
|
||||||
|
opts :: proplists:proplist()
|
||||||
|
}).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Callback
|
%% Callback
|
||||||
|
@ -84,15 +96,18 @@ set_reconnect_callback(Pid, OnReconnect) ->
|
||||||
|
|
||||||
init([Pool, Id, Mod, Opts]) ->
|
init([Pool, Id, Mod, Opts]) ->
|
||||||
process_flag(trap_exit, true),
|
process_flag(trap_exit, true),
|
||||||
State = #state{pool = Pool, id = Id, mod = Mod, opts = Opts,
|
State = #state{pool = Pool,
|
||||||
|
id = Id,
|
||||||
|
mod = Mod,
|
||||||
|
opts = Opts,
|
||||||
on_reconnect = proplists:get_value(on_reconnect, Opts),
|
on_reconnect = proplists:get_value(on_reconnect, Opts),
|
||||||
on_disconnect = proplists:get_value(on_disconnect, Opts)},
|
on_disconnect = proplists:get_value(on_disconnect, Opts)
|
||||||
|
},
|
||||||
case connect_internal(State) of
|
case connect_internal(State) of
|
||||||
{ok, NewState} ->
|
{ok, NewState} ->
|
||||||
gproc_pool:connect_worker(ecpool:name(Pool), {Pool, Id}),
|
gproc_pool:connect_worker(ecpool:name(Pool), {Pool, Id}),
|
||||||
{ok, NewState};
|
{ok, NewState};
|
||||||
{error, Error} ->
|
{error, Error} -> {stop, Error}
|
||||||
{stop, Error}
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
handle_call(is_connected, _From, State = #state{client = Client}) ->
|
handle_call(is_connected, _From, State = #state{client = Client}) ->
|
||||||
|
@ -165,10 +180,6 @@ connopts([{pool_type, _}|Opts], Acc) ->
|
||||||
connopts(Opts, Acc);
|
connopts(Opts, Acc);
|
||||||
connopts([{auto_reconnect, _}|Opts], Acc) ->
|
connopts([{auto_reconnect, _}|Opts], Acc) ->
|
||||||
connopts(Opts, Acc);
|
connopts(Opts, Acc);
|
||||||
connopts([{bind, _}|Opts], Acc) ->
|
|
||||||
connopts(Opts, Acc);
|
|
||||||
connopts([{unbind, _}|Opts], Acc) ->
|
|
||||||
connopts(Opts, Acc);
|
|
||||||
connopts([Opt|Opts], Acc) ->
|
connopts([Opt|Opts], Acc) ->
|
||||||
connopts(Opts, [Opt|Acc]).
|
connopts(Opts, [Opt|Acc]).
|
||||||
|
|
||||||
|
@ -203,3 +214,4 @@ connect_internal(State) ->
|
||||||
catch
|
catch
|
||||||
_C:Reason -> {error, Reason}
|
_C:Reason -> {error, Reason}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue