fix issue #486 - gen_server2:call/3 to register a client

This commit is contained in:
Feng 2016-03-30 19:33:53 +08:00
parent 5412dda622
commit a758423a2e
1 changed files with 17 additions and 17 deletions

View File

@ -70,14 +70,14 @@ lookup_proc(ClientId) when is_binary(ClientId) ->
%% @doc Register ClientId with Pid. %% @doc Register ClientId with Pid.
-spec(register(Client :: mqtt_client()) -> ok). -spec(register(Client :: mqtt_client()) -> ok).
register(Client = #mqtt_client{client_id = ClientId}) -> register(Client = #mqtt_client{client_id = ClientId}) ->
CmPid = gproc_pool:pick_worker(?POOL, ClientId), gen_server2:call(pick(ClientId), {register, Client}, 120000).
gen_server2:cast(CmPid, {register, Client}).
%% @doc Unregister clientId with pid. %% @doc Unregister clientId with pid.
-spec(unregister(ClientId :: binary()) -> ok). -spec(unregister(ClientId :: binary()) -> ok).
unregister(ClientId) when is_binary(ClientId) -> unregister(ClientId) when is_binary(ClientId) ->
CmPid = gproc_pool:pick_worker(?POOL, ClientId), gen_server2:cast(pick(ClientId), {unregister, ClientId, self()}).
gen_server2:cast(CmPid, {unregister, ClientId, self()}).
pick(ClientId) -> gproc_pool:pick_worker(?POOL, ClientId).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% gen_server callbacks %% gen_server callbacks
@ -85,16 +85,16 @@ unregister(ClientId) when is_binary(ClientId) ->
init([Pool, Id, StatsFun]) -> init([Pool, Id, StatsFun]) ->
?GPROC_POOL(join, Pool, Id), ?GPROC_POOL(join, Pool, Id),
{ok, #state{pool = Pool, id = Id, {ok, #state{pool = Pool, id = Id, statsfun = StatsFun, monitors = dict:new()}}.
statsfun = StatsFun,
monitors = dict:new()}}.
prioritise_call(_Req, _From, _Len, _State) -> prioritise_call(Req, _From, _Len, _State) ->
1. case Req of
{register, _Client} -> 2;
_ -> 1
end.
prioritise_cast(Msg, _Len, _State) -> prioritise_cast(Msg, _Len, _State) ->
case Msg of case Msg of
{register, _Client} -> 2;
{unregister, _ClientId, _Pid} -> 9; {unregister, _ClientId, _Pid} -> 9;
_ -> 1 _ -> 1
end. end.
@ -102,19 +102,19 @@ prioritise_cast(Msg, _Len, _State) ->
prioritise_info(_Msg, _Len, _State) -> prioritise_info(_Msg, _Len, _State) ->
3. 3.
handle_call(Req, _From, State) -> handle_call({register, Client = #mqtt_client{client_id = ClientId,
?UNEXPECTED_REQ(Req, State). client_pid = Pid}}, _From, State) ->
handle_cast({register, Client = #mqtt_client{client_id = ClientId,
client_pid = Pid}}, State) ->
case lookup_proc(ClientId) of case lookup_proc(ClientId) of
Pid -> Pid ->
{noreply, State}; {reply, ok, State};
_ -> _ ->
ets:insert(mqtt_client, Client), ets:insert(mqtt_client, Client),
{noreply, setstats(monitor_client(ClientId, Pid, State))} {reply, ok, setstats(monitor_client(ClientId, Pid, State))}
end; end;
handle_call(Req, _From, State) ->
?UNEXPECTED_REQ(Req, State).
handle_cast({unregister, ClientId, Pid}, State) -> handle_cast({unregister, ClientId, Pid}, State) ->
case lookup_proc(ClientId) of case lookup_proc(ClientId) of
Pid -> Pid ->