Backoff on HTTP requests when 429 is recieved #762
2 changed files with 4 additions and 7 deletions
|
@ -49,7 +49,7 @@ def get(url, headers \\ [], options \\ []) do
|
|||
timestamp = next_backoff_timestamp(env)
|
||||
ttl = Timex.diff(timestamp, DateTime.utc_now(), :seconds)
|
||||
# we will cache the host for 5 minutes
|
||||
@cachex.put(@backoff_cache, host, true, ttl)
|
||||
@cachex.put(@backoff_cache, host, true, ttl: ttl)
|
||||
{:error, :ratelimit}
|
||||
|
||||
_ ->
|
||||
|
|
|
@ -17,8 +17,7 @@ test "should return {:ok, env} when not rate limited" do
|
|||
test "should return {:error, env} when rate limited" do
|
||||
# Shove a value into the cache to simulate a rate limit
|
||||
Cachex.put(@backoff_cache, "akkoma.dev", true)
|
||||
assert {:error, env} = Backoff.get("https://akkoma.dev/api/v1/instance")
|
||||
assert env.status == 429
|
||||
assert {:error, :ratelimit} = Backoff.get("https://akkoma.dev/api/v1/instance")
|
||||
end
|
||||
|
||||
test "should insert a value into the cache when rate limited" do
|
||||
|
@ -27,8 +26,7 @@ test "should insert a value into the cache when rate limited" do
|
|||
{:ok, %Tesla.Env{status: 429, body: "Rate limited"}}
|
||||
end)
|
||||
|
||||
assert {:error, env} = Backoff.get("https://ratelimited.dev/api/v1/instance")
|
||||
assert env.status == 429
|
||||
assert {:error, :ratelimit} = Backoff.get("https://ratelimited.dev/api/v1/instance")
|
||||
assert {:ok, true} = Cachex.get(@backoff_cache, "ratelimited.dev")
|
||||
end
|
||||
|
||||
|
@ -46,8 +44,7 @@ test "should parse the value of x-ratelimit-reset, if present" do
|
|||
}}
|
||||
end)
|
||||
|
||||
assert {:error, env} = Backoff.get("https://ratelimited.dev/api/v1/instance")
|
||||
assert env.status == 429
|
||||
assert {:error, :ratelimit} = Backoff.get("https://ratelimited.dev/api/v1/instance")
|
||||
assert {:ok, true} = Cachex.get(@backoff_cache, "ratelimited.dev")
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue