Replace 'state' record with map

This commit is contained in:
Feng Lee 2018-09-06 18:43:47 +08:00
parent 5774ba542c
commit 328d035dab
2 changed files with 14 additions and 12 deletions

View File

@ -23,16 +23,14 @@
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2,
code_change/3]). code_change/3]).
-record(state, {pool, id}).
-define(POOL, ?MODULE). -define(POOL, ?MODULE).
%% @doc Start pooler supervisor. %% @doc Start pooler supervisor.
start_link() -> start_link() ->
emqx_pool_sup:start_link(?POOL, random, {?MODULE, start_link, []}). emqx_pool_sup:start_link(?POOL, random, {?MODULE, start_link, []}).
%% @doc Start pool %% @doc Start pool.
-spec(start_link(atom(), pos_integer()) -> {ok, pid()} | ignore | {error, term()}). -spec(start_link(atom(), pos_integer()) -> emqx_types:startlink_ret()).
start_link(Pool, Id) -> start_link(Pool, Id) ->
gen_server:start_link({local, emqx_misc:proc_name(?MODULE, Id)}, ?MODULE, [Pool, Id], []). gen_server:start_link({local, emqx_misc:proc_name(?MODULE, Id)}, ?MODULE, [Pool, Id], []).
@ -49,13 +47,13 @@ async_submit(Fun) ->
worker() -> worker() ->
gproc_pool:pick_worker(pool). gproc_pool:pick_worker(pool).
%%----------------------------------------------------------------------------- %%------------------------------------------------------------------------------
%% gen_server callbacks %% gen_server callbacks
%%----------------------------------------------------------------------------- %%------------------------------------------------------------------------------
init([Pool, Id]) -> init([Pool, Id]) ->
true = gproc_pool:connect_worker(Pool, {Pool, Id}), true = gproc_pool:connect_worker(Pool, {Pool, Id}),
{ok, #state{pool = Pool, id = Id}}. {ok, #{pool => Pool, id => Id}}.
handle_call({submit, Fun}, _From, State) -> handle_call({submit, Fun}, _From, State) ->
{reply, catch run(Fun), State}; {reply, catch run(Fun), State};
@ -79,15 +77,15 @@ handle_info(Info, State) ->
emqx_logger:error("[Pool] unexpected info: ~p", [Info]), emqx_logger:error("[Pool] unexpected info: ~p", [Info]),
{noreply, State}. {noreply, State}.
terminate(_Reason, #state{pool = Pool, id = Id}) -> terminate(_Reason, #{pool := Pool, id := Id}) ->
true = gproc_pool:disconnect_worker(Pool, {Pool, Id}). true = gproc_pool:disconnect_worker(Pool, {Pool, Id}).
code_change(_OldVsn, State, _Extra) -> code_change(_OldVsn, State, _Extra) ->
{ok, State}. {ok, State}.
%%----------------------------------------------------------------------------- %%------------------------------------------------------------------------------
%% Internal functions %% Internal functions
%%----------------------------------------------------------------------------- %%------------------------------------------------------------------------------
run({M, F, A}) -> run({M, F, A}) ->
erlang:apply(M, F, A); erlang:apply(M, F, A);

View File

@ -26,8 +26,12 @@ spec(Args) ->
-spec(spec(any(), list()) -> supervisor:child_spec()). -spec(spec(any(), list()) -> supervisor:child_spec()).
spec(ChildId, Args) -> spec(ChildId, Args) ->
{ChildId, {?MODULE, start_link, Args}, #{id => ChildId,
transient, infinity, supervisor, [?MODULE]}. start => {?MODULE, start_link, Args},
restart => transient,
shutdown => infinity,
type => supervisor,
modules => [?MODULE]}.
-spec(start_link(atom() | tuple(), atom(), mfa()) -> {ok, pid()} | {error, term()}). -spec(start_link(atom() | tuple(), atom(), mfa()) -> {ok, pid()} | {error, term()}).
start_link(Pool, Type, MFA) -> start_link(Pool, Type, MFA) ->