cpp-msgpack-rpc
0.2.0
An RPC library implementing MessagePack RPC.
Toggle main menu visibility
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
28
#include "
msgpack_rpc/common/msgpack_rpc_exception.h
"
29
#include "
msgpack_rpc/common/status_code.h
"
30
#include "
msgpack_rpc/config/reconnection_config.h
"
31
#include "
msgpack_rpc/executors/i_executor.h
"
32
#include "
msgpack_rpc/executors/operation_type.h
"
33
#include "
msgpack_rpc/executors/timer.h
"
34
#include "
msgpack_rpc/logging/logger.h
"
35
36
namespace
msgpack_rpc::clients::impl
{
37
41
class
ReconnectionTimer
{
42
public
:
50
ReconnectionTimer
(
const
std::shared_ptr<executors::IExecutor>& executor,
51
std::shared_ptr<logging::Logger> logger,
52
const
config::ReconnectionConfig
&
config
)
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.
61
if
(
max_waiting_time_
<
initial_waiting_time_
) {
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
78
MSGPACK_RPC_WARN
(
logger_
,
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
92
void
reset
() {
next_wait_time_without_jitter_
=
initial_waiting_time_
; }
93
97
void
cancel
() {
timer_
.
cancel
(); }
98
99
private
:
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
109
next_wait_time_without_jitter_
*= 2;
110
if
(
next_wait_time_without_jitter_
>
max_waiting_time_
) {
111
next_wait_time_without_jitter_
=
max_waiting_time_
;
112
}
113
114
return
wait_time;
115
}
116
118
executors::Timer
timer_
;
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>
134
jitter_time_dist_
;
135
137
std::chrono::nanoseconds
next_wait_time_without_jitter_
;
138
};
139
140
}
// namespace msgpack_rpc::clients::impl
msgpack_rpc::clients::impl::ReconnectionTimer::logger_
std::shared_ptr< logging::Logger > logger_
Logger.
Definition
reconnection_timer.h:121
msgpack_rpc::clients::impl::ReconnectionTimer::timer_
executors::Timer timer_
Timer.
Definition
reconnection_timer.h:118
msgpack_rpc::clients::impl::ReconnectionTimer::max_waiting_time_
std::chrono::nanoseconds max_waiting_time_
Maximum waiting time.
Definition
reconnection_timer.h:127
msgpack_rpc::clients::impl::ReconnectionTimer::reset
void reset()
Reset the waiting time.
Definition
reconnection_timer.h:92
msgpack_rpc::clients::impl::ReconnectionTimer::jitter_time_dist_
std::uniform_int_distribution< std::chrono::nanoseconds::rep > jitter_time_dist_
Distribution of jitter time.
Definition
reconnection_timer.h:134
msgpack_rpc::clients::impl::ReconnectionTimer::compute_waiting_time
std::chrono::nanoseconds compute_waiting_time()
Compute the waiting time.
Definition
reconnection_timer.h:105
msgpack_rpc::clients::impl::ReconnectionTimer::initial_waiting_time_
std::chrono::nanoseconds initial_waiting_time_
Initial waiting time.
Definition
reconnection_timer.h:124
msgpack_rpc::clients::impl::ReconnectionTimer::next_wait_time_without_jitter_
std::chrono::nanoseconds next_wait_time_without_jitter_
Next waiting time without jitter.
Definition
reconnection_timer.h:137
msgpack_rpc::clients::impl::ReconnectionTimer::async_wait
void async_wait(Function &&function)
Asynchronously wait until the next reconnection.
Definition
reconnection_timer.h:75
msgpack_rpc::clients::impl::ReconnectionTimer::random_
std::mt19937 random_
Random number generator.
Definition
reconnection_timer.h:130
msgpack_rpc::clients::impl::ReconnectionTimer::ReconnectionTimer
ReconnectionTimer(const std::shared_ptr< executors::IExecutor > &executor, std::shared_ptr< logging::Logger > logger, const config::ReconnectionConfig &config)
Constructor.
Definition
reconnection_timer.h:50
msgpack_rpc::clients::impl::ReconnectionTimer::cancel
void cancel()
Cancel this timer.
Definition
reconnection_timer.h:97
msgpack_rpc::MsgpackRPCException
Class of exceptions in cpp-msgpack-rpc library.
Definition
msgpack_rpc_exception.h:35
msgpack_rpc::config::ReconnectionConfig
Class of configurations of reconnection.
Definition
reconnection_config.h:31
msgpack_rpc::executors::Timer
Class of timers to call functions later.
Definition
timer.h:37
msgpack_rpc::executors::Timer::cancel
void cancel()
Cancel this timer.
Definition
timer.h:89
msgpack_rpc::executors::Timer::async_sleep_for
void async_sleep_for(std::chrono::steady_clock::duration duration, Function &&function)
Asynchronously sleep for a duration.
Definition
timer.h:75
i_executor.h
Definition of IExecutor class.
logger.h
Definition of Logger class.
MSGPACK_RPC_WARN
#define MSGPACK_RPC_WARN(LOGGER_PTR,...)
Write a warning log.
Definition
logger.h:210
msgpack_rpc_exception.h
Definition of MsgpackRPCException class.
msgpack_rpc::clients::impl
Namespace of internal implementations.
Definition
i_call_future_impl.h:26
msgpack_rpc::config
Namespace of configurations.
Definition
client_config.h:32
msgpack_rpc::executors
Namespace of executors to process asynchronous tasks.
Definition
asio_context_type.h:24
std
STL namespace.
operation_type.h
Definition of OperationType enumeration.
reconnection_config.h
Definition of ReconnectionConfig class.
status_code.h
Definition of StatusCode enumeration.
timer.h
Definition of Timer class.
src
msgpack_rpc
clients
impl
reconnection_timer.h
Generated on
for cpp-msgpack-rpc by
1.18.0