Browse Source

move exponential backoff to herd library, add deps from herd

Yuriy Zhloba 9 years ago
parent
commit
edf88d4e64
3 changed files with 4 additions and 14 deletions
  1. 2 1
      rebar.config
  2. 1 11
      src/epgsql_pool_utils.erl
  3. 1 2
      src/epgsql_pool_worker.erl

+ 2 - 1
rebar.config

@@ -7,5 +7,6 @@
 
 {deps, [
         {pooler, ".*", {git, "https://github.com/seth/pooler", "b6c522a67a1d067122705ef725535a8664dd8514"}},
-        {epgsql, ".*", {git, "https://github.com/epgsql/epgsql", {tag, "3.1.0"}}}
+        {epgsql, ".*", {git, "https://github.com/epgsql/epgsql", {tag, "3.1.0"}}},
+        {herd, ".*", {git, "https://github.com/wgnet/herd", {tag, "1.2"}}}
        ]}.

+ 1 - 11
src/epgsql_pool_utils.erl

@@ -40,22 +40,12 @@ close_connection(#epgsql_connection{sock = Sock} = Connection) ->
 reconnect(#epgsql_connection{reconnect_attempt = Attempt} = Connection) ->
     {ok, MaxTimeout} = application:get_env(epgsql_pool, max_reconnect_timeout),
     {ok, MinTimeout} = application:get_env(epgsql_pool, min_reconnect_timeout),
-    Timeout = exponential_backoff(Attempt, 10, MinTimeout, MaxTimeout),
+    Timeout = herd_reconnect:exp_backoff(Attempt, MinTimeout, MaxTimeout),
     error_logger:warning_msg("epgsql_pool reconnect after ~p attempt ~p", [Timeout, Attempt]),
     erlang:send_after(Timeout, self(), open_connection),
     Connection#epgsql_connection{reconnect_attempt = Attempt + 1}.
 
 
--spec exponential_backoff(integer(), integer(), integer(), integer()) -> integer().
-exponential_backoff(Attempt, MaxAttempt, _BaseTimeout, MaxTimeout) when Attempt >= MaxAttempt ->
-    Half = MaxTimeout div 2,
-    Half + random:uniform(Half);
-exponential_backoff(Attempt, _MaxAttempt, BaseTimeout, MaxTimeout) ->
-    Timeout = min(erlang:round(math:pow(2, Attempt) * BaseTimeout), MaxTimeout),
-    Half = Timeout div 2,
-    Half + random:uniform(Half).
-
-
 -spec pool_name_to_atom(epgsql_pool:pool_name()) -> atom().
 pool_name_to_atom(PoolName) when is_binary(PoolName) ->
     pool_name_to_atom(erlang:binary_to_atom(PoolName, utf8));

+ 1 - 2
src/epgsql_pool_worker.erl

@@ -34,8 +34,7 @@ stop(Pid) ->
 -spec init(gs_args()) -> gs_init_reply().
 init(PoolName) ->
     process_flag(trap_exit, true),
-    <<A:32, B:32, C:32>> = crypto:rand_bytes(12),
-    random:seed({A,B,C}),
+    herd_rand:init_crypto(),
     self() ! open_connection,
     {ok, #state{pool_name = PoolName,
                 send_keep_alive_timer = make_ref(), % no need to check for undefined in cancel_timer