fix(emqx_gateway): fix the function_clause error when client disconnect after connection created

This commit is contained in:
lafirest 2021-11-30 12:25:06 +08:00
parent e6d7e59c18
commit cfe2954a88
2 changed files with 14 additions and 11 deletions

View File

@ -98,10 +98,10 @@ info(ctx, #channel{ctx = Ctx}) ->
stats(_) -> stats(_) ->
[]. [].
init(ConnInfo = #{peername := {PeerHost, _}, init(ConnInfoT = #{peername := {PeerHost, _},
sockname := {_, SockPort}}, sockname := {_, SockPort}},
#{ctx := Ctx} = Config) -> #{ctx := Ctx} = Config) ->
Peercert = maps:get(peercert, ConnInfo, undefined), Peercert = maps:get(peercert, ConnInfoT, undefined),
Mountpoint = maps:get(mountpoint, Config, <<>>), Mountpoint = maps:get(mountpoint, Config, <<>>),
ListenerId = case maps:get(listener, Config, undefined) of ListenerId = case maps:get(listener, Config, undefined) of
undefined -> undefined; undefined -> undefined;
@ -123,6 +123,10 @@ init(ConnInfo = #{peername := {PeerHost, _},
} }
), ),
%% because it is possible to disconnect after init, and then trigger the $event.disconnected hook
%% and these two fields are required in the hook
ConnInfo = ConnInfoT#{proto_name => <<"CoAP">>, proto_ver => <<"1">>},
Heartbeat = ?GET_IDLE_TIME(Config), Heartbeat = ?GET_IDLE_TIME(Config),
#channel{ ctx = Ctx #channel{ ctx = Ctx
, conninfo = ConnInfo , conninfo = ConnInfo
@ -349,8 +353,6 @@ ensure_connected(Channel = #channel{ctx = Ctx,
conninfo = ConnInfo, conninfo = ConnInfo,
clientinfo = ClientInfo}) -> clientinfo = ClientInfo}) ->
NConnInfo = ConnInfo#{ connected_at => erlang:system_time(millisecond) NConnInfo = ConnInfo#{ connected_at => erlang:system_time(millisecond)
, proto_name => <<"COAP">>
, proto_ver => <<"1">>
}, },
ok = run_hooks(Ctx, 'client.connected', [ClientInfo, NConnInfo]), ok = run_hooks(Ctx, 'client.connected', [ClientInfo, NConnInfo]),
_ = run_hooks(Ctx, 'client.connack', [NConnInfo, connection_accepted, []]), _ = run_hooks(Ctx, 'client.connack', [NConnInfo, connection_accepted, []]),

View File

@ -93,10 +93,10 @@ info(ctx, #channel{ctx = Ctx}) ->
stats(_) -> stats(_) ->
[]. [].
init(ConnInfo = #{peername := {PeerHost, _}, init(ConnInfoT = #{peername := {PeerHost, _},
sockname := {_, SockPort}}, sockname := {_, SockPort}},
#{ctx := Ctx} = Config) -> #{ctx := Ctx} = Config) ->
Peercert = maps:get(peercert, ConnInfo, undefined), Peercert = maps:get(peercert, ConnInfoT, undefined),
Mountpoint = maps:get(mountpoint, Config, undefined), Mountpoint = maps:get(mountpoint, Config, undefined),
ListenerId = case maps:get(listener, Config, undefined) of ListenerId = case maps:get(listener, Config, undefined) of
undefined -> undefined; undefined -> undefined;
@ -118,6 +118,8 @@ init(ConnInfo = #{peername := {PeerHost, _},
} }
), ),
ConnInfo = ConnInfoT#{proto_name => <<"LwM2M">>, proto_ver => <<"0.0">>},
#channel{ ctx = Ctx #channel{ ctx = Ctx
, conninfo = ConnInfo , conninfo = ConnInfo
, clientinfo = ClientInfo , clientinfo = ClientInfo
@ -293,7 +295,6 @@ check_lwm2m_version(#coap_message{options = Opts},
end, end,
if IsValid -> if IsValid ->
NConnInfo = ConnInfo#{ connected_at => erlang:system_time(millisecond) NConnInfo = ConnInfo#{ connected_at => erlang:system_time(millisecond)
, proto_name => <<"LwM2M">>
, proto_ver => Ver , proto_ver => Ver
}, },
{ok, Channel#channel{conninfo = NConnInfo}}; {ok, Channel#channel{conninfo = NConnInfo}};