From 6befd736e7246b86d4a98e4f160c42272effdec0 Mon Sep 17 00:00:00 2001 From: Feng Date: Wed, 13 Apr 2016 00:29:23 +0800 Subject: [PATCH] Spec Syntax --- src/ecpool.erl | 10 +++++----- src/ecpool_sup.erl | 8 ++++---- src/ecpool_worker.erl | 14 +++++++------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/ecpool.erl b/src/ecpool.erl index 5bdc24ac8..d1627a4f6 100644 --- a/src/ecpool.erl +++ b/src/ecpool.erl @@ -42,7 +42,7 @@ pool_spec(ChildId, Pool, Mod, Opts) -> permanent, 5000, supervisor, [ecpool_pool_sup]}. %% @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) -> 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). %% @doc Get client/connection --spec get_client(atom()) -> pid(). +-spec(get_client(atom()) -> pid()). get_client(Pool) -> gproc_pool:pick_worker(name(Pool)). %% @doc Get client/connection with hash key. --spec get_client(atom(), any()) -> pid(). +-spec(get_client(atom(), any()) -> pid()). get_client(Pool, Key) -> gproc_pool:pick_worker(name(Pool), Key). %% @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_worker(gproc_pool:pick_worker(name(Pool)), Fun). %% @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_worker(gproc_pool:pick_worker(name(Pool), Key), Fun). diff --git a/src/ecpool_sup.erl b/src/ecpool_sup.erl index 5bb0d5092..1b3f15802 100644 --- a/src/ecpool_sup.erl +++ b/src/ecpool_sup.erl @@ -35,14 +35,14 @@ -export([init/1]). %% @doc Start supervisor. --spec start_link() -> {ok, pid()} | {error, any()}. +-spec(start_link() -> {ok, pid()} | {error, any()}). start_link() -> supervisor:start_link({local, ?MODULE}, ?MODULE, []). start_pool(Pool, Mod, Opts) when is_atom(Pool) -> 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) -> ChildId = child_id(Pool), case supervisor:terminate_child(?MODULE, ChildId) of @@ -53,13 +53,13 @@ stop_pool(Pool) when is_atom(Pool) -> end. %% @doc All Pools supervisored by ecpool_sup. --spec pools() -> [{atom(), pid()}]. +-spec(pools() -> [{atom(), pid()}]). pools() -> [{Pool, Pid} || {{pool_sup, Pool}, Pid, supervisor, _} <- supervisor:which_children(?MODULE)]. %% @doc Find a pool. --spec pool(atom()) -> undefined | pid(). +-spec(pool(atom()) -> undefined | pid()). pool(Pool) when is_atom(Pool) -> ChildId = child_id(Pool), case [Pid || {Id, Pid, supervisor, _} <- supervisor:which_children(?MODULE), Id =:= ChildId] of diff --git a/src/ecpool_worker.erl b/src/ecpool_worker.erl index a56890eb5..c1b36d78c 100644 --- a/src/ecpool_worker.erl +++ b/src/ecpool_worker.erl @@ -61,19 +61,19 @@ behaviour_info(_Other) -> %%% API %%%============================================================================= -%% @doc Start the pool worker. --spec start_link(atom(), pos_integer(), module(), list()) -> - {ok, pid()} | ignore | {error, any()}. +%% @doc Start a pool worker. +-spec(start_link(atom(), pos_integer(), module(), list()) -> + {ok, pid()} | ignore | {error, any()}). start_link(Pool, Id, Mod, Opts) -> gen_server:start_link(?MODULE, [Pool, Id, Mod, Opts], []). %% @doc Get client/connection. --spec client(pid()) -> undefined | pid(). +-spec(client(pid()) -> undefined | pid()). client(Pid) -> gen_server:call(Pid, client, infinity). %% @doc Is client connected? --spec is_connected(pid()) -> boolean(). +-spec(is_connected(pid()) -> boolean()). is_connected(Pid) -> gen_server:call(Pid, is_connected). @@ -113,10 +113,10 @@ handle_info({'EXIT', Pid, Reason}, State = #state{client = Pid, opts = Opts}) -> end; handle_info(reconnect, State = #state{opts = Opts}) -> - case connect(State) of + case catch connect(State) of {ok, Client} -> {noreply, State#state{client = Client}}; - {error, _Error} -> + {Err, _Reason} when Err =:= error orelse Err =:= 'EXIT' -> reconnect(proplists:get_value(auto_reconnect, Opts), State) end;