Skip to content

Commit

Permalink
Add delays to SM buffer
Browse files Browse the repository at this point in the history
It is rerouted when no session was resumed and the resume_timeout has passed.
This resulted in suprising messages, sometimes after a long time.
  • Loading branch information
gustawlippa committed Dec 3, 2024
1 parent de22c49 commit 014ce65
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/stream_management/mod_stream_management.erl
Original file line number Diff line number Diff line change
Expand Up @@ -386,14 +386,28 @@ maybe_handle_stream_mgmt_reroute(Acc, _StateData, _HostType, _Reason, {error, no
handle_user_terminate(#sm_state{counter_in = H} = SmState, StateData, HostType) ->
Sid = mongoose_c2s:get_sid(StateData),
do_remove_smid(HostType, Sid, H),
reroute_buffer(StateData, SmState),
FromServer = mongoose_c2s:get_lserver(StateData),
NewState = add_delay_elements_to_buffer(SmState, FromServer),
reroute_buffer(StateData, NewState),
SmState#sm_state{buffer = [], buffer_size = 0}.

reroute_buffer(StateData, #sm_state{buffer = Buffer, peer = {gen_statem, {Pid, _}}}) ->
mongoose_c2s:reroute_buffer_to_pid(StateData, Pid, Buffer);
reroute_buffer(StateData, #sm_state{buffer = Buffer}) ->
mongoose_c2s:reroute_buffer(StateData, Buffer).

add_delay_elements_to_buffer(#sm_state{buffer = Buffer} = SmState, FromServer) ->
BufferWithDelays = [begin
TS = mongoose_acc:timestamp(Acc),
StanzaName = mongoose_acc:stanza_name(Acc),
StanzaType = mongoose_acc:stanza_type(Acc),
{From, To, El} = mongoose_acc:packet(Acc),
ElWithDelay = maybe_add_timestamp(El, StanzaName, StanzaType, TS, FromServer),
AccParams = #{from_jid => From, to_jid => To, element => ElWithDelay},
mongoose_acc:update_stanza(AccParams, Acc)
end || Acc <- Buffer],
SmState#sm_state{buffer = BufferWithDelays}.

-spec terminate(term(), c2s_state(), mongoose_c2s:data()) -> term().
terminate(Reason, C2SState, StateData) ->
?LOG_DEBUG(#{what => stream_mgmt_statem_terminate, reason => Reason,
Expand Down

0 comments on commit 014ce65

Please sign in to comment.