Skip to content

Commit ec372f5

Browse files
authored
Merge pull request #46 from balena/master
Adding options `connection_timeout`, `retry` and `retry_timeout`
2 parents 481cb44 + f8faa6b commit ec372f5

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/eetcd.erl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ open(Name, Hosts, Transport, TransportOpts) ->
3838
%% The pinned address is maintained until the client connection is closed.
3939
%% When the client receives an error, it randomly picks another normal endpoint.
4040
%%
41+
%% `{connect_timeout, Interval}' is the connection timeout. Defaults to one second (1000).
42+
%% `{retry, Attempts}' is the number of times it will try to reconnect on failure before giving up. Defaults to zero (disabled).
43+
%% `{retry_timeout, Interval}' is the time between retries in milliseconds.
44+
%%
4145
%% `{auto_sync_interval_ms, Interval}' sets the default `Interval' in milliseconds of auto-sync.
4246
%% Default is 0, which means no auto-sync. If enabled auto-sync, you can set `auto_sync_interval_ms'
4347
%% in application env to change the interval. If disabled, the `auto_sync_interval_ms' in application
@@ -50,7 +54,14 @@ open(Name, Hosts, Transport, TransportOpts) ->
5054
%% You can use `eetcd:info/0' to see the internal connection status.
5155
-spec open(name(),
5256
[string()],
53-
[{mode, connect_all|random} |{name, string()} | {password, string()}],
57+
[
58+
{mode, connect_all | random}
59+
| {name, string()}
60+
| {password, string()}
61+
| {retry, non_neg_integer()}
62+
| {retry_timeout, pos_integer()}
63+
| {connect_timeout, timeout()}
64+
],
5465
tcp | tls | ssl,
5566
[gen_tcp:connect_option()] | [ssl:connect_option()]) ->
5667
{ok, pid()} | {error, any()}.

src/eetcd_conn.erl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,10 @@ flush_token(Gun, Headers) ->
9292
init({Name, Hosts, Options, Transport, TransportOpts}) ->
9393
erlang:process_flag(trap_exit, true),
9494
GunOpts = #{protocols => [http2],
95+
connect_timeout => proplists:get_value(connect_timeout, Options, 1000),
9596
http2_opts => #{keepalive => 45000},
96-
retry => 0,
97+
retry => proplists:get_value(retry, Options, 0),
98+
retry_timeout => proplists:get_value(retry_timeout, Options, 5000),
9799
transport => Transport,
98100
transport_opts => TransportOpts
99101
},
@@ -228,7 +230,11 @@ fold_connect([Host | Hosts], Name, GunOpts, Auth, Ok, Fail) ->
228230

229231
connect(Name, {IP, Port}, GunOpts, Auth) ->
230232
{ok, Gun} = gun:open(IP, Port, GunOpts),
231-
case gun:await_up(Gun, 1000) of
233+
Retries = maps:get(retry, GunOpts),
234+
ConnectTimeout = maps:get(connect_timeout, GunOpts),
235+
RetryTimeout = maps:get(retry_timeout, GunOpts),
236+
AwaitTime = (Retries + 1) * ConnectTimeout + Retries * RetryTimeout,
237+
case gun:await_up(Gun, AwaitTime) of
232238
{ok, http2} ->
233239
case check_health_remote(Gun) of
234240
ok ->
@@ -590,4 +596,3 @@ put_in_authenticate(Data, Options) ->
590596
shuffle(List) ->
591597
Disorders = [begin {rand:uniform(), K} end||K <-List],
592598
[begin K end||{_, K} <- lists:keysort(1, Disorders)].
593-

0 commit comments

Comments
 (0)