Skip to content

Commit

Permalink
handle a socket going down & add default timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
andymck committed May 24, 2022
1 parent 8a6faf3 commit 920a31c
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/http2_client.erl
Expand Up @@ -187,6 +187,8 @@
-define(WINDOW_UPDATE, 16#8).
-define(CONTINUATION, 16#9).

-define(CONNECT_TIMEOUT, 30000).

-type stream_id() :: integer().
-type header() :: [{Name::binary, Value::binary()}].
-type send_option() :: {end_stream, boolean()}.
Expand Down Expand Up @@ -252,7 +254,8 @@
continuation := continuation_data() | undefined,
ping_sent := [{binary(), %% the opaque data that was sent
integer()}], %% the time when it was sent
stream_count := integer()}.
stream_count := integer(),
socket_mon := port() }.

-export_type([connection_option/0,
stream_option/0]).
Expand Down Expand Up @@ -369,9 +372,10 @@ init({Transport0, Host, Port, Options, Owner}) ->
{[], Options}
end,
ConnectResult = Transport:connect(Host, Port,
TransportOptions ++ default_opts(Transport)),
TransportOptions ++ default_opts(Transport)), ?CONNECT_TIMEOUT,
case ConnectResult of
{ok, Socket} ->
Ref = monitor(port, Socket),
new_connection(
#{socket => Socket,
owner => Owner,
Expand All @@ -393,7 +397,8 @@ init({Transport0, Host, Port, Options, Owner}) ->
%% set to 0 if no streams were processed.
ping_sent => [],
stream_count => 0,
timeout => 3000 %% milliseconds
timeout => 3000, %% milliseconds
socket_mon => Ref
}, H2Opts);
{error, Error} ->
{stop, Error}
Expand Down Expand Up @@ -510,6 +515,8 @@ handle_info({ping_timeout, Opaque}, #{ping_sent := Pings} = C) ->
false ->
{noreply, C}
end;
handle_info({'DOWN', MRef, port, Socket, _Reason}, #{socket_mon := MRef, socket := Socket} = C) ->
{stop, socket_down, C};
handle_info(_Other, C) ->
%% io:format("got unexpected info: ~p~n", [_Other]),
{noreply, C}.
Expand Down

0 comments on commit 920a31c

Please sign in to comment.