memberlist_core/delegate/node.rs
1use std::{borrow::Cow, future::Future};
2
3use crate::proto::Meta;
4use bytes::Bytes;
5
6/// Used to manage node related events.
7#[auto_impl::auto_impl(Box, Arc)]
8pub trait NodeDelegate: Send + Sync + 'static {
9 /// Used to retrieve meta-data about the current node
10 /// when broadcasting an alive message. It's length is limited to
11 /// the given byte size. This metadata is available in the NodeState structure.
12 fn node_meta(&self, limit: usize) -> impl Future<Output = Meta> + Send {
13 let _ = limit;
14 async move { Meta::empty() }
15 }
16
17 /// Called when a user-data message is received.
18 /// Care should be taken that this method does not block, since doing
19 /// so would block the entire UDP packet receive loop. Additionally, the byte
20 /// slice may be modified after the call returns, so it should be copied if needed
21 fn notify_message(&self, msg: Cow<'_, [u8]>) -> impl Future<Output = ()> + Send {
22 async move {
23 let _ = msg;
24 }
25 }
26
27 /// Called when user data messages can be broadcast.
28 /// It can return a list of buffers to send. Each buffer should assume an
29 /// overhead as provided with a limit on the total byte size allowed.
30 /// The total byte size of the resulting data to send must not exceed
31 /// the limit. Care should be taken that this method does not block,
32 /// since doing so would block the entire UDP packet receive loop.
33 ///
34 /// The `encoded_len` function accepts a bytes, and will return
35 /// the same bytes back and the encoded length of the message.
36 fn broadcast_messages<F>(
37 &self,
38 limit: usize,
39 encoded_len: F,
40 ) -> impl Future<Output = impl Iterator<Item = Bytes> + Send> + Send
41 where
42 F: Fn(Bytes) -> (usize, Bytes) + Send + Sync + 'static,
43 {
44 let _ = limit;
45 let _ = encoded_len;
46 async move { std::iter::empty() }
47 }
48
49 /// Used for a TCP Push/Pull. This is sent to
50 /// the remote side in addition to the membership information. Any
51 /// data can be sent here. See `merge_remote_state` as well. The `join`
52 /// boolean indicates this is for a join instead of a push/pull.
53 fn local_state(&self, join: bool) -> impl Future<Output = Bytes> + Send {
54 let _ = join;
55 async move { Bytes::new() }
56 }
57
58 /// Invoked after a TCP Push/Pull. This is the
59 /// state received from the remote side and is the result of the
60 /// remote side's `local_state` call. The 'join'
61 /// boolean indicates this is for a join instead of a push/pull.
62 fn merge_remote_state(&self, buf: &[u8], join: bool) -> impl Future<Output = ()> + Send {
63 let _ = buf;
64 let _ = join;
65 async move {}
66 }
67}