-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathOlpClientSettingsFactory.cpp
More file actions
127 lines (113 loc) · 4.44 KB
/
OlpClientSettingsFactory.cpp
File metadata and controls
127 lines (113 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* Copyright (C) 2019-2023 HERE Europe B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* License-Filename: LICENSE
*/
#include "olp/core/client/OlpClientSettingsFactory.h"
#include "olp/core/Config.h"
#include "olp/core/cache/CacheSettings.h"
#include "olp/core/cache/DefaultCache.h"
#include "olp/core/client/OlpClientSettings.h"
#include "olp/core/http/NetworkConstants.h"
#include "olp/core/http/NetworkInitializationSettings.h"
#include "olp/core/logging/Log.h"
#include "olp/core/porting/make_unique.h"
#include "olp/core/thread/ThreadPoolTaskScheduler.h"
namespace {
auto constexpr kLogTag = "OlpClientSettingsFactory";
}
namespace olp {
namespace client {
std::unique_ptr<thread::TaskScheduler>
OlpClientSettingsFactory::CreateDefaultTaskScheduler(
size_t thread_count, bool enable_cancellation_lane) {
return std::make_unique<thread::ThreadPoolTaskScheduler>(
thread_count, enable_cancellation_lane);
}
std::shared_ptr<http::Network>
OlpClientSettingsFactory::CreateDefaultNetworkRequestHandler(
size_t max_requests_count) {
http::NetworkInitializationSettings settings;
settings.max_requests_count = max_requests_count;
return CreateDefaultNetworkRequestHandler(std::move(settings));
}
std::shared_ptr<http::Network>
OlpClientSettingsFactory::CreateDefaultNetworkRequestHandler(
http::NetworkInitializationSettings settings) {
return http::CreateDefaultNetwork(std::move(settings));
}
std::unique_ptr<cache::KeyValueCache>
OlpClientSettingsFactory::CreateDefaultCache(cache::CacheSettings settings) {
#ifdef OLP_SDK_ENABLE_DEFAULT_CACHE
auto cache = std::make_unique<cache::DefaultCache>(settings);
auto error = cache->Open();
if (error == cache::DefaultCache::StorageOpenResult::OpenDiskPathFailure) {
OLP_SDK_LOG_ERROR_F(
kLogTag,
"Error opening disk cache, disk_path_mutable=%s, "
"disk_path_protected=%s",
porting::value_or(settings.disk_path_mutable, "(empty)").c_str(),
porting::value_or(settings.disk_path_protected, "(empty)").c_str());
return nullptr;
}
return cache;
#else
OLP_SDK_CORE_UNUSED(settings);
OLP_SDK_LOG_ERROR(kLogTag, "Default cache implementation is disabled.");
return nullptr;
#endif // OLP_SDK_ENABLE_DEFAULT_CACHE
}
void OlpClientSettingsFactory::PrewarmConnection(
const OlpClientSettings& settings, const std::string& url,
http::Network::Callback callback) {
if (url.empty() || !settings.network_request_handler) {
OLP_SDK_LOG_WARNING_F(kLogTag, "PrewarmConnection: invalid input, url='%s'",
url.c_str());
return;
}
const auto& retry_settings = settings.retry_settings;
auto request_settings =
http::NetworkSettings()
.WithTransferTimeout(std::chrono::seconds(retry_settings.timeout))
.WithConnectionTimeout(std::chrono::seconds(retry_settings.timeout))
.WithProxySettings(
settings.proxy_settings.value_or(http::NetworkProxySettings()));
auto request =
http::NetworkRequest(url)
.WithVerb(http::NetworkRequest::HttpVerb::OPTIONS)
.WithBody(nullptr)
.WithSettings(std::move(request_settings))
.WithHeader(http::kUserAgentHeader, http::kOlpSdkUserAgent);
auto outcome = settings.network_request_handler->Send(
request, nullptr, [url, callback](http::NetworkResponse response) {
OLP_SDK_LOG_INFO_F(
kLogTag,
"PrewarmConnection: completed for url='%s', status='%d %s'",
url.c_str(), response.GetStatus(), response.GetError().c_str());
// call user callback if it is set
if (callback) {
callback(std::move(response));
}
});
if (!outcome.IsSuccessful()) {
OLP_SDK_LOG_WARNING_F(
kLogTag,
"PrewarmConnection: sending OPTIONS failed, url='%s', error='%s'",
url.c_str(), ErrorCodeToString(outcome.GetErrorCode()).c_str());
}
}
} // namespace client
} // namespace olp