From dbb6bab31e09a6810331ffcb018e973b2bbe9fad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Sun, 22 Sep 2019 16:10:10 +0200 Subject: Cancel the existing keepalive when switching protocols --- src/gun.erl | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/gun.erl b/src/gun.erl index 6efca0b..8eaa55e 100644 --- a/src/gun.erl +++ b/src/gun.erl @@ -1305,10 +1305,13 @@ commands([{switch_protocol, Protocol0}], State0=#state{ end, {StateName, ProtoState} = Protocol:init(Owner, Socket, Transport, ProtoOpts), EvHandlerState = EvHandler:protocol_changed(#{protocol => Protocol:name()}, EvHandlerState0), - State = State0#state{protocol=Protocol, protocol_state=ProtoState, event_handler_state=EvHandlerState}, + %% We cancel the existing keepalive and, depending on the protocol, + %% we enable keepalive again, effectively resetting the timer. + State = keepalive_cancel(active(State0#state{protocol=Protocol, protocol_state=ProtoState, + event_handler_state=EvHandlerState})), case Protocol:has_keepalive() of - true -> {next_state, StateName, keepalive_timeout(active(State))}; - false -> {next_state, StateName, keepalive_cancel(active(State))} + true -> {next_state, StateName, keepalive_timeout(State)}; + false -> {next_state, StateName, State} end; %% Perform a TLS handshake. commands([TLSHandshake={tls_handshake, _, _}], State) -> -- cgit v1.2.3