From 0fe54af72b81fdb198a377198d7f3a91f9ad6d7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Mon, 14 Aug 2017 14:57:59 +0200 Subject: Allow an infinity keepalive value This disables the keepalive mechanism entirely. --- doc/src/manual/gun.asciidoc | 4 ++-- src/gun.erl | 5 ++++- src/gun_http.erl | 2 ++ src/gun_http2.erl | 2 ++ test/gun_SUITE.erl | 14 ++++++++++++++ 5 files changed, 24 insertions(+), 3 deletions(-) diff --git a/doc/src/manual/gun.asciidoc b/doc/src/manual/gun.asciidoc index b690558..02d0b86 100644 --- a/doc/src/manual/gun.asciidoc +++ b/doc/src/manual/gun.asciidoc @@ -57,12 +57,12 @@ Configuration for the HTTP protocol. The following keys are defined: -keepalive => pos_integer():: +keepalive => timeout():: Time between pings in milliseconds. Since the HTTP protocol has no standardized way to ping the server, Gun will simply send an empty line when the connection is idle. Gun only makes a best effort here as servers usually have configurable limits to drop - idle connections. Defaults to 5000. + idle connections. Use `infinity` to disable. Defaults to 5000. transform_header_name => fun((LowercaseName :: binary()) -> TransformedName :: binary()) | undefined:: A function that will be applied to all header names before they are sent to the server. Gun assumes that all header names are in diff --git a/src/gun.erl b/src/gun.erl index 4857344..e9155b2 100644 --- a/src/gun.erl +++ b/src/gun.erl @@ -624,7 +624,10 @@ before_loop(State=#state{opts=Opts, protocol=Protocol}) -> end, ProtoOpts = maps:get(ProtoOptsKey, Opts, #{}), Keepalive = maps:get(keepalive, ProtoOpts, 5000), - KeepaliveRef = erlang:send_after(Keepalive, self(), keepalive), + KeepaliveRef = case Keepalive of + infinity -> undefined; + _ -> erlang:send_after(Keepalive, self(), keepalive) + end, loop(State#state{keepalive_ref=KeepaliveRef}). loop(State=#state{parent=Parent, owner=Owner, owner_ref=OwnerRef, host=Host, port=Port, opts=Opts, diff --git a/src/gun_http.erl b/src/gun_http.erl index 3766ca5..204ce4a 100644 --- a/src/gun_http.erl +++ b/src/gun_http.erl @@ -60,6 +60,8 @@ check_options(Opts) -> do_check_options([]) -> ok; +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([{version, V}|Opts]) when V =:= 'HTTP/1.1'; V =:= 'HTTP/1.0' -> diff --git a/src/gun_http2.erl b/src/gun_http2.erl index 4e108ae..38017b3 100644 --- a/src/gun_http2.erl +++ b/src/gun_http2.erl @@ -74,6 +74,8 @@ check_options(Opts) -> do_check_options([]) -> ok; +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([Opt={content_handlers, Handlers}|Opts]) -> diff --git a/test/gun_SUITE.erl b/test/gun_SUITE.erl index 842d40c..a7e2169 100644 --- a/test/gun_SUITE.erl +++ b/test/gun_SUITE.erl @@ -110,6 +110,20 @@ info(_) -> #{sock_ip := _, sock_port := _} = gun:info(Pid), ok. +keepalive_infinity(_) -> + doc("Ensure infinity for keepalive is accepted by all protocols."), + {ok, Pid} = gun:open("localhost", 12345, #{ + http_opts => #{keepalive => infinity}, + http2_opts => #{keepalive => infinity}, + retry => 0}), + Ref = monitor(process, Pid), + receive + {'DOWN', Ref, process, Pid, {{gone, _}, _}} -> + ok + after 5000 -> + error(timeout) + end. + reply_to(_) -> doc("The reply_to option allows using a separate process for requests."), do_reply_to(http), -- cgit v1.2.3