@@ -267,6 +267,26 @@ class RetryEventHandler : public CustomEventHandler
267267 }
268268};
269269
270+ #ifdef ENABLE_OTLP_RETRY_PREVIEW
271+ class RetryDeadlineEventHandler : public RetryEventHandler
272+ {
273+ public:
274+ void OnEvent (http_client::SessionState state, nostd::string_view reason) noexcept override
275+ {
276+ CustomEventHandler::OnEvent (state, reason);
277+ if (state == http_client::SessionState::Response && operation_ != nullptr )
278+ {
279+ ++response_event_count_;
280+ retry_time_during_response_ = operation_->NextRetryTime ();
281+ }
282+ }
283+
284+ curl::HttpOperation *operation_{nullptr };
285+ int response_event_count_{0 };
286+ std::chrono::system_clock::time_point retry_time_during_response_{};
287+ };
288+ #endif // ENABLE_OTLP_RETRY_PREVIEW
289+
270290class BasicCurlHttpTests : public ::testing::Test, public HTTP_SERVER_NS ::HttpRequestCallback
271291{
272292protected:
@@ -302,6 +322,7 @@ class BasicCurlHttpTests : public ::testing::Test, public HTTP_SERVER_NS::HttpRe
302322 server_.addHandler (" /post/" , *this );
303323 server_.addHandler (" /retry/" , *this );
304324 server_.addHandler (" /retry-after/" , *this );
325+ server_.addHandler (" /retry-after-invalid/" , *this );
305326 server_.addHandler (" /close/" , *this );
306327 server_.start ();
307328 is_running_ = true ;
@@ -335,12 +356,21 @@ class BasicCurlHttpTests : public ::testing::Test, public HTTP_SERVER_NS::HttpRe
335356 response.body = " {'k1':'v1', 'k2':'v2', 'k3':'v3'}" ;
336357 response_status = 200 ;
337358 }
338- else if (request.uri == " /retry/" )
359+ else if (request.uri == " /retry/" || request.uri == " /retry-after/" ||
360+ request.uri == " /retry-after-invalid/" )
339361 {
340362 std::unique_lock<std::mutex> lk1 (mtx_requests);
341363 received_requests_.push_back (request);
342364 response.headers [" Content-Type" ] = " text/plain" ;
343- response_status = 429 ;
365+ if (request.uri == " /retry-after/" )
366+ {
367+ response.headers [" Retry-After" ] = " 2" ;
368+ }
369+ else if (request.uri == " /retry-after-invalid/" )
370+ {
371+ response.headers [" Retry-After" ] = " invalid" ;
372+ }
373+ response_status = 429 ;
344374 }
345375 else if (request.uri == " /retry-after/" )
346376 {
@@ -693,6 +723,89 @@ TEST_F(BasicCurlHttpTests, ExponentialBackoffRetry)
693723 ASSERT_FALSE (operation.IsRetryable ());
694724}
695725
726+ TEST_F (BasicCurlHttpTests, RetryDeadlineIsStableWithinAttempt)
727+ {
728+ RetryEventHandler handler;
729+ http_client::HttpSslOptions no_ssl;
730+ http_client::Body body;
731+ http_client::Headers headers;
732+ http_client::Compression compression = http_client::Compression::kNone ;
733+ http_client::RetryPolicy retry_policy = {4 , std::chrono::duration<float >{1 .0f },
734+ std::chrono::duration<float >{5 .0f }, 2 .0f };
735+
736+ curl::HttpOperation operation (http_client::Method::Post, " http://127.0.0.1:19000/retry/" , no_ssl,
737+ &handler, headers, body, compression, false ,
738+ curl::kDefaultHttpConnTimeout , false , false , retry_policy);
739+
740+ ASSERT_EQ (CURLE_OK , operation.Send ());
741+ ASSERT_TRUE (operation.IsRetryable ());
742+
743+ const auto retry_time = operation.NextRetryTime ();
744+ for (int i = 0 ; i < 8 ; ++i)
745+ {
746+ EXPECT_EQ (retry_time, operation.NextRetryTime ());
747+ }
748+ }
749+
750+ TEST_F (BasicCurlHttpTests, RetryDeadlineIsReadyDuringResponseEvent)
751+ {
752+ RetryDeadlineEventHandler handler;
753+ http_client::HttpSslOptions no_ssl;
754+ http_client::Body body;
755+ http_client::Headers headers;
756+ http_client::Compression compression = http_client::Compression::kNone ;
757+ http_client::RetryPolicy retry_policy = {4 , std::chrono::duration<float >{1 .0f },
758+ std::chrono::duration<float >{5 .0f }, 2 .0f };
759+
760+ curl::HttpOperation operation (http_client::Method::Post, " http://127.0.0.1:19000/retry-after/" ,
761+ no_ssl, &handler, headers, body, compression, false ,
762+ curl::kDefaultHttpConnTimeout , false , false , retry_policy);
763+ handler.operation_ = &operation;
764+
765+ const auto before_send = std::chrono::system_clock::now ();
766+ ASSERT_EQ (CURLE_OK , operation.Send ());
767+ const auto after_send = std::chrono::system_clock::now ();
768+
769+ ASSERT_TRUE (operation.IsRetryable ());
770+ ASSERT_EQ (1 , handler.response_event_count_ );
771+ EXPECT_EQ (handler.retry_time_during_response_ , operation.NextRetryTime ());
772+ EXPECT_GE (handler.retry_time_during_response_ .time_since_epoch ().count (),
773+ (before_send + std::chrono::seconds{2 }).time_since_epoch ().count ());
774+ EXPECT_LE (handler.retry_time_during_response_ .time_since_epoch ().count (),
775+ (after_send + std::chrono::seconds{2 }).time_since_epoch ().count ());
776+ }
777+
778+ TEST_F (BasicCurlHttpTests, InvalidRetryAfterUsesStableBackoff)
779+ {
780+ RetryDeadlineEventHandler handler;
781+ http_client::HttpSslOptions no_ssl;
782+ http_client::Body body;
783+ http_client::Headers headers;
784+ http_client::Compression compression = http_client::Compression::kNone ;
785+ http_client::RetryPolicy retry_policy = {4 , std::chrono::duration<float >{1 .0f },
786+ std::chrono::duration<float >{5 .0f }, 2 .0f };
787+
788+ curl::HttpOperation operation (
789+ http_client::Method::Post, " http://127.0.0.1:19000/retry-after-invalid/" , no_ssl, &handler,
790+ headers, body, compression, false , curl::kDefaultHttpConnTimeout , false , false , retry_policy);
791+ handler.operation_ = &operation;
792+
793+ const auto before_send = std::chrono::system_clock::now ();
794+ ASSERT_EQ (CURLE_OK , operation.Send ());
795+ const auto after_send = std::chrono::system_clock::now ();
796+
797+ ASSERT_TRUE (operation.IsRetryable ());
798+ ASSERT_EQ (1 , handler.response_event_count_ );
799+ for (int i = 0 ; i < 8 ; ++i)
800+ {
801+ EXPECT_EQ (handler.retry_time_during_response_ , operation.NextRetryTime ());
802+ }
803+ EXPECT_GE (handler.retry_time_during_response_ .time_since_epoch ().count (),
804+ (before_send + std::chrono::milliseconds{750 }).time_since_epoch ().count ());
805+ EXPECT_LE (handler.retry_time_during_response_ .time_since_epoch ().count (),
806+ (after_send + std::chrono::milliseconds{1250 }).time_since_epoch ().count ());
807+ }
808+
696809// A Retry-After beyond max_backoff closes the session. The IO loop used to queue it anyway, where
697810// it held back later retries and the background thread until the server's time.
698811TEST_F (BasicCurlHttpTests, RetryAfterBeyondMaxBackoffIsNotQueued)
0 commit comments