Match {error,einval}

This commit is contained in:
Frank Feng 2017-03-12 23:06:57 +08:00
parent 80a7cbf09e
commit 6472457342
2 changed files with 21 additions and 13 deletions

View File

@ -252,8 +252,13 @@ handle_info({keepalive, start, Interval}, State = #client_state{connection = Con
{error, Error} -> {error, Error}
end
end,
KeepAlive = emqttd_keepalive:start(StatFun, Interval, {keepalive, check}),
{noreply, State#client_state{keepalive = KeepAlive}, hibernate};
case emqttd_keepalive:start(StatFun, Interval, {keepalive, check}) of
{ok, KeepAlive} ->
{noreply, State#client_state{keepalive = KeepAlive}, hibernate};
{error, Error} ->
?LOG(warning, "Keepalive error - ~p", [Error], State),
shutdown(Error, State)
end;
handle_info({keepalive, check}, State = #client_state{keepalive = KeepAlive}) ->
case emqttd_keepalive:check(KeepAlive) of

View File

@ -29,14 +29,18 @@
-export_type([keepalive/0]).
%% @doc Start a keepalive
-spec(start(fun(), integer(), any()) -> undefined | keepalive()).
-spec(start(fun(), integer(), any()) -> {ok, keepalive()} | {error, any()}).
start(_, 0, _) ->
undefined;
{ok, #keepalive{}};
start(StatFun, TimeoutSec, TimeoutMsg) ->
{ok, StatVal} = StatFun(),
#keepalive{statfun = StatFun, statval = StatVal,
tsec = TimeoutSec, tmsg = TimeoutMsg,
tref = timer(TimeoutSec, TimeoutMsg)}.
case StatFun() of
{ok, StatVal} ->
{ok, #keepalive{statfun = StatFun, statval = StatVal,
tsec = TimeoutSec, tmsg = TimeoutMsg,
tref = timer(TimeoutSec, TimeoutMsg)}};
{error, Error} ->
{error, Error}
end.
%% @doc Check keepalive, called when timeout.
-spec(check(keepalive()) -> {ok, keepalive()} | {error, any()}).
@ -59,12 +63,11 @@ resume(KeepAlive = #keepalive{tsec = TimeoutSec, tmsg = TimeoutMsg}) ->
%% @doc Cancel Keepalive
-spec(cancel(keepalive()) -> ok).
cancel(#keepalive{tref = TRef}) ->
cancel(TRef);
cancel(undefined) ->
cancel(#keepalive{tref = TRef}) when is_reference(TRef) ->
erlang:cancel_timer(TRef),
ok;
cancel(TRef) ->
catch erlang:cancel_timer(TRef).
cancel(_) ->
ok.
timer(Sec, Msg) ->
erlang:send_after(timer:seconds(Sec), self(), Msg).