diff --git a/src/eetcd.erl b/src/eetcd.erl index 792866c..664365c 100644 --- a/src/eetcd.erl +++ b/src/eetcd.erl @@ -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 @@ -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()}. diff --git a/src/eetcd_conn.erl b/src/eetcd_conn.erl index 5b7a911..230c943 100644 --- a/src/eetcd_conn.erl +++ b/src/eetcd_conn.erl @@ -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 }, @@ -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 -> @@ -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)]. -