Skip to main content

ConnectionAttemptState

Struct ConnectionAttemptState 

Source
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 current ConnectionStatus indicating 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 is None if 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

Source

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 initial ConnectionStatus for 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);
Source

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()));
Source

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 a String, such as &str, String, error types that implement Display, 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

Source§

fn clone(&self) -> ConnectionAttemptState

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ConnectionAttemptState

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for ConnectionAttemptState

Source§

impl PartialEq for ConnectionAttemptState

Source§

fn eq(&self, other: &ConnectionAttemptState) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for ConnectionAttemptState

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.