Skip to content

Commit

Permalink
Retry Auth on network error (#1504)
Browse files Browse the repository at this point in the history
When transient network error occurs,
eg timeout or network overload, use
retry settings in order to determine
if retry auth request, instead of
returning an error.

Relates-To: IOTSDK-24136

Signed-off-by: Mykhailo Diachenko <[email protected]>
  • Loading branch information
diachenko-mischa committed Apr 29, 2024
1 parent 37a0669 commit b8cbc2f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
16 changes: 8 additions & 8 deletions olp-cpp-sdk-authentication/src/AuthenticationClientImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ constexpr auto kErrorWrongTimestamp = 401204;
constexpr auto kLogTag = "AuthenticationClient";
const auto kMaxTime = std::numeric_limits<time_t>::max();

bool HasWrongTimestamp(SignInResult& result) {
bool HasWrongTimestamp(const SignInResult& result) {
const auto& error_response = result.GetErrorResponse();
const auto status = result.GetStatus();
return status == http::HttpStatusCode::UNAUTHORIZED &&
Expand Down Expand Up @@ -309,7 +309,7 @@ client::CancellationToken AuthenticationClientImpl::SignInClient(

const auto request_body = GenerateClientBody(properties);

SignInResult response;
SignInClientResponse response;

const auto& retry_settings = settings_.retry_settings;

Expand All @@ -324,20 +324,20 @@ client::CancellationToken AuthenticationClientImpl::SignInClient(

const auto status = auth_response.status;
if (status < 0) {
return GetSignInResponse<SignInResult>(auth_response, context,
credentials.GetKey());
response = GetSignInResponse<SignInResult>(auth_response, context,
credentials.GetKey());
} else {
response = ParseAuthResponse(status, auth_response.response);
}

response = ParseAuthResponse(status, auth_response.response);

if (retry_settings.retry_condition(auth_response)) {
RetryDelay(retry_settings, retry);
continue;
}

// In case we can't authorize with system time, retry with the server
// time from response headers (if available).
if (HasWrongTimestamp(response)) {
if (HasWrongTimestamp(response.GetResult())) {
auto server_time = GetTimestampFromHeaders(auth_response.headers);
if (server_time) {
timer = RequestTimer(*server_time);
Expand All @@ -346,7 +346,7 @@ client::CancellationToken AuthenticationClientImpl::SignInClient(
}

if (status == http::HttpStatusCode::OK) {
StoreInCache(credentials.GetKey(), response);
StoreInCache(credentials.GetKey(), response.GetResult());
}

break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -457,10 +457,11 @@ TEST_F(AuthenticationClientTest, SignInClientData) {
kResponseValidJson))
.WillOnce(testing::WithArg<2>([&](http::Network::Callback callback) {
http::RequestId request_id(6);
callback(http::NetworkResponse()
.WithRequestId(request_id)
.WithStatus(-1)
.WithError(""));
callback(
http::NetworkResponse()
.WithRequestId(request_id)
.WithStatus(static_cast<int>(http::ErrorCode::UNKNOWN_ERROR))
.WithError(""));

return http::SendOutcome(request_id);
}));
Expand Down Expand Up @@ -2096,6 +2097,7 @@ TEST_F(AuthenticationClientTest, Timeout) {
settings.token_endpoint_url = kTokenEndpointUrl;
settings.task_scheduler = task_scheduler_;
settings.use_system_time = true;
settings.retry_settings.max_attempts = 1;
settings.retry_settings.timeout = kMinTimeout;

const auth::AuthenticationCredentials credentials(key_, secret_);
Expand Down

0 comments on commit b8cbc2f

Please sign in to comment.