|
Network System 0.1.1
High-performance modular networking library for scalable client-server applications
|
Wrapper around messaging_client that adds automatic reconnection with exponential backoff and circuit breaker pattern. More...
#include <resilient_client.h>

Public Member Functions | |
| 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. | |
| ~resilient_client () noexcept | |
| Destructor - disconnects client if still connected. | |
| auto | connect () -> VoidResult |
| Connects to the server with retry logic. | |
| auto | disconnect () -> VoidResult |
| Disconnects from the server. | |
| auto | send_with_retry (std::vector< uint8_t > &&data) -> VoidResult |
| Sends data with automatic reconnection on failure. | |
| auto | is_connected () const noexcept -> bool |
| Checks if currently connected to server. | |
| auto | set_reconnect_callback (std::function< void(size_t attempt)> callback) -> void |
| Sets callback for reconnection events. | |
| auto | set_disconnect_callback (std::function< void()> callback) -> void |
| Sets callback for connection loss events. | |
| auto | get_client () const -> std::shared_ptr< core::messaging_client > |
| Gets the underlying messaging client. | |
Private Member Functions | |
| auto | reconnect () -> VoidResult |
| Attempts to reconnect with exponential backoff. | |
| auto | calculate_backoff (size_t attempt) const -> std::chrono::milliseconds |
| Calculates backoff duration for given attempt. | |
Private Attributes | |
| std::shared_ptr< core::messaging_client > | client_ |
| std::string | host_ |
| unsigned short | port_ |
| size_t | max_retries_ |
| std::chrono::milliseconds | initial_backoff_ |
| std::atomic< bool > | is_connected_ {false} |
| std::function< void(size_t)> | reconnect_callback_ |
| std::function< void()> | disconnect_callback_ |
Wrapper around messaging_client that adds automatic reconnection with exponential backoff and circuit breaker pattern.
The circuit breaker prevents excessive retry attempts when the backend is unavailable. When the circuit opens, send_with_retry() will fail immediately without attempting network calls.
Definition at line 80 of file resilient_client.h.
| kcenon::network::utils::resilient_client::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.
| client_id | Client identifier |
| host | Server hostname or IP address |
| port | Server port number |
| max_retries | Maximum number of reconnection attempts (default: 3) |
| initial_backoff | Initial backoff duration (default: 1 second) |
| cb_config | Circuit breaker configuration (default values if not specified) |
Definition at line 14 of file resilient_client.cpp.
References client_, and NETWORK_LOG_INFO.
|
noexcept |
Destructor - disconnects client if still connected.
Definition at line 45 of file resilient_client.cpp.
References disconnect(), and is_connected_.

|
private |
Calculates backoff duration for given attempt.
| attempt | Current attempt number (1-based) |
Definition at line 310 of file resilient_client.cpp.
| auto kcenon::network::utils::resilient_client::connect | ( | ) | -> VoidResult |
Connects to the server with retry logic.
Attempts to connect with exponential backoff between retries. Invokes reconnect_callback_ on each retry attempt.
Definition at line 60 of file resilient_client.cpp.
References kcenon::network::error_codes::network_system::connection_failed, kcenon::network::error_void(), NETWORK_LOG_INFO, NETWORK_LOG_WARN, and kcenon::network::ok().

| auto kcenon::network::utils::resilient_client::disconnect | ( | ) | -> VoidResult |
Disconnects from the server.
Definition at line 109 of file resilient_client.cpp.
References NETWORK_LOG_ERROR, NETWORK_LOG_INFO, and kcenon::network::ok().
Referenced by ~resilient_client().


|
nodiscard |
Gets the underlying messaging client.
Definition at line 259 of file resilient_client.cpp.
References client_.
|
nodiscardnoexcept |
Checks if currently connected to server.
Definition at line 242 of file resilient_client.cpp.
References client_, and is_connected_.
|
private |
Attempts to reconnect with exponential backoff.
Definition at line 265 of file resilient_client.cpp.
References kcenon::network::error_codes::network_system::connection_failed, kcenon::network::error_void(), NETWORK_LOG_INFO, NETWORK_LOG_WARN, and kcenon::network::ok().

| auto kcenon::network::utils::resilient_client::send_with_retry | ( | std::vector< uint8_t > && | data | ) | -> VoidResult |
Sends data with automatic reconnection on failure.
| data | Data to send (moved for efficiency) |
If connection is lost, automatically attempts to reconnect before retrying the send operation.
Definition at line 138 of file resilient_client.cpp.
References kcenon::network::error_codes_ext::network_system::circuit_open, kcenon::network::error_void(), NETWORK_LOG_DEBUG, NETWORK_LOG_ERROR, NETWORK_LOG_INFO, NETWORK_LOG_WARN, kcenon::network::ok(), and kcenon::network::error_codes::network_system::send_failed.

| auto kcenon::network::utils::resilient_client::set_disconnect_callback | ( | std::function< void()> | callback | ) | -> void |
Sets callback for connection loss events.
| callback | Function called when connection is lost |
Definition at line 253 of file resilient_client.cpp.
| auto kcenon::network::utils::resilient_client::set_reconnect_callback | ( | std::function< void(size_t attempt)> | callback | ) | -> void |
Sets callback for reconnection events.
| callback | Function called on each reconnection attempt |
The callback receives the current attempt number (1-based).
Definition at line 247 of file resilient_client.cpp.
|
private |
Underlying client
Definition at line 180 of file resilient_client.h.
Referenced by get_client(), is_connected(), and resilient_client().
|
private |
Disconnection callback
Definition at line 190 of file resilient_client.h.
|
private |
Server hostname
Definition at line 182 of file resilient_client.h.
|
private |
Initial backoff duration
Definition at line 185 of file resilient_client.h.
|
private |
Connection state
Definition at line 187 of file resilient_client.h.
Referenced by is_connected(), and ~resilient_client().
|
private |
Maximum retry attempts
Definition at line 184 of file resilient_client.h.
|
private |
Server port
Definition at line 183 of file resilient_client.h.
|
private |
Reconnection callback
Definition at line 189 of file resilient_client.h.