serf_core/delegate/
merge.rs

1use memberlist_core::{CheapClone, transport::Id};
2use std::{future::Future, sync::Arc};
3
4use crate::types::Member;
5
6/// Used to involve a client in
7/// a potential cluster merge operation. Namely, when
8/// a node does a promised push/pull (as part of a join),
9/// the delegate is involved and allowed to cancel the join
10/// based on custom logic. The merge delegate is NOT invoked
11/// as part of the push-pull anti-entropy.
12#[auto_impl::auto_impl(Box, Arc)]
13pub trait MergeDelegate: Send + Sync + 'static {
14  /// The error type of the delegate
15  type Error: std::error::Error + Send + Sync + 'static;
16  /// The id type of the delegate
17  type Id: Id;
18  /// The address type of the delegate
19  type Address: CheapClone + Send + Sync + 'static;
20
21  /// Invoked when a merge could take place.
22  /// Provides a list of the nodes known by the peer. If
23  /// the return value is `Err`, the merge is canceled.
24  fn notify_merge(
25    &self,
26    members: Arc<[Member<Self::Id, Self::Address>]>,
27  ) -> impl Future<Output = Result<(), Self::Error>> + Send;
28}
29
30/// A default implementation of the `MergeDelegate` trait.
31#[derive(Debug, Clone, Copy)]
32pub struct DefaultMergeDelegate<I, A>(std::marker::PhantomData<(I, A)>);
33
34impl<I, A> Default for DefaultMergeDelegate<I, A> {
35  fn default() -> Self {
36    Self(Default::default())
37  }
38}
39
40impl<I, A> MergeDelegate for DefaultMergeDelegate<I, A>
41where
42  I: Id + Send + Sync + 'static,
43  A: CheapClone + Send + Sync + 'static,
44{
45  type Error = std::convert::Infallible;
46  type Id = I;
47  type Address = A;
48
49  async fn notify_merge(
50    &self,
51    _members: Arc<[Member<Self::Id, Self::Address>]>,
52  ) -> Result<(), Self::Error> {
53    Ok(())
54  }
55}