pub struct ConnectionAttemptState {
pub status: ConnectionStatus,
pub attempts: usize,
pub last_error: Option<String>,
}Expand description
Tracks the state of connection attempts for a specific host and plugin combination.
This structure maintains comprehensive information about connection attempts, including
the current connection status, the number of attempts that have been made, and any error
message from the most recent failed attempt. It is used by the State structure to
track connection history and current state for each host/plugin pair.
§Fields
-
status- The currentConnectionStatusindicating whether the connection is in progress, successfully established, pending retry, or has failed. This field provides high-level information about the connection state. -
attempts- The total number of connection attempts that have been made for this host/plugin combination. This counter is incremented each time a connection attempt begins and is preserved across status changes, allowing you to track how many attempts were needed before a connection succeeded or ultimately failed. -
last_error- An optional error message from the most recent failed connection attempt. This field isNoneif no error has occurred or if the connection is currently in progress. When present, it contains diagnostic information about why the last connection attempt failed, which can be useful for troubleshooting and logging.
§Examples
// Create a new connection state indicating a successful connection
let state = ConnectionAttemptState::new(ConnectionStatus::Connected)
.with_attempts(2);
assert_eq!(state.status, ConnectionStatus::Connected);
assert_eq!(state.attempts, 2);
assert_eq!(state.last_error, None);
// Create a connection state with a failure and error message
let failed_state = ConnectionAttemptState::new(
ConnectionStatus::Failed(ConnectionFailureKind::Timeout)
)
.with_attempts(3)
.with_last_error("connection timed out after 30 seconds");
assert_eq!(failed_state.attempts, 3);
assert_eq!(
failed_state.last_error,
Some("connection timed out after 30 seconds".to_string())
);Fields§
§status: ConnectionStatus§attempts: usize§last_error: Option<String>Implementations§
Source§impl ConnectionAttemptState
impl ConnectionAttemptState
Sourcepub fn new(status: ConnectionStatus) -> Self
pub fn new(status: ConnectionStatus) -> Self
Creates a new ConnectionAttemptState with the specified connection status.
This constructor initializes a connection attempt state with the given status, setting the attempt counter to 0 and leaving the error field empty. This is typically used when first recording a connection attempt or when transitioning to a new connection state.
§Parameters
status- The initialConnectionStatusfor this connection attempt state. This indicates whether the connection is in progress, successfully established, pending retry, or has failed.
§Returns
Returns a new ConnectionAttemptState instance with the specified status,
zero attempts, and no error message.
§Examples
// Create a state for a new connection attempt
let state = ConnectionAttemptState::new(ConnectionStatus::Connecting);
assert_eq!(state.status, ConnectionStatus::Connecting);
assert_eq!(state.attempts, 0);
assert_eq!(state.last_error, None);Sourcepub fn with_attempts(self, attempts: usize) -> Self
pub fn with_attempts(self, attempts: usize) -> Self
Sets the number of connection attempts for this connection state.
This builder method allows you to specify how many connection attempts have been made for a particular host/plugin combination. The attempt count is typically used to track retry behavior and can be helpful for implementing exponential backoff strategies or determining when to give up on a connection.
This method consumes self and returns a new instance with the updated attempt
count, following the builder pattern for convenient method chaining.
§Parameters
attempts- The number of connection attempts to record. This should represent the total number of attempts made, not just the increment. A value of 0 indicates no attempts have been made yet, while higher values indicate multiple retry attempts.
§Returns
Returns self with the attempts field updated to the specified value, allowing
for method chaining with other builder methods like with_last_error.
§Examples
// Create a connection state with 3 attempts
let state = ConnectionAttemptState::new(ConnectionStatus::Connecting)
.with_attempts(3);
assert_eq!(state.attempts, 3);// Chain multiple builder methods together
let state = ConnectionAttemptState::new(
ConnectionStatus::Failed(ConnectionFailureKind::Timeout)
)
.with_attempts(5)
.with_last_error("connection timed out after 30 seconds");
assert_eq!(state.attempts, 5);
assert_eq!(state.last_error, Some("connection timed out after 30 seconds".to_string()));Sourcepub fn with_last_error(self, last_error: impl Into<String>) -> Self
pub fn with_last_error(self, last_error: impl Into<String>) -> Self
Sets the error message from the most recent failed connection attempt.
This builder method allows you to record diagnostic information about why a connection attempt failed. The error message is typically set when marking a connection as retry pending or failed, and can be used for logging, debugging, or displaying error information to users.
This method consumes self and returns a new instance with the error message
set, following the builder pattern for convenient method chaining.
§Parameters
last_error- The error message to record. Can be any type that converts into aString, such as&str,String, error types that implementDisplay, or other string-like types. The error message should provide meaningful diagnostic information about what went wrong during the connection attempt.
§Returns
Returns self with the last_error field set to Some(error_message), allowing
for method chaining with other builder methods like with_attempts.
§Examples
// Create a connection state with an error message
let state = ConnectionAttemptState::new(
ConnectionStatus::Failed(ConnectionFailureKind::Timeout)
)
.with_last_error("connection timed out after 30 seconds");
assert_eq!(
state.last_error,
Some("connection timed out after 30 seconds".to_string())
);// Chain multiple builder methods together
let state = ConnectionAttemptState::new(ConnectionStatus::RetryPending)
.with_attempts(2)
.with_last_error("authentication failed: invalid credentials");
assert_eq!(state.attempts, 2);
assert_eq!(
state.last_error,
Some("authentication failed: invalid credentials".to_string())
);// Use with String type
let error_msg = String::from("network unreachable");
let state = ConnectionAttemptState::new(
ConnectionStatus::Failed(ConnectionFailureKind::Transport)
)
.with_last_error(error_msg);
assert_eq!(state.last_error, Some("network unreachable".to_string()));Trait Implementations§
Source§impl Clone for ConnectionAttemptState
impl Clone for ConnectionAttemptState
Source§fn clone(&self) -> ConnectionAttemptState
fn clone(&self) -> ConnectionAttemptState
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for ConnectionAttemptState
impl Debug for ConnectionAttemptState
impl Eq for ConnectionAttemptState
Source§impl PartialEq for ConnectionAttemptState
impl PartialEq for ConnectionAttemptState
Source§fn eq(&self, other: &ConnectionAttemptState) -> bool
fn eq(&self, other: &ConnectionAttemptState) -> bool
self and other values to be equal, and is used by ==.impl StructuralPartialEq for ConnectionAttemptState
Auto Trait Implementations§
impl Freeze for ConnectionAttemptState
impl RefUnwindSafe for ConnectionAttemptState
impl Send for ConnectionAttemptState
impl Sync for ConnectionAttemptState
impl Unpin for ConnectionAttemptState
impl UnsafeUnpin for ConnectionAttemptState
impl UnwindSafe for ConnectionAttemptState
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.