Spec Syntax

This commit is contained in:
Feng 2016-04-13 00:29:23 +08:00
parent f1cbfee1fc
commit 6befd736e7
3 changed files with 16 additions and 16 deletions

View File

@ -42,7 +42,7 @@ pool_spec(ChildId, Pool, Mod, Opts) ->
permanent, 5000, supervisor, [ecpool_pool_sup]}. permanent, 5000, supervisor, [ecpool_pool_sup]}.
%% @doc Start the pool %% @doc Start the pool
-spec start_pool(atom(), atom(), [option()]) -> {ok, pid()} | {error, any()}. -spec(start_pool(atom(), atom(), [option()]) -> {ok, pid()} | {error, any()}).
start_pool(Pool, Mod, Opts) when is_atom(Pool) -> start_pool(Pool, Mod, Opts) when is_atom(Pool) ->
ecpool_pool_sup:start_link(Pool, Mod, Opts). ecpool_pool_sup:start_link(Pool, Mod, Opts).
@ -55,22 +55,22 @@ stop_sup_pool(Pool) when is_atom(Pool) ->
ecpool_sup:stop_pool(Pool). ecpool_sup:stop_pool(Pool).
%% @doc Get client/connection %% @doc Get client/connection
-spec get_client(atom()) -> pid(). -spec(get_client(atom()) -> pid()).
get_client(Pool) -> get_client(Pool) ->
gproc_pool:pick_worker(name(Pool)). gproc_pool:pick_worker(name(Pool)).
%% @doc Get client/connection with hash key. %% @doc Get client/connection with hash key.
-spec get_client(atom(), any()) -> pid(). -spec(get_client(atom(), any()) -> pid()).
get_client(Pool, Key) -> get_client(Pool, Key) ->
gproc_pool:pick_worker(name(Pool), Key). gproc_pool:pick_worker(name(Pool), Key).
%% @doc Call the fun with client/connection %% @doc Call the fun with client/connection
-spec with_client(atom(), fun((Client :: pid()) -> any())) -> any(). -spec(with_client(atom(), fun((Client :: pid()) -> any())) -> any()).
with_client(Pool, Fun) when is_atom(Pool) -> with_client(Pool, Fun) when is_atom(Pool) ->
with_worker(gproc_pool:pick_worker(name(Pool)), Fun). with_worker(gproc_pool:pick_worker(name(Pool)), Fun).
%% @doc Call the fun with client/connection %% @doc Call the fun with client/connection
-spec with_client(atom(), any(), fun((Client :: pid()) -> any())) -> any(). -spec(with_client(atom(), any(), fun((Client :: pid()) -> any())) -> any()).
with_client(Pool, Key, Fun) when is_atom(Pool) -> with_client(Pool, Key, Fun) when is_atom(Pool) ->
with_worker(gproc_pool:pick_worker(name(Pool), Key), Fun). with_worker(gproc_pool:pick_worker(name(Pool), Key), Fun).

View File

@ -35,14 +35,14 @@
-export([init/1]). -export([init/1]).
%% @doc Start supervisor. %% @doc Start supervisor.
-spec start_link() -> {ok, pid()} | {error, any()}. -spec(start_link() -> {ok, pid()} | {error, any()}).
start_link() -> start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []). supervisor:start_link({local, ?MODULE}, ?MODULE, []).
start_pool(Pool, Mod, Opts) when is_atom(Pool) -> start_pool(Pool, Mod, Opts) when is_atom(Pool) ->
supervisor:start_child(?MODULE, pool_spec(Pool, Mod, Opts)). supervisor:start_child(?MODULE, pool_spec(Pool, Mod, Opts)).
-spec stop_pool(Pool :: atom()) -> ok | {error, any()}. -spec(stop_pool(Pool :: atom()) -> ok | {error, any()}).
stop_pool(Pool) when is_atom(Pool) -> stop_pool(Pool) when is_atom(Pool) ->
ChildId = child_id(Pool), ChildId = child_id(Pool),
case supervisor:terminate_child(?MODULE, ChildId) of case supervisor:terminate_child(?MODULE, ChildId) of
@ -53,13 +53,13 @@ stop_pool(Pool) when is_atom(Pool) ->
end. end.
%% @doc All Pools supervisored by ecpool_sup. %% @doc All Pools supervisored by ecpool_sup.
-spec pools() -> [{atom(), pid()}]. -spec(pools() -> [{atom(), pid()}]).
pools() -> pools() ->
[{Pool, Pid} || {{pool_sup, Pool}, Pid, supervisor, _} [{Pool, Pid} || {{pool_sup, Pool}, Pid, supervisor, _}
<- supervisor:which_children(?MODULE)]. <- supervisor:which_children(?MODULE)].
%% @doc Find a pool. %% @doc Find a pool.
-spec pool(atom()) -> undefined | pid(). -spec(pool(atom()) -> undefined | pid()).
pool(Pool) when is_atom(Pool) -> pool(Pool) when is_atom(Pool) ->
ChildId = child_id(Pool), ChildId = child_id(Pool),
case [Pid || {Id, Pid, supervisor, _} <- supervisor:which_children(?MODULE), Id =:= ChildId] of case [Pid || {Id, Pid, supervisor, _} <- supervisor:which_children(?MODULE), Id =:= ChildId] of

View File

@ -61,19 +61,19 @@ behaviour_info(_Other) ->
%%% API %%% API
%%%============================================================================= %%%=============================================================================
%% @doc Start the pool worker. %% @doc Start a pool worker.
-spec start_link(atom(), pos_integer(), module(), list()) -> -spec(start_link(atom(), pos_integer(), module(), list()) ->
{ok, pid()} | ignore | {error, any()}. {ok, pid()} | ignore | {error, any()}).
start_link(Pool, Id, Mod, Opts) -> start_link(Pool, Id, Mod, Opts) ->
gen_server:start_link(?MODULE, [Pool, Id, Mod, Opts], []). gen_server:start_link(?MODULE, [Pool, Id, Mod, Opts], []).
%% @doc Get client/connection. %% @doc Get client/connection.
-spec client(pid()) -> undefined | pid(). -spec(client(pid()) -> undefined | pid()).
client(Pid) -> client(Pid) ->
gen_server:call(Pid, client, infinity). gen_server:call(Pid, client, infinity).
%% @doc Is client connected? %% @doc Is client connected?
-spec is_connected(pid()) -> boolean(). -spec(is_connected(pid()) -> boolean()).
is_connected(Pid) -> is_connected(Pid) ->
gen_server:call(Pid, is_connected). gen_server:call(Pid, is_connected).
@ -113,10 +113,10 @@ handle_info({'EXIT', Pid, Reason}, State = #state{client = Pid, opts = Opts}) ->
end; end;
handle_info(reconnect, State = #state{opts = Opts}) -> handle_info(reconnect, State = #state{opts = Opts}) ->
case connect(State) of case catch connect(State) of
{ok, Client} -> {ok, Client} ->
{noreply, State#state{client = Client}}; {noreply, State#state{client = Client}};
{error, _Error} -> {Err, _Reason} when Err =:= error orelse Err =:= 'EXIT' ->
reconnect(proplists:get_value(auto_reconnect, Opts), State) reconnect(proplists:get_value(auto_reconnect, Opts), State)
end; end;