Skip to content

Commit b8cbc2f

Browse files
Retry Auth on network error (#1504)
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]>
1 parent 37a0669 commit b8cbc2f

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

olp-cpp-sdk-authentication/src/AuthenticationClientImpl.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ constexpr auto kErrorWrongTimestamp = 401204;
101101
constexpr auto kLogTag = "AuthenticationClient";
102102
const auto kMaxTime = std::numeric_limits<time_t>::max();
103103

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

310310
const auto request_body = GenerateClientBody(properties);
311311

312-
SignInResult response;
312+
SignInClientResponse response;
313313

314314
const auto& retry_settings = settings_.retry_settings;
315315

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

325325
const auto status = auth_response.status;
326326
if (status < 0) {
327-
return GetSignInResponse<SignInResult>(auth_response, context,
328-
credentials.GetKey());
327+
response = GetSignInResponse<SignInResult>(auth_response, context,
328+
credentials.GetKey());
329+
} else {
330+
response = ParseAuthResponse(status, auth_response.response);
329331
}
330332

331-
response = ParseAuthResponse(status, auth_response.response);
332-
333333
if (retry_settings.retry_condition(auth_response)) {
334334
RetryDelay(retry_settings, retry);
335335
continue;
336336
}
337337

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

348348
if (status == http::HttpStatusCode::OK) {
349-
StoreInCache(credentials.GetKey(), response);
349+
StoreInCache(credentials.GetKey(), response.GetResult());
350350
}
351351

352352
break;

tests/integration/olp-cpp-sdk-authentication/AuthenticationClientTest.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -457,10 +457,11 @@ TEST_F(AuthenticationClientTest, SignInClientData) {
457457
kResponseValidJson))
458458
.WillOnce(testing::WithArg<2>([&](http::Network::Callback callback) {
459459
http::RequestId request_id(6);
460-
callback(http::NetworkResponse()
461-
.WithRequestId(request_id)
462-
.WithStatus(-1)
463-
.WithError(""));
460+
callback(
461+
http::NetworkResponse()
462+
.WithRequestId(request_id)
463+
.WithStatus(static_cast<int>(http::ErrorCode::UNKNOWN_ERROR))
464+
.WithError(""));
464465

465466
return http::SendOutcome(request_id);
466467
}));
@@ -2096,6 +2097,7 @@ TEST_F(AuthenticationClientTest, Timeout) {
20962097
settings.token_endpoint_url = kTokenEndpointUrl;
20972098
settings.task_scheduler = task_scheduler_;
20982099
settings.use_system_time = true;
2100+
settings.retry_settings.max_attempts = 1;
20992101
settings.retry_settings.timeout = kMinTimeout;
21002102

21012103
const auth::AuthenticationCredentials credentials(key_, secret_);

0 commit comments

Comments
 (0)