Skip to content

Commit

Permalink
Improve resilience of looking at Retry-After
Browse files Browse the repository at this point in the history
  • Loading branch information
whatyouhide committed Apr 21, 2024
1 parent cf93d22 commit ab7fbb9
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions lib/sentry/transport.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ defmodule Sentry.Transport do
Process.sleep(delay_ms)
post_envelope_with_retries(client, endpoint, headers, payload, tl(retries_left))

{:retry_after, _delay_ms} ->
{:error, "Sentry server asked us to retry too many times"}

{:error, _reason} when retries_left != [] ->
[sleep_interval | retries_left] = retries_left
Process.sleep(sleep_interval)
Expand All @@ -56,23 +59,27 @@ defmodule Sentry.Transport do
{:ok, json} <- Config.json_library().decode(body) do
{:ok, Map.get(json, "id")}
else
{:ok, 429, headers, _body} ->
delay_ms =
with timeout when is_binary(timeout) <-
:proplists.get_value("Retry-After", headers, nil),
{delay_s, ""} <- Integer.parse(timeout) do
delay_s * 1000
else
_ ->
# https://develop.sentry.dev/sdk/rate-limiting/#stage-1-parse-response-headers
60_000
end

{:retry_after, delay_ms}

{:ok, status, headers, _body} ->
error_header =
:proplists.get_value("X-Sentry-Error", headers, nil) ||
:proplists.get_value("x-sentry-error", headers, nil) || ""

{:error, "Received #{status} from Sentry server: #{error_header}"}

{:ok, 429, headers, _body} ->
with timeout when is_binary(timeout) <- :proplists.get_value("Retry-After", headers, nil),
{delay_ms, ""} <- Integer.parse(timeout) do
{:retry_after, delay_ms * 1000}
else
_ ->
# https://develop.sentry.dev/sdk/rate-limiting/#stage-1-parse-response-headers
{:retry_after, 60_000}
end

{:error, reason} ->
{:error, reason}
end
Expand Down

0 comments on commit ab7fbb9

Please sign in to comment.