Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
resilient_client.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
7#include <atomic>
8#include <chrono>
9#include <functional>
10#include <memory>
11#include <string>
12#include <vector>
13
15
16#ifdef WITH_COMMON_SYSTEM
17#include "kcenon/common/resilience/circuit_breaker.h"
18#include "kcenon/common/resilience/circuit_state.h"
19#endif
20
22
24{
25
81 {
82 public:
92 resilient_client(const std::string& client_id,
93 const std::string& host,
94 unsigned short port,
95 size_t max_retries = 3,
96 std::chrono::milliseconds initial_backoff = std::chrono::seconds(1)
97#ifdef WITH_COMMON_SYSTEM
98 , common::resilience::circuit_breaker_config cb_config = {}
99#endif
100 );
101
105 ~resilient_client() noexcept;
106
114 auto connect() -> VoidResult;
115
120 auto disconnect() -> VoidResult;
121
130 auto send_with_retry(std::vector<uint8_t>&& data) -> VoidResult;
131
136 [[nodiscard]] auto is_connected() const noexcept -> bool;
137
144 auto set_reconnect_callback(std::function<void(size_t attempt)> callback) -> void;
145
150 auto set_disconnect_callback(std::function<void()> callback) -> void;
151
156 [[nodiscard]] auto get_client() const -> std::shared_ptr<core::messaging_client>;
157
158#ifdef WITH_COMMON_SYSTEM
163 [[nodiscard]] auto circuit_state() const -> common::resilience::circuit_state;
164#endif
165
166 private:
171 auto reconnect() -> VoidResult;
172
178 auto calculate_backoff(size_t attempt) const -> std::chrono::milliseconds;
179
180 std::shared_ptr<core::messaging_client> client_;
182 std::string host_;
183 unsigned short port_;
185 std::chrono::milliseconds initial_backoff_;
187 std::atomic<bool> is_connected_{false};
189 std::function<void(size_t)> reconnect_callback_;
190 std::function<void()> disconnect_callback_;
192#ifdef WITH_COMMON_SYSTEM
193 std::unique_ptr<common::resilience::circuit_breaker> circuit_breaker_;
194#endif
195 };
196
197} // namespace kcenon::network::utils
Wrapper around messaging_client that adds automatic reconnection with exponential backoff and circuit...
auto reconnect() -> VoidResult
Attempts to reconnect with exponential backoff.
auto set_reconnect_callback(std::function< void(size_t attempt)> callback) -> void
Sets callback for reconnection events.
std::shared_ptr< core::messaging_client > client_
~resilient_client() noexcept
Destructor - disconnects client if still connected.
auto calculate_backoff(size_t attempt) const -> std::chrono::milliseconds
Calculates backoff duration for given attempt.
resilient_client(const std::string &client_id, const std::string &host, unsigned short port, size_t max_retries=3, std::chrono::milliseconds initial_backoff=std::chrono::seconds(1))
Constructs a resilient client with reconnection support.
auto disconnect() -> VoidResult
Disconnects from the server.
auto connect() -> VoidResult
Connects to the server with retry logic.
auto send_with_retry(std::vector< uint8_t > &&data) -> VoidResult
Sends data with automatic reconnection on failure.
std::function< void(size_t)> reconnect_callback_
auto is_connected() const noexcept -> bool
Checks if currently connected to server.
auto get_client() const -> std::shared_ptr< core::messaging_client >
Gets the underlying messaging client.
auto set_disconnect_callback(std::function< void()> callback) -> void
Sets callback for connection loss events.
TCP client implementation.
Utility components for network_system.
Result< std::monostate > VoidResult
Network-specific error and result type definitions.