|
1 | 1 | /* |
2 | | - * Copyright (C) 2020-2022 HERE Europe B.V. |
| 2 | + * Copyright (C) 2020-2024 HERE Europe B.V. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
@@ -47,9 +47,7 @@ std::string FindApi(const Apis& apis, const std::string& service, |
47 | 47 |
|
48 | 48 | OlpClient CreateClient(const std::string& base_url, |
49 | 49 | const OlpClientSettings& settings) { |
50 | | - OlpClient client; |
51 | | - client.SetBaseUrl(base_url); |
52 | | - client.SetSettings(settings); |
| 50 | + OlpClient client(settings, base_url); |
53 | 51 | return client; |
54 | 52 | } |
55 | 53 |
|
@@ -227,20 +225,28 @@ CancellationToken ApiLookupClientImpl::LookupApi( |
227 | 225 | OlpClient ApiLookupClientImpl::CreateAndCacheClient( |
228 | 226 | const std::string& base_url, const std::string& cache_key, |
229 | 227 | boost::optional<time_t> expiration) { |
| 228 | + const auto new_expiration = |
| 229 | + std::chrono::steady_clock::now() + |
| 230 | + std::chrono::seconds(expiration.value_or(kLookupApiDefaultExpiryTime)); |
| 231 | + |
230 | 232 | std::lock_guard<std::mutex> lock(cached_clients_mutex_); |
231 | | - ClientWithExpiration& client_with_expiration = cached_clients_[cache_key]; |
232 | 233 |
|
233 | | - const auto current_base_url = client_with_expiration.client.GetBaseUrl(); |
234 | | - if (current_base_url.empty()) { |
235 | | - client_with_expiration.client.SetSettings(settings_); |
| 234 | + auto findIt = cached_clients_.find(cache_key); |
| 235 | + if (findIt == cached_clients_.end()) { |
| 236 | + auto emplace_result = cached_clients_.emplace( |
| 237 | + cache_key, |
| 238 | + ClientWithExpiration{OlpClient(settings_, base_url), new_expiration}); |
| 239 | + return emplace_result.first->second.client; |
236 | 240 | } |
| 241 | + |
| 242 | + ClientWithExpiration& client_with_expiration = findIt->second; |
| 243 | + const auto current_base_url = client_with_expiration.client.GetBaseUrl(); |
237 | 244 | if (current_base_url != base_url) { |
238 | 245 | client_with_expiration.client.SetBaseUrl(base_url); |
239 | 246 | } |
240 | 247 |
|
241 | | - client_with_expiration.expire_at = |
242 | | - std::chrono::steady_clock::now() + |
243 | | - std::chrono::seconds(expiration.value_or(kLookupApiDefaultExpiryTime)); |
| 248 | + client_with_expiration.expire_at = new_expiration; |
| 249 | + |
244 | 250 | return client_with_expiration.client; |
245 | 251 | } |
246 | 252 |
|
|
0 commit comments