Correctly handle mod_client_state (re)load

This commit is contained in:
Evgeniy Khramtsov 2017-10-05 11:03:36 +03:00
parent bd06bc00e2
commit f3af117108
1 changed files with 17 additions and 7 deletions

View File

@ -187,7 +187,7 @@ unregister_hooks(Host) ->
%%--------------------------------------------------------------------
-spec c2s_stream_started(c2s_state(), stream_start()) -> c2s_state().
c2s_stream_started(State, _) ->
State#{csi_state => active, csi_queue => queue_new()}.
init_csi_state(State).
-spec c2s_authenticated_packet(c2s_state(), xmpp_element()) -> c2s_state().
c2s_authenticated_packet(C2SState, #csi{type = active}) ->
@ -284,6 +284,10 @@ add_stream_feature(Features, Host) ->
%%--------------------------------------------------------------------
%% Internal functions.
%%--------------------------------------------------------------------
-spec init_csi_state(c2s_state()) -> c2s_state().
init_csi_state(C2SState) ->
C2SState#{csi_state => active, csi_queue => queue_new()}.
-spec enqueue_stanza(csi_type(), stanza(), c2s_state()) -> filter_acc().
enqueue_stanza(Type, Stanza, #{csi_state := inactive,
csi_queue := Q} = C2SState) ->
@ -302,12 +306,18 @@ enqueue_stanza(_Type, Stanza, State) ->
-spec dequeue_sender(jid(), c2s_state()) -> c2s_state().
dequeue_sender(#jid{luser = U, lserver = S} = Sender,
#{csi_queue := Q, jid := JID} = C2SState) ->
?DEBUG("Flushing packets of ~s@~s from CSI queue of ~s",
[U, S, jid:encode(JID)]),
{Elems, Q1} = queue_take(Sender, Q),
C2SState1 = flush_stanzas(C2SState, Elems),
C2SState1#{csi_queue => Q1}.
#{jid := JID} = C2SState) ->
case maps:get(csi_queue, C2SState, undefined) of
undefined ->
%% This may happen when the module is (re)loaded in runtime
init_csi_state(C2SState);
Q ->
?DEBUG("Flushing packets of ~s@~s from CSI queue of ~s",
[U, S, jid:encode(JID)]),
{Elems, Q1} = queue_take(Sender, Q),
C2SState1 = flush_stanzas(C2SState, Elems),
C2SState1#{csi_queue => Q1}
end.
-spec flush_queue(c2s_state()) -> c2s_state().
flush_queue(#{csi_queue := Q, jid := JID} = C2SState) ->