olp-cpp-sdk  1.21.0
BackdownStrategy.h
1 /*
2  * Copyright (C) 2020-2022 HERE Europe B.V.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * SPDX-License-Identifier: Apache-2.0
17  * License-Filename: LICENSE
18  */
19 
20 #pragma once
21 
22 #include <algorithm>
23 #include <chrono>
24 #include <random>
25 
26 #include <olp/core/CoreApi.h>
27 
28 namespace olp {
29 namespace client {
30 
46 struct CORE_API ExponentialBackdownStrategy {
47  public:
57  std::chrono::milliseconds operator()(
58  std::chrono::milliseconds initial_backdown_period, size_t retry_count) {
59  const auto exponential_wait_time =
60  initial_backdown_period.count() * (size_t{1} << retry_count);
61 
62  static thread_local std::mt19937 kGenerator(std::random_device{}());
63  std::uniform_int_distribution<std::chrono::milliseconds::rep> dist(
64  0, exponential_wait_time);
65  return std::chrono::milliseconds(dist(kGenerator));
66  }
67 };
68 
82 struct CORE_API EqualJitterBackdownStrategy {
83  public:
90  std::chrono::milliseconds cap = std::chrono::seconds(1))
91  : cap_{cap} {}
92 
102  std::chrono::milliseconds operator()(
103  std::chrono::milliseconds initial_backdown_period, size_t retry_count) {
104  // make sure we don't overflow
105  constexpr size_t max_retry_count = 30u;
106  retry_count = std::min<size_t>(retry_count, max_retry_count);
107  const int64_t exponential_wait_time =
108  initial_backdown_period.count() * (1ull << retry_count);
109  static thread_local std::mt19937 kGenerator(std::random_device{}());
110  const auto temp = std::min<int64_t>(cap_.count(), exponential_wait_time);
111  std::uniform_int_distribution<int64_t> dist(0, temp / 2);
112  const auto sleep = temp / 2 + dist(kGenerator);
113  return std::chrono::milliseconds(sleep);
114  }
115 
116  private:
117  std::chrono::milliseconds cap_;
118 };
119 
120 } // namespace client
121 } // namespace olp
Rules all the other namespaces.
Definition: AppleSignInProperties.h:24
Computes wait time for the next retry attempt via the exponential backoff with the added jitter.
Definition: BackdownStrategy.h:82
EqualJitterBackdownStrategy(std::chrono::milliseconds cap=std::chrono::seconds(1))
Creates a EqualJitterBackdownStrategy instance.
Definition: BackdownStrategy.h:89
std::chrono::milliseconds operator()(std::chrono::milliseconds initial_backdown_period, size_t retry_count)
Computes the next retry attempt wait time based on the number of retries and initial backdown period.
Definition: BackdownStrategy.h:102
Computes wait time for the next retry attempt via the exponential backoff with the added jitter.
Definition: BackdownStrategy.h:46
std::chrono::milliseconds operator()(std::chrono::milliseconds initial_backdown_period, size_t retry_count)
Computes the next retry attempt wait time based on the number of retries and initial backdown period.
Definition: BackdownStrategy.h:57