1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use std::time::Duration;

use memberlist_core::{transport::Id, CheapClone};

use crate::types::Member;

/// Implemented to allow overriding the reconnect timeout for individual members.
#[auto_impl::auto_impl(Box, Arc)]
pub trait ReconnectDelegate: Send + Sync + 'static {
  /// The id type of the delegate
  type Id: Id;
  /// The address type of the delegate
  type Address: CheapClone + Send + Sync + 'static;

  /// Returns the reconnect timeout for the given member.
  fn reconnect_timeout(
    &self,
    member: &Member<Self::Id, Self::Address>,
    timeout: Duration,
  ) -> Duration;
}

/// Noop implementation of `ReconnectDelegate`.
#[derive(Debug)]
pub struct NoopReconnectDelegate<I, A>(std::marker::PhantomData<(I, A)>);

impl<I, A> Default for NoopReconnectDelegate<I, A> {
  fn default() -> Self {
    Self(Default::default())
  }
}

impl<I, A> Clone for NoopReconnectDelegate<I, A> {
  fn clone(&self) -> Self {
    *self
  }
}

impl<I, A> Copy for NoopReconnectDelegate<I, A> {}

impl<I, A> ReconnectDelegate for NoopReconnectDelegate<I, A>
where
  I: Id,
  A: CheapClone + Send + Sync + 'static,
{
  type Id = I;
  type Address = A;

  fn reconnect_timeout(
    &self,
    _member: &Member<Self::Id, Self::Address>,
    timeout: Duration,
  ) -> Duration {
    timeout
  }
}