fix(http): back off from the host that answered, and read Unix resets
After a redirect the 429/503 came from the final host, but the backoff was recorded for the requested one. X-RateLimit-Reset is also accepted as a Unix timestamp or as seconds.
This commit is contained in:
parent
b29d9d0981
commit
4070b7be21
2 changed files with 65 additions and 2 deletions
|
|
@ -83,6 +83,23 @@ defmodule Pleroma.HTTP.BackoffTest do
|
|||
mock_response(200)
|
||||
assert {:ok, _} = Backoff.get("https://other.example.com/users/bob")
|
||||
end
|
||||
|
||||
test "backs off from the host that answered after a redirect" do
|
||||
test_pid = self()
|
||||
|
||||
Tesla.Mock.mock(fn
|
||||
%{url: "https://redirector.example.com/users/alice"} = env ->
|
||||
%{env | status: 302, headers: [{"location", @url}], body: ""}
|
||||
|
||||
%{url: @url} = env ->
|
||||
send(test_pid, :request)
|
||||
%{env | status: 429, headers: [], body: ""}
|
||||
end)
|
||||
|
||||
assert {:error, :ratelimit} = Backoff.get("https://redirector.example.com/users/alice")
|
||||
assert {:ok, true} = Cachex.get(@cache, @host)
|
||||
assert {:ok, nil} = Cachex.get(@cache, "redirector.example.com")
|
||||
end
|
||||
end
|
||||
|
||||
describe "backoff headers" do
|
||||
|
|
@ -121,6 +138,21 @@ defmodule Pleroma.HTTP.BackoffTest do
|
|||
assert_ttl_about(600)
|
||||
end
|
||||
|
||||
test "parses X-RateLimit-Reset as a Unix timestamp" do
|
||||
reset = DateTime.utc_now() |> DateTime.add(600) |> DateTime.to_unix() |> to_string()
|
||||
mock_response(429, [{"x-ratelimit-reset", reset}])
|
||||
|
||||
assert {:error, :ratelimit} = Backoff.get(@url)
|
||||
assert_ttl_about(600)
|
||||
end
|
||||
|
||||
test "parses X-RateLimit-Reset in seconds" do
|
||||
mock_response(429, [{"x-ratelimit-reset", "600"}])
|
||||
|
||||
assert {:error, :ratelimit} = Backoff.get(@url)
|
||||
assert_ttl_about(600)
|
||||
end
|
||||
|
||||
test "matches header names case-insensitively" do
|
||||
mock_response(429, [{"Retry-After", "600"}])
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue