From ab7fbb96c51598e816c407b56100c386c2c7aa1b Mon Sep 17 00:00:00 2001 From: Andrea Leopardi Date: Sun, 21 Apr 2024 13:26:30 +0200 Subject: [PATCH] Improve resilience of looking at Retry-After --- lib/sentry/transport.ex | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/lib/sentry/transport.ex b/lib/sentry/transport.ex index 541c6003..76ff2d04 100644 --- a/lib/sentry/transport.ex +++ b/lib/sentry/transport.ex @@ -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) @@ -56,6 +59,20 @@ 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) || @@ -63,16 +80,6 @@ defmodule Sentry.Transport do {: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