From cec3a35b8de7bb5355ab1dc838d468e46a8a8bf6 Mon Sep 17 00:00:00 2001 From: Johannes Holmberg Date: Thu, 4 Nov 2021 13:40:42 +0100 Subject: [PATCH] Add keepalive function that resets the inactivity timer Can be used in a streaming loop when we expect more data to arrive and want to prevent an inactivity timeout. --- src/cowboy_http.erl | 2 ++ src/cowboy_req.erl | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/src/cowboy_http.erl b/src/cowboy_http.erl index c9bceed8..ae89621e 100644 --- a/src/cowboy_http.erl +++ b/src/cowboy_http.erl @@ -255,6 +255,8 @@ loop(State=#state{parent=Parent, socket=Socket, transport=Transport, opts=Opts, %% Messages pertaining to a stream. {{Pid, StreamID}, Msg} when Pid =:= self() -> loop(info(State, StreamID, Msg)); + {{Pid, _StreamID}, keepalive} when Pid =:= self() -> + loop(State); %% Exit signal from children. Msg = {'EXIT', Pid, _} -> loop(down(State, Pid, Msg)); diff --git a/src/cowboy_req.erl b/src/cowboy_req.erl index 90c5a3a0..1598bc61 100644 --- a/src/cowboy_req.erl +++ b/src/cowboy_req.erl @@ -82,6 +82,7 @@ -export([stream_reply/3]). %% @todo stream_body/2 (nofin) -export([stream_body/3]). +-export([stream_keepalive/1]). %% @todo stream_events/2 (nofin) -export([stream_events/3]). -export([stream_trailers/2]). @@ -889,6 +890,10 @@ stream_body(Msg, Req=#{pid := Pid}) -> cast(Msg, Req), receive {data_ack, Pid} -> ok end. +-spec stream_keepalive(req()) -> ok. +stream_keepalive(Req) -> + cast(keepalive, Req). + -spec stream_events(cow_sse:event() | [cow_sse:event()], fin | nofin, req()) -> ok. stream_events(Event, IsFin, Req) when is_map(Event) -> stream_events([Event], IsFin, Req);