From c1f9122ab2a646df9182e51e3181de6ffa71af0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20S=C3=B6derqvist?= Date: Thu, 12 May 2022 23:37:49 +0200 Subject: Add keepalive_tolerance http2 option MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The number of unacknowledged pings that can be tolerated before the connection is forcefully closed. When a keepalive ping is sent to the peer, a counter is incremented and if this counter exceeds the tolerance limit, the connection is forcefully closed. The counter is decremented whenever a ping ack is received from the peer. By default, the mechanism for closing the connection based on ping and ping ack is disabled. Loïc Hoguin: I have edited a lot of the code and renamed a few things as well as simplified the docs and increased test timeouts to avoid race conditions. --- src/gun.erl | 1 + src/gun_http2.erl | 28 +++++++++++++++++++++++----- 2 files changed, 24 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/gun.erl b/src/gun.erl index b27ea6e..976a137 100644 --- a/src/gun.erl +++ b/src/gun.erl @@ -223,6 +223,7 @@ cookie_ignore_informational => boolean(), flow => pos_integer(), keepalive => timeout(), + keepalive_tolerance => non_neg_integer(), notify_settings_changed => boolean(), %% Options copied from cow_http2_machine. diff --git a/src/gun_http2.erl b/src/gun_http2.erl index 292504b..c37b48a 100644 --- a/src/gun_http2.erl +++ b/src/gun_http2.erl @@ -113,7 +113,12 @@ %% the idea, that's why the main map has the ID as key. Then we also %% have a Ref->ID index for faster lookup when we only have the Ref. streams = #{} :: #{cow_http2:streamid() => #stream{}}, - stream_refs = #{} :: #{reference() => cow_http2:streamid()} + stream_refs = #{} :: #{reference() => cow_http2:streamid()}, + + %% Number of pings that have been sent but not yet acknowledged. + %% Used to determine whether the connection should be closed when + %% the keepalive_tolerance option is set. + pings_unack = 0 :: non_neg_integer() }). check_options(Opts) -> @@ -139,6 +144,8 @@ do_check_options([{keepalive, infinity}|Opts]) -> do_check_options(Opts); do_check_options([{keepalive, K}|Opts]) when is_integer(K), K > 0 -> do_check_options(Opts); +do_check_options([{keepalive_tolerance, K}|Opts]) when is_integer(K), K >= 0 -> + do_check_options(Opts); do_check_options([{notify_settings_changed, B}|Opts]) when is_boolean(B) -> do_check_options(Opts); do_check_options([Opt={Name, _}|Opts]) -> @@ -341,7 +348,8 @@ frame(State=#http2_state{http2_machine=HTTP2Machine0}, Frame, CookieStore, EvHan end. maybe_ack_or_notify(State=#http2_state{reply_to=ReplyTo, socket=Socket, - transport=Transport, opts=Opts, http2_machine=HTTP2Machine}, Frame) -> + transport=Transport, opts=Opts, http2_machine=HTTP2Machine, + pings_unack=PingsUnack}, Frame) -> case Frame of {settings, _} -> %% We notify remote settings changes only if the user requested it. @@ -361,6 +369,8 @@ maybe_ack_or_notify(State=#http2_state{reply_to=ReplyTo, socket=Socket, ok -> {state, State}; Error={error, _} -> Error end; + {ping_ack, _Opaque} -> + {state, State#http2_state{pings_unack=PingsUnack - 1}}; _ -> {state, State} end. @@ -908,10 +918,18 @@ close_stream(State, #stream{ref=StreamRef, reply_to=ReplyTo}, Reason) -> ReplyTo ! {gun_error, self(), stream_ref(State, StreamRef), Reason}, ok. -keepalive(#http2_state{socket=Socket, transport=Transport}, _, EvHandlerState) -> +keepalive(State=#http2_state{pings_unack=PingsUnack, opts=Opts}, _, EvHandlerState) + when PingsUnack >= map_get(keepalive_tolerance, Opts) -> + {connection_error(State, {connection_error, no_error, + 'The number of unacknowledged pings exceed the configured tolerance value.'}), + EvHandlerState}; +keepalive(State=#http2_state{socket=Socket, transport=Transport, pings_unack=PingsUnack}, + _, EvHandlerState) -> case Transport:send(Socket, cow_http2:ping(0)) of - ok -> {[], EvHandlerState}; - Error={error, _} -> {Error, EvHandlerState} + ok -> + {{state, State#http2_state{pings_unack=PingsUnack + 1}}, EvHandlerState}; + Error={error, _} -> + {Error, EvHandlerState} end. headers(State=#http2_state{socket=Socket, transport=Transport, opts=Opts, -- cgit v1.2.3