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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
use core::{
    pin::Pin,
    task::{Context, Poll},
};
use futures::{stream, Stream};
use pin_project::pin_project;
use std::{fmt, sync::Arc};
use tokio::sync::{broadcast, broadcast::error::RecvError};

const CHANNEL_CAPACITY: usize = 128;

/// Exposes the [`StreamSubscribe::subscribe()`] method which allows additional
/// consumers of events from a stream without consuming the stream itself.
///
/// If a subscriber begins to lag behind the stream, it will receive an [`Error::Lagged`]
/// error. The subscriber can then decide to abort its task or tolerate the lost events.
///
/// If the [`Stream`] is dropped or ends, any [`StreamSubscribe::subscribe()`] streams
/// will also end.
///
/// ## Warning
///
/// If the primary [`Stream`] is not polled, the [`StreamSubscribe::subscribe()`] streams
/// will never receive any events.
#[pin_project]
#[must_use = "subscribers will not get events unless this stream is polled"]
pub struct StreamSubscribe<S>
where
    S: Stream,
{
    #[pin]
    stream: S,
    sender: broadcast::Sender<Option<Arc<S::Item>>>,
}

impl<S: Stream> StreamSubscribe<S> {
    pub fn new(stream: S) -> Self {
        let (sender, _) = broadcast::channel(CHANNEL_CAPACITY);

        Self { stream, sender }
    }

    /// Subscribe to events from this stream
    #[must_use = "streams do nothing unless polled"]
    pub fn subscribe(&self) -> impl Stream<Item = Result<Arc<S::Item>, Error>> {
        stream::unfold(self.sender.subscribe(), |mut rx| async {
            match rx.recv().await {
                Ok(Some(obj)) => Some((Ok(obj), rx)),
                Err(RecvError::Lagged(amt)) => Some((Err(Error::Lagged(amt)), rx)),
                _ => None,
            }
        })
    }
}

impl<S: Stream> Stream for StreamSubscribe<S> {
    type Item = Arc<S::Item>;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        let this = self.project();
        let item = this.stream.poll_next(cx);

        match item {
            Poll::Ready(Some(item)) => {
                // `arc_with_non_send_sync` false positive: https://github.com/rust-lang/rust-clippy/issues/11076
                #[allow(clippy::arc_with_non_send_sync)]
                let item = Arc::new(item);
                this.sender.send(Some(item.clone())).ok();
                Poll::Ready(Some(item))
            }
            Poll::Ready(None) => {
                this.sender.send(None).ok();
                Poll::Ready(None)
            }
            Poll::Pending => Poll::Pending,
        }
    }
}

/// An error returned from the inner stream of a [`StreamSubscribe`].
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Error {
    /// The subscriber lagged too far behind. Polling again will return
    /// the oldest event still retained.
    ///
    /// Includes the number of skipped events.
    Lagged(u64),
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::Lagged(amt) => write!(f, "subscriber lagged by {amt}"),
        }
    }
}

impl std::error::Error for Error {}

#[cfg(test)]
mod tests {
    use super::*;
    use futures::{pin_mut, poll, stream, StreamExt};

    #[tokio::test]
    async fn stream_subscribe_continues_to_propagate_values() {
        let rx = stream::iter([Ok(0), Ok(1), Err(2), Ok(3), Ok(4)]);
        let rx = StreamSubscribe::new(rx);

        pin_mut!(rx);
        assert_eq!(poll!(rx.next()), Poll::Ready(Some(Arc::new(Ok(0)))));
        assert_eq!(poll!(rx.next()), Poll::Ready(Some(Arc::new(Ok(1)))));
        assert_eq!(poll!(rx.next()), Poll::Ready(Some(Arc::new(Err(2)))));
        assert_eq!(poll!(rx.next()), Poll::Ready(Some(Arc::new(Ok(3)))));
        assert_eq!(poll!(rx.next()), Poll::Ready(Some(Arc::new(Ok(4)))));
        assert_eq!(poll!(rx.next()), Poll::Ready(None));
    }

    #[tokio::test]
    async fn all_subscribers_get_events() {
        let events = [Ok(0), Ok(1), Err(2), Ok(3), Ok(4)];
        let rx = stream::iter(events);
        let rx = StreamSubscribe::new(rx);

        let rx_s1 = rx.subscribe();
        let rx_s2 = rx.subscribe();

        pin_mut!(rx);
        pin_mut!(rx_s1);
        pin_mut!(rx_s2);

        // Subscribers are pending until we start consuming the stream
        assert_eq!(poll!(rx_s1.next()), Poll::Pending, "rx_s1");
        assert_eq!(poll!(rx_s2.next()), Poll::Pending, "rx_s2");

        for item in events {
            assert_eq!(poll!(rx.next()), Poll::Ready(Some(Arc::new(item))), "rx");
            let expected = Poll::Ready(Some(Ok(Arc::new(item))));
            assert_eq!(poll!(rx_s1.next()), expected, "rx_s1");
            assert_eq!(poll!(rx_s2.next()), expected, "rx_s2");
        }

        // Ensure that if the stream is closed, all subscribers are closed
        assert_eq!(poll!(rx.next()), Poll::Ready(None), "rx");
        assert_eq!(poll!(rx_s1.next()), Poll::Ready(None), "rx_s1");
        assert_eq!(poll!(rx_s2.next()), Poll::Ready(None), "rx_s2");
    }

    #[tokio::test]
    async fn subscribers_can_catch_up_to_the_main_stream() {
        let events = (0..CHANNEL_CAPACITY).map(Ok::<_, ()>).collect::<Vec<_>>();
        let rx = stream::iter(events.clone());
        let rx = StreamSubscribe::new(rx);

        let rx_s1 = rx.subscribe();

        pin_mut!(rx);
        pin_mut!(rx_s1);

        for item in events.clone() {
            assert_eq!(poll!(rx.next()), Poll::Ready(Some(Arc::new(item))), "rx",);
        }

        for item in events {
            assert_eq!(
                poll!(rx_s1.next()),
                Poll::Ready(Some(Ok(Arc::new(item)))),
                "rx_s1"
            );
        }
    }

    #[tokio::test]
    async fn if_the_subscribers_lag_they_get_a_lagged_error_as_the_next_event() {
        // The broadcast channel rounds the capacity up to the next power of two.
        let max_capacity = CHANNEL_CAPACITY.next_power_of_two();
        let overflow = 5;
        let events = (0..max_capacity + overflow).collect::<Vec<_>>();
        let rx = stream::iter(events.clone());
        let rx = StreamSubscribe::new(rx);

        let rx_s1 = rx.subscribe();

        pin_mut!(rx);
        pin_mut!(rx_s1);

        // Consume the entire stream, overflowing the inner channel
        for _ in events {
            rx.next().await;
        }

        assert_eq!(
            poll!(rx_s1.next()),
            Poll::Ready(Some(Err(Error::Lagged(overflow as u64)))),
        );

        let expected_next_event = overflow;
        assert_eq!(
            poll!(rx_s1.next()),
            Poll::Ready(Some(Ok(Arc::new(expected_next_event)))),
        );
    }

    #[tokio::test]
    async fn a_lagging_subscriber_does_not_impact_a_well_behaved_subscriber() {
        // The broadcast channel rounds the capacity up to the next power of two.
        let max_capacity = CHANNEL_CAPACITY.next_power_of_two();
        let overflow = 5;
        let events = (0..max_capacity + overflow).collect::<Vec<_>>();
        let rx = stream::iter(events.clone());
        let rx = StreamSubscribe::new(rx);

        let rx_s1 = rx.subscribe();
        let rx_s2 = rx.subscribe();

        pin_mut!(rx);
        pin_mut!(rx_s1);
        pin_mut!(rx_s2);

        for event in events {
            assert_eq!(poll!(rx_s1.next()), Poll::Pending, "rx_s1");

            rx.next().await;

            assert_eq!(
                poll!(rx_s1.next()),
                Poll::Ready(Some(Ok(Arc::new(event)))),
                "rx_s1"
            );
        }

        assert_eq!(
            poll!(rx_s2.next()),
            Poll::Ready(Some(Err(Error::Lagged(overflow as u64)))),
            "rx_s2"
        );

        let expected_next_event = overflow;
        assert_eq!(
            poll!(rx_s2.next()),
            Poll::Ready(Some(Ok(Arc::new(expected_next_event)))),
            "rx_s2"
        );
    }
}