Chops Net IP
Loading...
Searching...
No Matches
tcp_connector_timeout.hpp
Go to the documentation of this file.
1
41#ifndef TCP_CONNECTOR_TIMEOUT_HPP_INCLUDED
42#define TCP_CONNECTOR_TIMEOUT_HPP_INCLUDED
43
44#include <cstddef> // std::size_t
45#include <cmath> // std::pow
46#include <chrono>
47#include <optional>
48#include <functional> // std::function
49
50namespace chops {
51namespace net {
52
53/*
54 * @brief A using declaration for @c std::optional of @c std::chrono::milliseconds.
55 */
56using optional_millis = std::optional<std::chrono::milliseconds>;
57/*
58 * @brief Signature of TCP connector timeout function objects.
59 *
60 * Within the TCP connector the timeout function object is stored within a @c std::function.
61 * The function object must be copy constructible.
62 */
63using tcp_connector_timeout_func = std::function< optional_millis (std::size_t) >;
64
65/*
66 * @brief A @c simple_timeout always returns the same timeout value.
67 *
68 */
70
71 const std::chrono::milliseconds m_timeout;
72
73/*
74 * @brief Construct a @c simple_timeout.
75 *
76 * @param timeout Milliseconds to wait between unsuccessful connect attempts.
77 */
78 simple_timeout(std::chrono::milliseconds timeout = std::chrono::milliseconds(1000)) : m_timeout(timeout) {}
79
80/*
81 * @brief Function call interface, for use by TCP connector functionality.
82 *
83 * This method is not directly called by application code. It is called by the TCP connector
84 * internal functionality. See file documentation for documentation on parameters and return
85 * value.
86 */
87 optional_millis operator()(std::size_t) const noexcept {
88 return optional_millis { m_timeout };
89 }
90};
91
92/*
93 * @brief A @c counted_timeout limits the number of connect attempts.
94 *
95 */
97
98 const std::chrono::milliseconds m_timeout;
99 const std::size_t m_max_attempts;
100
101/*
102 * @brief Construct a @c counted_timeout.
103 *
104 * @param timeout Milliseconds to wait between unsuccessful connect attempts.
105 *
106 * @param max_conn_attempts Maximum number of connect attempts.
107 */
108 counted_timeout (std::chrono::milliseconds timeout, std::size_t max_conn_attempts) :
109 m_timeout(timeout), m_max_attempts(max_conn_attempts) {}
110
111/*
112 * @brief Function call interface, for use by TCP connector functionality.
113 *
114 * This method is not directly called by application code. It is called by the TCP connector
115 * internal functionality. See file documentation for documentation on parameters and return
116 * value.
117 */
118 optional_millis operator()(std::size_t attempts) const noexcept {
119 return (attempts > m_max_attempts) ? optional_millis { } : optional_millis { m_timeout };
120 }
121};
122
123/*
124 * @brief Increase the timeout value by a scaling factor up to a maximum value.
125 *
126 * Increase the timeout value for each unsuccessful connect attempt. This decreases network
127 * traffic where there are multiple connectors all trying to connect to an unreachable host.
128 *
129 */
131
132 using tick_type = typename std::chrono::milliseconds::rep;
133 const tick_type m_initial_ticks;
134 const tick_type m_max_ticks;
135 const int m_scale_factor;
136
137/*
138 * @brief Construct a @c backoff_timeout.
139 *
140 * @param initial_timeout Milliseconds to wait for the first connect attempt.
141 *
142 * @param max_timeout Maximum timeout value.
143 *
144 * @param scale_factor The timeout value is multiplied by this number for each connect attempt.
145 */
146 backoff_timeout(std::chrono::milliseconds initial_timeout, std::chrono::milliseconds max_timeout,
147 int scale_factor = 2) :
148 m_initial_ticks(initial_timeout.count()), m_max_ticks(max_timeout.count()),
149 m_scale_factor(scale_factor) { }
150
151/*
152 * @brief Function call interface, for use by TCP connector functionality.
153 *
154 * This method is not directly called by application code. It is called by the TCP connector
155 * internal functionality. See file documentation for documentation on parameters and return
156 * value.
157 */
158 optional_millis operator()(std::size_t attempts) const noexcept {
159 auto tmp = static_cast<tick_type>((attempts-1u) * m_scale_factor * m_initial_ticks);
160 tmp = (tmp == 0u ? m_initial_ticks : ((tmp > m_max_ticks) ? m_max_ticks : tmp));
161 return optional_millis { std::chrono::milliseconds { tmp } };
162 }
163};
164
165/*
166 * @brief Exponentially increase the timeout value up to a maximum.
167 *
168 * Increase the timeout value similar to @c backoff_timeout, except using an exponential
169 * @c std::pow calculation instead of a scaled backoff.
170 *
171 */
173
174 using tick_type = typename std::chrono::milliseconds::rep;
175 const tick_type m_initial_ticks;
176 const tick_type m_max_ticks;
177
178/*
179 * @brief Construct a @c exponential_backoff_timeout.
180 *
181 * @param initial_timeout Milliseconds to wait for the first connect attempt.
182 *
183 * @param max_timeout Maximum timeout value.
184 *
185 */
186 exponential_backoff_timeout(std::chrono::milliseconds initial_timeout,
187 std::chrono::milliseconds max_timeout) :
188 m_initial_ticks(initial_timeout.count()), m_max_ticks(max_timeout.count()) { }
189
190/*
191 * @brief Function call interface, for use by TCP connector functionality.
192 *
193 * This method is not directly called by application code. It is called by the TCP connector
194 * internal functionality. See file documentation for documentation on parameters and return
195 * value.
196 */
197 optional_millis operator()(std::size_t attempts) const noexcept {
198
199 auto tmp = static_cast<tick_type>(std::pow(m_initial_ticks, attempts));
200 std::chrono::milliseconds tout { (tmp > m_max_ticks) ? m_max_ticks : tmp };
201 return optional_millis { tout };
202 } //end operator()
203
204};
205
206} // end net namespace
207} // end chops namespace
208
209#endif
210
Definition tcp_connector_timeout.hpp:130
Definition tcp_connector_timeout.hpp:96
Definition tcp_connector_timeout.hpp:172
Definition tcp_connector_timeout.hpp:69