From 6612610964cabadfcf408e4223a702555a3570cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Wed, 9 Jan 2019 18:12:51 +0100 Subject: Add function gun:stream_info/2 --- src/gun.erl | 12 +++++++++++- src/gun_http.erl | 16 ++++++++++++++++ src/gun_http2.erl | 13 +++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/gun.erl b/src/gun.erl index 75092f1..1fd7e37 100644 --- a/src/gun.erl +++ b/src/gun.erl @@ -78,8 +78,9 @@ %% Flushing gun messages. -export([flush/1]). -%% Cancelling a stream. +%% Streams. -export([cancel/2]). +-export([stream_info/2]). %% Websocket. -export([ws_upgrade/2]). @@ -643,6 +644,10 @@ flush_ref(StreamRef) -> cancel(ServerPid, StreamRef) -> gen_statem:cast(ServerPid, {cancel, self(), StreamRef}). +-spec stream_info(pid(), reference()) -> {ok, map() | undefined} | {error, not_connected}. +stream_info(ServerPid, StreamRef) -> + gen_statem:call(ServerPid, {stream_info, StreamRef}). + %% @todo Allow upgrading an HTTP/1.1 connection to HTTP/2. %% http2_upgrade @@ -725,6 +730,8 @@ not_connected(_, {retries, Retries}, {keep_state, State, {state_timeout, Timeout, {retries, Retries - 1}}} end; +not_connected({call, From}, {stream_info, _}, _) -> + {keep_state_and_data, {reply, From, {error, not_connected}}}; not_connected(Type, Event, State) -> handle_common(Type, Event, ?FUNCTION_NAME, State). @@ -843,6 +850,9 @@ connected(cast, {ws_send, ReplyTo, _}, _) -> "Connection needs to be upgraded to Websocket " "before the gun:ws_send/1 function can be used."}}, keep_state_and_data; +connected({call, From}, {stream_info, StreamRef}, + #state{protocol=Protocol, protocol_state=ProtoState}) -> + {keep_state_and_data, {reply, From, Protocol:stream_info(ProtoState, StreamRef)}}; connected(Type, Event, State) -> handle_common(Type, Event, ?FUNCTION_NAME, State). diff --git a/src/gun_http.erl b/src/gun_http.erl index 376f431..719307c 100644 --- a/src/gun_http.erl +++ b/src/gun_http.erl @@ -25,6 +25,7 @@ -export([data/5]). -export([connect/5]). -export([cancel/3]). +-export([stream_info/2]). -export([down/1]). -export([ws_upgrade/7]). @@ -473,6 +474,21 @@ cancel(State, StreamRef, ReplyTo) -> error_stream_not_found(State, StreamRef, ReplyTo) end. +stream_info(#http_state{streams=Streams}, StreamRef) -> + case lists:keyfind(StreamRef, #stream.ref, Streams) of + #stream{reply_to=ReplyTo, is_alive=IsAlive} -> + {ok, #{ + ref => StreamRef, + reply_to => ReplyTo, + state => case IsAlive of + true -> running; + false -> stopping + end + }}; + false -> + {ok, undefined} + end. + %% HTTP does not provide any way to figure out what streams are unprocessed. down(#http_state{streams=Streams}) -> KilledStreams = [case Ref of diff --git a/src/gun_http2.erl b/src/gun_http2.erl index 9159d78..8072a00 100644 --- a/src/gun_http2.erl +++ b/src/gun_http2.erl @@ -24,6 +24,7 @@ -export([request/9]). -export([data/5]). -export([cancel/3]). +-export([stream_info/2]). -export([down/1]). -record(stream, { @@ -371,6 +372,18 @@ cancel(State=#http2_state{socket=Socket, transport=Transport, error_stream_not_found(State, StreamRef, ReplyTo) end. +stream_info(State, StreamRef) -> + case get_stream_by_ref(State, StreamRef) of + #stream{reply_to=ReplyTo} -> + {ok, #{ + ref => StreamRef, + reply_to => ReplyTo, + state => running + }}; + false -> + {ok, undefined} + end. + %% @todo Add unprocessed streams when GOAWAY handling is done. down(#http2_state{streams=Streams}) -> KilledStreams = [Ref || #stream{ref=Ref} <- Streams], -- cgit v1.2.3