Repository navigation
[BUG] Keep the curl retry deadline stable within an attempt #4452
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e6aff05
b699ff2
146f69a
62494c1
e1d614e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -631,11 +631,16 @@ bool HttpOperation::IsRetryable() | |
|
|
||
| std::chrono::system_clock::time_point HttpOperation::NextRetryTime() | ||
| { | ||
| if (retry_after_time_point_ != std::chrono::system_clock::time_point{}) | ||
| if (next_retry_time_point_ != std::chrono::system_clock::time_point{}) | ||
| { | ||
| return retry_after_time_point_; | ||
| return next_retry_time_point_; | ||
| } | ||
|
|
||
| return CalculateNextRetryTime(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this fallback is the behaviour #4403 describes, recomputing a fresh jittered value on every call. i think it is unreachable as written. would you rather drop it so a missing deadline surfaces as a visible bug, or keep it with an assert naming the invariant? see the summary for why the invariant is worth naming. |
||
| } | ||
|
|
||
| std::chrono::system_clock::time_point HttpOperation::CalculateNextRetryTime() | ||
| { | ||
| // One engine per thread. Every HttpClient drives its own background thread, and drawing from | ||
| // the engine advances its state, so a shared one is written by all of them at once. | ||
| static thread_local std::mt19937 gen{std::random_device{}()}; | ||
|
|
@@ -1565,9 +1570,9 @@ void HttpOperation::Abort() | |
| bool HttpOperation::PerformCurlMessage(CURLcode code) | ||
| { | ||
| ++retry_attempts_; | ||
| last_attempt_time_ = std::chrono::system_clock::now(); | ||
| last_curl_result_ = code; | ||
| retry_after_time_point_ = std::chrono::system_clock::time_point{}; | ||
| last_attempt_time_ = std::chrono::system_clock::now(); | ||
| last_curl_result_ = code; | ||
| next_retry_time_point_ = std::chrono::system_clock::time_point{}; | ||
|
|
||
| if (code != CURLE_OK) | ||
| { | ||
|
|
@@ -1598,34 +1603,15 @@ bool HttpOperation::PerformCurlMessage(CURLcode code) | |
| curl_easy_getinfo(curl_resource_.easy_handle, CURLINFO_RESPONSE_CODE, &response_code_); | ||
| } | ||
|
|
||
| // Transform state | ||
| if (GetSessionState() == opentelemetry::ext::http::client::SessionState::Connecting) | ||
| { | ||
| DispatchEvent(opentelemetry::ext::http::client::SessionState::Connected); | ||
| } | ||
|
|
||
| if (GetSessionState() == opentelemetry::ext::http::client::SessionState::Connected) | ||
| { | ||
| DispatchEvent(opentelemetry::ext::http::client::SessionState::Sending); | ||
| } | ||
|
|
||
| if (GetSessionState() == opentelemetry::ext::http::client::SessionState::Sending) | ||
| { | ||
| DispatchEvent(opentelemetry::ext::http::client::SessionState::Response); | ||
| } | ||
|
|
||
| // A server-driven Retry-After may request a retry far beyond the retry | ||
| // policy's max_backoff (e.g. a malicious or misbehaving server returning | ||
| // "Retry-After: 2 years"). Such a session would linger in the pending retry | ||
| // list and block the FIFO drain in doRetrySessions(), so it must be closed | ||
| // rather than honored. Cap the requested delay at max_backoff; if the | ||
| // server-driven retry time still exceeds the maximum expected retry window, | ||
| // the session is closed below and not retried. | ||
| // Establish the deadline before dispatching Response. Event handlers run synchronously and may | ||
| // inspect it from the callback, so calculating it afterwards could expose a different jittered | ||
| // value from the one retained for scheduling this attempt. | ||
| const bool is_retryable = IsRetryable(); | ||
| bool retry_after_exceeds_max_delay = false; | ||
|
|
||
| if (is_retryable) | ||
| { | ||
| bool has_valid_retry_after = false; | ||
| nostd::string_view retry_after; | ||
| if (FindRetryAfterValue(response_headers_, retry_after)) | ||
| { | ||
|
|
@@ -1636,23 +1622,55 @@ bool HttpOperation::PerformCurlMessage(CURLcode code) | |
|
|
||
| if (parsed_delay || parsed_date) | ||
| { | ||
| has_valid_retry_after = true; | ||
| // Reuse the attempt timestamp instead of calling now() again, so the | ||
| // retry-after delay is measured from the attempt that produced this | ||
| // response and we avoid an extra system_clock syscall on the hot path. | ||
| retry_after_time_point_ = parsed_delay | ||
| ? (last_attempt_time_ + delay) | ||
| : ((date > last_attempt_time_) ? date : last_attempt_time_); | ||
| next_retry_time_point_ = parsed_delay | ||
| ? (last_attempt_time_ + delay) | ||
| : ((date > last_attempt_time_) ? date : last_attempt_time_); | ||
|
|
||
| const auto max_retry_time = | ||
| last_attempt_time_ + | ||
| std::chrono::duration_cast<std::chrono::milliseconds>(retry_policy_.max_backoff); | ||
| if (retry_after_time_point_ > max_retry_time) | ||
| if (next_retry_time_point_ > max_retry_time) | ||
| { | ||
| retry_after_exceeds_max_delay = true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (!has_valid_retry_after) | ||
| { | ||
| next_retry_time_point_ = CalculateNextRetryTime(); | ||
| } | ||
| } | ||
|
|
||
| // Transform state | ||
| if (GetSessionState() == opentelemetry::ext::http::client::SessionState::Connecting) | ||
| { | ||
| DispatchEvent(opentelemetry::ext::http::client::SessionState::Connected); | ||
| } | ||
|
|
||
| if (GetSessionState() == opentelemetry::ext::http::client::SessionState::Connected) | ||
| { | ||
| DispatchEvent(opentelemetry::ext::http::client::SessionState::Sending); | ||
| } | ||
|
|
||
| if (GetSessionState() == opentelemetry::ext::http::client::SessionState::Sending) | ||
| { | ||
| DispatchEvent(opentelemetry::ext::http::client::SessionState::Response); | ||
| } | ||
|
|
||
| // A server-driven Retry-After may request a retry far beyond the retry | ||
| // policy's max_backoff (e.g. a malicious or misbehaving server returning | ||
| // "Retry-After: 2 years"). Such a session would linger in the pending retry | ||
| // list and block the FIFO drain in doRetrySessions(), so it must be closed | ||
| // rather than honored. Cap the requested delay at max_backoff; if the | ||
| // server-driven retry time still exceeds the maximum expected retry window, | ||
| // the session is closed below and not retried. | ||
| if (is_retryable) | ||
| { | ||
| if (!retry_after_exceeds_max_delay) | ||
| { | ||
| // Clear any response data received in previous attempt | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.