Fix function_clause error and improve ws channel
This commit is contained in:
parent
64148ac0e8
commit
32b2a01d68
|
@ -259,8 +259,7 @@ connected(enter, _PrevSt, State = #state{proto_state = ProtoState}) ->
|
||||||
%% Ensure keepalive after connected successfully.
|
%% Ensure keepalive after connected successfully.
|
||||||
Interval = emqx_protocol:info(keepalive, ProtoState),
|
Interval = emqx_protocol:info(keepalive, ProtoState),
|
||||||
case ensure_keepalive(Interval, NState) of
|
case ensure_keepalive(Interval, NState) of
|
||||||
ignore ->
|
ignore -> keep_state(NState);
|
||||||
keep_state(NState);
|
|
||||||
{ok, KeepAlive} ->
|
{ok, KeepAlive} ->
|
||||||
keep_state(NState#state{keepalive = KeepAlive});
|
keep_state(NState#state{keepalive = KeepAlive});
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
|
|
|
@ -270,7 +270,7 @@ websocket_info(Deliver = {deliver, _Topic, _Msg},
|
||||||
{ok, NProtoState} ->
|
{ok, NProtoState} ->
|
||||||
reply(State#state{proto_state = NProtoState});
|
reply(State#state{proto_state = NProtoState});
|
||||||
{ok, Packets, NProtoState} ->
|
{ok, Packets, NProtoState} ->
|
||||||
reply(Packets, State#state{proto_state = NProtoState});
|
reply(enqueue(Packets, State#state{proto_state = NProtoState}));
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
stop(Reason, State);
|
stop(Reason, State);
|
||||||
{error, Reason, NProtoState} ->
|
{error, Reason, NProtoState} ->
|
||||||
|
@ -282,7 +282,6 @@ websocket_info({keepalive, check}, State = #state{keepalive = KeepAlive}) ->
|
||||||
{ok, KeepAlive1} ->
|
{ok, KeepAlive1} ->
|
||||||
{ok, State#state{keepalive = KeepAlive1}};
|
{ok, State#state{keepalive = KeepAlive1}};
|
||||||
{error, timeout} ->
|
{error, timeout} ->
|
||||||
?LOG(debug, "Keepalive Timeout!"),
|
|
||||||
stop(keepalive_timeout, State);
|
stop(keepalive_timeout, State);
|
||||||
{error, Error} ->
|
{error, Error} ->
|
||||||
?LOG(error, "Keepalive error: ~p", [Error]),
|
?LOG(error, "Keepalive error: ~p", [Error]),
|
||||||
|
@ -315,7 +314,7 @@ websocket_info({timeout, Timer, Msg},
|
||||||
{ok, NProtoState} ->
|
{ok, NProtoState} ->
|
||||||
{ok, State#state{proto_state = NProtoState}};
|
{ok, State#state{proto_state = NProtoState}};
|
||||||
{ok, Packets, NProtoState} ->
|
{ok, Packets, NProtoState} ->
|
||||||
reply(Packets, State#state{proto_state = NProtoState});
|
reply(enqueue(Packets, State#state{proto_state = NProtoState}));
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
stop(Reason, State);
|
stop(Reason, State);
|
||||||
{error, Reason, NProtoState} ->
|
{error, Reason, NProtoState} ->
|
||||||
|
@ -364,8 +363,7 @@ connected(State = #state{proto_state = ProtoState}) ->
|
||||||
%% Ensure keepalive after connected successfully.
|
%% Ensure keepalive after connected successfully.
|
||||||
Interval = emqx_protocol:info(keepalive, ProtoState),
|
Interval = emqx_protocol:info(keepalive, ProtoState),
|
||||||
case ensure_keepalive(Interval, NState) of
|
case ensure_keepalive(Interval, NState) of
|
||||||
ignore ->
|
ignore -> reply(NState);
|
||||||
reply(NState);
|
|
||||||
{ok, KeepAlive} ->
|
{ok, KeepAlive} ->
|
||||||
reply(NState#state{keepalive = KeepAlive});
|
reply(NState#state{keepalive = KeepAlive});
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
|
@ -377,16 +375,10 @@ connected(State = #state{proto_state = ProtoState}) ->
|
||||||
|
|
||||||
ensure_keepalive(0, _State) ->
|
ensure_keepalive(0, _State) ->
|
||||||
ignore;
|
ignore;
|
||||||
ensure_keepalive(Interval, State = #state{proto_state = ProtoState}) ->
|
ensure_keepalive(Interval, #state{proto_state = ProtoState}) ->
|
||||||
Backoff = emqx_zone:get_env(emqx_protocol:info(zone, ProtoState),
|
Backoff = emqx_zone:get_env(emqx_protocol:info(zone, ProtoState),
|
||||||
keepalive_backoff, 0.75),
|
keepalive_backoff, 0.75),
|
||||||
case emqx_keepalive:start(stat_fun(), round(Interval * Backoff), {keepalive, check}) of
|
emqx_keepalive:start(stat_fun(), round(Interval * Backoff), {keepalive, check}).
|
||||||
{ok, KeepAlive} ->
|
|
||||||
{ok, State#state{keepalive = KeepAlive}};
|
|
||||||
{error, Reason} ->
|
|
||||||
?LOG(warning, "Keepalive error: ~p", [Reason]),
|
|
||||||
stop(Reason, State)
|
|
||||||
end.
|
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Process incoming data
|
%% Process incoming data
|
||||||
|
@ -415,8 +407,7 @@ process_incoming(Data, State = #state{parse_state = ParseState}) ->
|
||||||
%% Handle incoming packets
|
%% Handle incoming packets
|
||||||
|
|
||||||
handle_incoming(Packet = ?PACKET(Type), SuccFun,
|
handle_incoming(Packet = ?PACKET(Type), SuccFun,
|
||||||
State = #state{proto_state = ProtoState,
|
State = #state{proto_state = ProtoState}) ->
|
||||||
pendings = Pendings}) ->
|
|
||||||
_ = inc_incoming_stats(Type),
|
_ = inc_incoming_stats(Type),
|
||||||
ok = emqx_metrics:inc_recv(Packet),
|
ok = emqx_metrics:inc_recv(Packet),
|
||||||
?LOG(debug, "RECV ~s", [emqx_packet:format(Packet)]),
|
?LOG(debug, "RECV ~s", [emqx_packet:format(Packet)]),
|
||||||
|
@ -424,9 +415,7 @@ handle_incoming(Packet = ?PACKET(Type), SuccFun,
|
||||||
{ok, NProtoState} ->
|
{ok, NProtoState} ->
|
||||||
SuccFun(State#state{proto_state = NProtoState});
|
SuccFun(State#state{proto_state = NProtoState});
|
||||||
{ok, OutPackets, NProtoState} ->
|
{ok, OutPackets, NProtoState} ->
|
||||||
Pendings1 = lists:append(Pendings, OutPackets),
|
SuccFun(enqueue(OutPackets, State#state{proto_state = NProtoState}));
|
||||||
SuccFun(State#state{proto_state = NProtoState,
|
|
||||||
pendings = Pendings1});
|
|
||||||
{error, Reason, NProtoState} ->
|
{error, Reason, NProtoState} ->
|
||||||
stop(Reason, State#state{proto_state = NProtoState});
|
stop(Reason, State#state{proto_state = NProtoState});
|
||||||
{stop, Error, NProtoState} ->
|
{stop, Error, NProtoState} ->
|
||||||
|
@ -436,9 +425,6 @@ handle_incoming(Packet = ?PACKET(Type), SuccFun,
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Handle outgoing packets
|
%% Handle outgoing packets
|
||||||
|
|
||||||
handle_outgoing(Packet, State) when is_tuple(Packet) ->
|
|
||||||
handle_outgoing([Packet], State);
|
|
||||||
|
|
||||||
handle_outgoing(Packets, #state{serialize = Serialize}) ->
|
handle_outgoing(Packets, #state{serialize = Serialize}) ->
|
||||||
Data = lists:map(Serialize, Packets),
|
Data = lists:map(Serialize, Packets),
|
||||||
emqx_pd:update_counter(send_oct, iolist_size(Data)),
|
emqx_pd:update_counter(send_oct, iolist_size(Data)),
|
||||||
|
@ -471,10 +457,6 @@ inc_outgoing_stats(Type) ->
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Reply or Stop
|
%% Reply or Stop
|
||||||
|
|
||||||
reply(Packets, State = #state{pendings = Pendings}) ->
|
|
||||||
Pendings1 = lists:append(Pendings, Packets),
|
|
||||||
reply(State#state{pendings = Pendings1}).
|
|
||||||
|
|
||||||
reply(State = #state{pendings = []}) ->
|
reply(State = #state{pendings = []}) ->
|
||||||
{ok, State};
|
{ok, State};
|
||||||
reply(State = #state{pendings = Pendings}) ->
|
reply(State = #state{pendings = Pendings}) ->
|
||||||
|
@ -488,6 +470,11 @@ stop(Reason, State = #state{pendings = Pendings}) ->
|
||||||
{reply, [Reply, close],
|
{reply, [Reply, close],
|
||||||
State#state{pendings = [], reason = Reason}}.
|
State#state{pendings = [], reason = Reason}}.
|
||||||
|
|
||||||
|
enqueue(Packet, State) when is_record(Packet, mqtt_packet) ->
|
||||||
|
enqueue([Packet], State);
|
||||||
|
enqueue(Packets, State = #state{pendings = Pendings}) ->
|
||||||
|
State#state{pendings = lists:append(Pendings, Packets)}.
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Ensure stats timer
|
%% Ensure stats timer
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue