Skip to content

Commit

Permalink
Merge pull request #46 from balena/master
Browse files Browse the repository at this point in the history
Adding options `connection_timeout`, `retry` and `retry_timeout`
  • Loading branch information
zhongwencool authored Apr 9, 2022
2 parents 481cb44 + f8faa6b commit ec372f5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
13 changes: 12 additions & 1 deletion src/eetcd.erl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ open(Name, Hosts, Transport, TransportOpts) ->
%% The pinned address is maintained until the client connection is closed.
%% When the client receives an error, it randomly picks another normal endpoint.
%%
%% `{connect_timeout, Interval}' is the connection timeout. Defaults to one second (1000).
%% `{retry, Attempts}' is the number of times it will try to reconnect on failure before giving up. Defaults to zero (disabled).
%% `{retry_timeout, Interval}' is the time between retries in milliseconds.
%%
%% `{auto_sync_interval_ms, Interval}' sets the default `Interval' in milliseconds of auto-sync.
%% Default is 0, which means no auto-sync. If enabled auto-sync, you can set `auto_sync_interval_ms'
%% in application env to change the interval. If disabled, the `auto_sync_interval_ms' in application
Expand All @@ -50,7 +54,14 @@ open(Name, Hosts, Transport, TransportOpts) ->
%% You can use `eetcd:info/0' to see the internal connection status.
-spec open(name(),
[string()],
[{mode, connect_all|random} |{name, string()} | {password, string()}],
[
{mode, connect_all | random}
| {name, string()}
| {password, string()}
| {retry, non_neg_integer()}
| {retry_timeout, pos_integer()}
| {connect_timeout, timeout()}
],
tcp | tls | ssl,
[gen_tcp:connect_option()] | [ssl:connect_option()]) ->
{ok, pid()} | {error, any()}.
Expand Down
11 changes: 8 additions & 3 deletions src/eetcd_conn.erl
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ flush_token(Gun, Headers) ->
init({Name, Hosts, Options, Transport, TransportOpts}) ->
erlang:process_flag(trap_exit, true),
GunOpts = #{protocols => [http2],
connect_timeout => proplists:get_value(connect_timeout, Options, 1000),
http2_opts => #{keepalive => 45000},
retry => 0,
retry => proplists:get_value(retry, Options, 0),
retry_timeout => proplists:get_value(retry_timeout, Options, 5000),
transport => Transport,
transport_opts => TransportOpts
},
Expand Down Expand Up @@ -228,7 +230,11 @@ fold_connect([Host | Hosts], Name, GunOpts, Auth, Ok, Fail) ->

connect(Name, {IP, Port}, GunOpts, Auth) ->
{ok, Gun} = gun:open(IP, Port, GunOpts),
case gun:await_up(Gun, 1000) of
Retries = maps:get(retry, GunOpts),
ConnectTimeout = maps:get(connect_timeout, GunOpts),
RetryTimeout = maps:get(retry_timeout, GunOpts),
AwaitTime = (Retries + 1) * ConnectTimeout + Retries * RetryTimeout,
case gun:await_up(Gun, AwaitTime) of
{ok, http2} ->
case check_health_remote(Gun) of
ok ->
Expand Down Expand Up @@ -590,4 +596,3 @@ put_in_authenticate(Data, Options) ->
shuffle(List) ->
Disorders = [begin {rand:uniform(), K} end||K <-List],
[begin K end||{_, K} <- lists:keysort(1, Disorders)].

0 comments on commit ec372f5

Please sign in to comment.