Skip to content

Commit 25c48da

Browse files
authored
Add RetrySettings for authentication (#1251)
olp::authentication::AuthenticationSettings and olp::authentication::Settingsstructs are extended with RetrySettings. AuthentificationClientImpl now uses user defined retry settings instead of hardcoded values. Fixed double token request bug in TokenProvider. Relates-To: OLPEDGE-2634, OLPSUP-15460 Signed-off-by: Mykola Malik <[email protected]>
1 parent dd53a5d commit 25c48da

File tree

12 files changed

+466
-115
lines changed

12 files changed

+466
-115
lines changed

olp-cpp-sdk-authentication/include/olp/authentication/AuthenticationSettings.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2020 HERE Europe B.V.
2+
* Copyright (C) 2020-2021 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
2323
#include <string>
2424

2525
#include <olp/authentication/AuthenticationApi.h>
26+
#include <olp/core/client/RetrySettings.h>
2627
#include <olp/core/http/NetworkProxySettings.h>
2728
#include <boost/optional.hpp>
2829

@@ -89,6 +90,12 @@ struct AUTHENTICATION_API AuthenticationSettings {
8990
* authentication server.
9091
*/
9192
bool use_system_time{true};
93+
94+
/**
95+
* @brief A collection of settings that controls how failed requests should be
96+
* treated.
97+
*/
98+
client::RetrySettings retry_settings;
9299
};
93100

94101
} // namespace authentication

olp-cpp-sdk-authentication/include/olp/authentication/Settings.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <memory>
2424
#include <string>
2525

26+
#include <olp/core/client/RetrySettings.h>
2627
#include <olp/core/http/NetworkProxySettings.h>
2728

2829
#include "AuthenticationApi.h"
@@ -98,6 +99,12 @@ struct AUTHENTICATION_API Settings {
9899
* authentication server.
99100
*/
100101
bool use_system_time{true};
102+
103+
/**
104+
* @brief A collection of settings that controls how failed requests should be
105+
* treated.
106+
*/
107+
client::RetrySettings retry_settings;
101108
};
102109

103110
} // namespace authentication

olp-cpp-sdk-authentication/include/olp/authentication/TokenProvider.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2020 HERE Europe B.V.
2+
* Copyright (C) 2019-2021 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -119,9 +119,9 @@ class TokenProvider {
119119

120120
/// @copydoc TokenProvider::operator()()
121121
std::string operator()() const {
122-
return GetResponse().IsSuccessful()
123-
? GetResponse().GetResult().GetAccessToken()
124-
: "";
122+
auto response = GetResponse();
123+
return response.IsSuccessful() ? response.GetResult().GetAccessToken()
124+
: "";
125125
}
126126

127127
/// @copydoc TokenProvider::GetErrorResponse()

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

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2020 HERE Europe B.V.
2+
* Copyright (C) 2020-2021 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -99,19 +99,17 @@ constexpr auto kOperator = "operator";
9999
constexpr auto kErrorWrongTimestamp = 401204;
100100
constexpr auto kLogTag = "AuthenticationClient";
101101

102-
constexpr auto kDefaultRetryCount = 3;
103-
constexpr auto kDefaultRetryTime = std::chrono::milliseconds(200);
104-
105102
bool HasWrongTimestamp(olp::authentication::SignInResult& result) {
106103
const auto& error_response = result.GetErrorResponse();
107104
const auto status = result.GetStatus();
108105
return status == olp::http::HttpStatusCode::UNAUTHORIZED &&
109106
error_response.code == kErrorWrongTimestamp;
110107
}
111108

112-
void RetryDelay(size_t retry) {
113-
client::ExponentialBackdownStrategy retry_delay;
114-
std::this_thread::sleep_for(retry_delay(kDefaultRetryTime, retry));
109+
void RetryDelay(const client::RetrySettings& retry_settings, size_t retry) {
110+
std::this_thread::sleep_for(retry_settings.backdown_strategy(
111+
std::chrono::milliseconds(retry_settings.initial_backdown_period),
112+
retry));
115113
}
116114

117115
client::OlpClient::RequestBodyType GenerateAppleSignInBody(
@@ -284,7 +282,9 @@ client::CancellationToken AuthenticationClientImpl::SignInClient(
284282

285283
SignInResult response;
286284

287-
for (auto retry = 0; retry < kDefaultRetryCount; ++retry) {
285+
const auto& retry_settings = settings_.retry_settings;
286+
287+
for (auto retry = 0; retry < retry_settings.max_attempts; ++retry) {
288288
if (context.IsCancelled()) {
289289
return client::ApiError::Cancelled();
290290
}
@@ -310,8 +310,8 @@ client::CancellationToken AuthenticationClientImpl::SignInClient(
310310

311311
response = ParseAuthResponse(status, auth_response.response);
312312

313-
if (client::DefaultRetryCondition(auth_response)) {
314-
RetryDelay(retry);
313+
if (retry_settings.retry_condition(auth_response)) {
314+
RetryDelay(retry_settings, retry);
315315
continue;
316316
}
317317

@@ -492,7 +492,9 @@ client::CancellationToken AuthenticationClientImpl::HandleUserRequest(
492492

493493
SignInUserResult response;
494494

495-
for (auto retry = 0; retry < kDefaultRetryCount; ++retry) {
495+
const auto& retry_settings = settings_.retry_settings;
496+
497+
for (auto retry = 0; retry < retry_settings.max_attempts; ++retry) {
496498
if (context.IsCancelled()) {
497499
return client::ApiError::Cancelled();
498500
}
@@ -517,8 +519,8 @@ client::CancellationToken AuthenticationClientImpl::HandleUserRequest(
517519

518520
response = ParseUserAuthResponse(status, auth_response.response);
519521

520-
if (client::DefaultRetryCondition(auth_response)) {
521-
RetryDelay(retry);
522+
if (retry_settings.retry_condition(auth_response)) {
523+
RetryDelay(retry_settings, retry);
522524
continue;
523525
}
524526

@@ -560,10 +562,12 @@ client::CancellationToken AuthenticationClientImpl::SignUpHereUser(
560562
std::string url = settings_.token_endpoint_url;
561563
url.append(kUserEndpoint);
562564
http::NetworkRequest request(url);
563-
http::NetworkSettings network_settings;
564-
if (settings_.network_proxy_settings) {
565-
network_settings.WithProxySettings(settings_.network_proxy_settings.get());
566-
}
565+
auto network_settings =
566+
http::NetworkSettings()
567+
.WithTransferTimeout(settings_.retry_settings.timeout)
568+
.WithConnectionTimeout(settings_.retry_settings.timeout)
569+
.WithProxySettings(settings_.network_proxy_settings.get_value_or({}));
570+
567571
request.WithVerb(http::NetworkRequest::HttpVerb::POST);
568572

569573
auto auth_header = GenerateAuthorizationHeader(
@@ -637,10 +641,12 @@ client::CancellationToken AuthenticationClientImpl::SignOut(
637641
std::string url = settings_.token_endpoint_url;
638642
url.append(kSignoutEndpoint);
639643
http::NetworkRequest request(url);
640-
http::NetworkSettings network_settings;
641-
if (settings_.network_proxy_settings) {
642-
network_settings.WithProxySettings(settings_.network_proxy_settings.get());
643-
}
644+
auto network_settings =
645+
http::NetworkSettings()
646+
.WithTransferTimeout(settings_.retry_settings.timeout)
647+
.WithConnectionTimeout(settings_.retry_settings.timeout)
648+
.WithProxySettings(settings_.network_proxy_settings.get_value_or({}));
649+
644650
request.WithVerb(http::NetworkRequest::HttpVerb::POST);
645651
request.WithHeader(http::kAuthorizationHeader,
646652
GenerateBearerHeader(access_token));

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2020 HERE Europe B.V.
2+
* Copyright (C) 2020-2021 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -264,8 +264,7 @@ client::OlpClient CreateOlpClient(
264264
settings.network_request_handler = auth_settings.network_request_handler;
265265
settings.authentication_settings = authentication_settings;
266266
settings.proxy_settings = auth_settings.network_proxy_settings;
267-
settings.retry_settings.backdown_strategy =
268-
client::ExponentialBackdownStrategy();
267+
settings.retry_settings = auth_settings.retry_settings;
269268

270269
if (!retry) {
271270
settings.retry_settings.max_attempts = 0;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2020 HERE Europe B.V.
2+
* Copyright (C) 2019-2021 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -45,6 +45,7 @@ AuthenticationSettings ConvertSettings(Settings settings) {
4545
auth_settings.network_request_handler = settings.network_request_handler;
4646
auth_settings.token_endpoint_url = settings.token_endpoint_url;
4747
auth_settings.use_system_time = settings.use_system_time;
48+
auth_settings.retry_settings = settings.retry_settings;
4849
return auth_settings;
4950
}
5051
} // namespace

olp-cpp-sdk-core/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ set(OLP_SDK_CLIENT_HEADERS
8181
./include/olp/core/client/OlpClientSettings.h
8282
./include/olp/core/client/OlpClientSettingsFactory.h
8383
./include/olp/core/client/PendingRequests.h
84+
./include/olp/core/client/RetrySettings.h
8485
./include/olp/core/client/TaskContext.h
8586
)
8687

@@ -253,11 +254,11 @@ set(OLP_SDK_CLIENT_SOURCES
253254
./src/client/HRN.cpp
254255
./src/client/OlpClient.cpp
255256
./src/client/OlpClientFactory.cpp
256-
./src/client/OlpClientSettings.cpp
257257
./src/client/OlpClientSettingsFactory.cpp
258258
./src/client/PendingRequests.cpp
259259
./src/client/PendingUrlRequests.h
260260
./src/client/PendingUrlRequests.cpp
261+
./src/client/RetrySettings.cpp
261262
./src/client/Tokenizer.h
262263
)
263264

olp-cpp-sdk-core/include/olp/core/client/OlpClientSettings.h

Lines changed: 2 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2020 HERE Europe B.V.
2+
* Copyright (C) 2019-2021 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,11 +26,11 @@
2626

2727
#include <boost/optional.hpp>
2828

29-
#include <olp/core/client/BackdownStrategy.h>
3029
#include <olp/core/client/CancellationToken.h>
3130
#include <olp/core/client/DefaultLookupEndpointProvider.h>
3231
#include <olp/core/client/HRN.h>
3332
#include <olp/core/client/HttpResponse.h>
33+
#include <olp/core/client/RetrySettings.h>
3434
#include <olp/core/http/Network.h>
3535

3636
namespace olp {
@@ -61,11 +61,6 @@ using NetworkAsyncCallback = std::function<void(HttpResponse)>;
6161
*/
6262
using NetworkAsyncCancel = std::function<void()>;
6363

64-
/**
65-
* @brief The default retry condition that disables retries.
66-
*/
67-
CORE_API bool DefaultRetryCondition(const olp::client::HttpResponse& response);
68-
6964
/**
7065
* @brief A set of settings that manages the `TokenProviderCallback` and
7166
* `TokenProviderCancelCallback` functions.
@@ -146,63 +141,6 @@ struct CORE_API AuthenticationSettings {
146141
boost::optional<TokenProviderCancelCallback> cancel;
147142
};
148143

149-
/**
150-
* @brief A collection of settings that controls how failed requests should be
151-
* treated by the Data SDK.
152-
*
153-
* For example, it specifies whether the failed request should be retried, how
154-
* long Data SDK needs to wait for the next retry attempt, the number of maximum
155-
* retries, and so on.
156-
*
157-
* You can customize all of these settings. The settings are used internally by
158-
* the `OlpClient` class.
159-
*/
160-
struct CORE_API RetrySettings {
161-
/**
162-
* @brief Calculates the number of retry timeouts based on
163-
* the initial backdown duration and retries count.
164-
*/
165-
using BackdownStrategy = std::function<std::chrono::milliseconds(
166-
std::chrono::milliseconds, size_t)>;
167-
168-
/**
169-
* @brief Checks whether the retry is desired.
170-
*
171-
* @see `HttpResponse` for more details.
172-
*/
173-
using RetryCondition = std::function<bool(const HttpResponse&)>;
174-
175-
/**
176-
* @brief The number of attempts.
177-
*
178-
* The default value is 3.
179-
*/
180-
int max_attempts = 3;
181-
182-
/**
183-
* @brief The connection timeout limit (in seconds).
184-
*/
185-
int timeout = 60;
186-
187-
/**
188-
* @brief The period between the error and the first retry attempt (in
189-
* milliseconds).
190-
*/
191-
int initial_backdown_period = 200;
192-
193-
/**
194-
* @brief The backdown strategy.
195-
*
196-
* Defines the delay between retries on a failed request.
197-
*/
198-
BackdownStrategy backdown_strategy = ExponentialBackdownStrategy();
199-
200-
/**
201-
* @brief Evaluates responses to determine if the retry should be attempted.
202-
*/
203-
RetryCondition retry_condition = DefaultRetryCondition;
204-
};
205-
206144
/**
207145
* @brief Settings to provide URLs for API lookup requests.
208146
*/

0 commit comments

Comments
 (0)