cpp-msgpack-rpc 0.2.0
An RPC library implementing MessagePack RPC.
Loading...
Searching...
No Matches
reconnection_timer.h
Go to the documentation of this file.
1/*
2 * Copyright 2023 MusicScience37 (Kenta Kabashima)
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 */
20#pragma once
21
22#include <chrono>
23#include <memory>
24#include <random>
25#include <ratio>
26#include <utility>
27
35
37
42public:
50 ReconnectionTimer(const std::shared_ptr<executors::IExecutor>& executor,
51 std::shared_ptr<logging::Logger> logger,
53 : timer_(executor, executors::OperationType::TRANSPORT),
54 logger_(std::move(logger)),
55 initial_waiting_time_(config.initial_waiting_time()),
56 max_waiting_time_(config.max_waiting_time()),
57 random_(std::random_device()()),
58 jitter_time_dist_(0, config.max_jitter_waiting_time().count()),
59 next_wait_time_without_jitter_(config.initial_waiting_time()) {
60 // This cannot be checked in ReconnectionConfig.
62 throw MsgpackRPCException(StatusCode::INVALID_ARGUMENT,
63 "The maximum waiting time must be longer than of equal to the "
64 "initial waiting time.");
65 }
66 }
67
74 template <typename Function>
75 void async_wait(Function&& function) {
76 const auto wait_time = compute_waiting_time();
77
79 "Failed to connect to all URIs, so retry after {:.3f} seconds.",
80 std::chrono::duration_cast<std::chrono::duration<double>>(wait_time)
81 .count());
82
83 timer_.async_sleep_for(wait_time, std::forward<Function>(function));
84 }
85
93
97 void cancel() { timer_.cancel(); }
98
99private:
105 [[nodiscard]] std::chrono::nanoseconds compute_waiting_time() {
106 std::chrono::nanoseconds wait_time = next_wait_time_without_jitter_;
107 wait_time += std::chrono::nanoseconds(jitter_time_dist_(random_));
108
112 }
113
114 return wait_time;
115 }
116
119
121 std::shared_ptr<logging::Logger> logger_;
122
124 std::chrono::nanoseconds initial_waiting_time_;
125
127 std::chrono::nanoseconds max_waiting_time_;
128
130 std::mt19937 random_;
131
133 std::uniform_int_distribution<std::chrono::nanoseconds::rep>
135
137 std::chrono::nanoseconds next_wait_time_without_jitter_;
138};
139
140} // namespace msgpack_rpc::clients::impl
std::shared_ptr< logging::Logger > logger_
Logger.
std::chrono::nanoseconds max_waiting_time_
Maximum waiting time.
std::uniform_int_distribution< std::chrono::nanoseconds::rep > jitter_time_dist_
Distribution of jitter time.
std::chrono::nanoseconds compute_waiting_time()
Compute the waiting time.
std::chrono::nanoseconds initial_waiting_time_
Initial waiting time.
std::chrono::nanoseconds next_wait_time_without_jitter_
Next waiting time without jitter.
void async_wait(Function &&function)
Asynchronously wait until the next reconnection.
std::mt19937 random_
Random number generator.
ReconnectionTimer(const std::shared_ptr< executors::IExecutor > &executor, std::shared_ptr< logging::Logger > logger, const config::ReconnectionConfig &config)
Constructor.
Class of exceptions in cpp-msgpack-rpc library.
Class of configurations of reconnection.
Class of timers to call functions later.
Definition timer.h:37
Definition of IExecutor class.
Definition of Logger class.
#define MSGPACK_RPC_WARN(LOGGER_PTR,...)
Write a warning log.
Definition logger.h:210
Definition of MsgpackRPCException class.
Namespace of internal implementations.
Namespace of configurations.
Namespace of executors to process asynchronous tasks.
STL namespace.
Definition of OperationType enumeration.
Definition of ReconnectionConfig class.
Definition of StatusCode enumeration.
Definition of Timer class.