Skip to content

Commit 11dcf1d

Browse files
adamkittelsonbenwilson512
authored andcommitted
implement exponential backoff with jitter according to the "full jitter" formula described at https://www.awsarchitectureblog.com/2015/03/backoff.html (#185)
1 parent 81fc12a commit 11dcf1d

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

lib/ex_aws/request.ex

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
defmodule ExAws.Request do
22
require Logger
33
@max_attempts 10
4+
@base_backoff_in_ms 10
5+
@max_backoff_in_ms 10_000
46

57
@moduledoc """
68
Makes requests to AWS.
@@ -84,9 +86,11 @@ defmodule ExAws.Request do
8486
{:attempt, attempt + 1}
8587
end
8688

87-
# TODO: make exponential
88-
# TODO: add jitter
8989
def backoff(attempt) do
90-
:timer.sleep(attempt * 1000)
90+
(@base_backoff_in_ms * :math.pow(2, attempt))
91+
|> min(@max_backoff_in_ms)
92+
|> trunc
93+
|> :rand.uniform
94+
|> :timer.sleep
9195
end
9296
end

0 commit comments

Comments
 (0)