Update 'ecpool.app.src' and adopt new exports syntax

This commit is contained in:
Feng Lee 2019-08-26 17:08:22 +08:00
parent 448820e709
commit 36fb68e5ed
5 changed files with 67 additions and 47 deletions

View File

@ -1,15 +1,14 @@
{application, ecpool,
[
{description, "Erlang Client/Connection Pool"},
[{description, "Erlang Client/Connection Pool"},
{vsn, "git"},
{registered, []},
{applications, [
kernel,
{applications, [kernel,
stdlib,
gproc
]},
{mod, { ecpool_app, []}},
{mod, {ecpool_app, []}},
{env, []},
{licenses,["Apache-2.0"]},
{maintainers, ["Feng Lee <feng@emqx.io>"]},
{links,[{"Github","https://github.com/emqx/ecpool"}]}
]}.

View File

@ -16,27 +16,36 @@
-module(ecpool).
-export([pool_spec/4, start_pool/3, start_sup_pool/3, stop_sup_pool/1,
get_client/1, get_client/2, with_client/2, with_client/3,
set_reconnect_callback/2,
name/1, workers/1]).
-export([ pool_spec/4
, start_pool/3
, start_sup_pool/3
, 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,
pool_type/0,
option/0
-export([set_reconnect_callback/2]).
-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()}
| {auto_reconnect, false | pos_integer()}
| {on_reconnect, reconn_callback()}
| tuple().
| tuple()).
pool_spec(ChildId, Pool, Mod, Opts) ->
#{id => ChildId,

View File

@ -18,22 +18,22 @@
-behaviour(gen_server).
-import(proplists, [get_value/3]).
%% API Function Exports
-export([start_link/2]).
-export([info/1]).
%% gen_server Function Exports
-export([init/1,
handle_call/3,
handle_cast/2,
handle_info/2,
terminate/2,
code_change/3
-export([ init/1
, handle_call/3
, handle_cast/2
, handle_info/2
, terminate/2
, code_change/3
]).
-import(proplists, [get_value/3]).
-record(state, {name, size, type}).
%%--------------------------------------------------------------------

View File

@ -21,9 +21,9 @@
-export([start_link/0]).
%% API
-export([start_pool/3,
stop_pool/1,
get_pool/1
-export([ start_pool/3
, stop_pool/1
, get_pool/1
]).
-export([pools/0]).

View File

@ -21,18 +21,30 @@
-export([start_link/4]).
%% API Function Exports
-export([client/1, is_connected/1, set_reconnect_callback/2]).
%% gen_server Function Exports
-export([init/1,
handle_call/3,
handle_cast/2,
handle_info/2,
terminate/2,
code_change/3
-export([ client/1
, is_connected/1
, set_reconnect_callback/2
]).
-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
@ -84,15 +96,18 @@ set_reconnect_callback(Pid, OnReconnect) ->
init([Pool, Id, Mod, Opts]) ->
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_disconnect = proplists:get_value(on_disconnect, Opts)},
on_disconnect = proplists:get_value(on_disconnect, Opts)
},
case connect_internal(State) of
{ok, NewState} ->
{ok, NewState} ->
gproc_pool:connect_worker(ecpool:name(Pool), {Pool, Id}),
{ok, NewState};
{error, Error} ->
{stop, Error}
{error, Error} -> {stop, Error}
end.
handle_call(is_connected, _From, State = #state{client = Client}) ->
@ -165,10 +180,6 @@ connopts([{pool_type, _}|Opts], Acc) ->
connopts(Opts, Acc);
connopts([{auto_reconnect, _}|Opts], Acc) ->
connopts(Opts, Acc);
connopts([{bind, _}|Opts], Acc) ->
connopts(Opts, Acc);
connopts([{unbind, _}|Opts], Acc) ->
connopts(Opts, Acc);
connopts([Opt|Opts], Acc) ->
connopts(Opts, [Opt|Acc]).
@ -203,3 +214,4 @@ connect_internal(State) ->
catch
_C:Reason -> {error, Reason}
end.