rw_futures_util/
pausable.rs

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
// Copyright 2024 RisingWave Labs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::pin::Pin;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::task::{Context, Poll, Waker};

use futures::Stream;
use pin_project_lite::pin_project;

pin_project! {
    #[derive(Debug)]
    #[must_use = "streams do nothing unless polled"]
    pub struct Pausable<St>
        where St: Stream
    {
        #[pin]
        stream: St,
        paused: Arc<AtomicBool>,
        waker: Arc<Mutex<Option<Waker>>>,
    }
}

/// A valve is a handle that can control the [`Pausable`] stream.
#[derive(Clone)]
pub struct Valve {
    paused: Arc<AtomicBool>,
    waker: Arc<Mutex<Option<Waker>>>,
}

impl Valve {
    /// Pause the stream controlled by the valve.
    pub fn pause(&self) {
        self.paused.store(true, Ordering::Relaxed);
    }

    /// Resume the stream controlled by the valve.
    pub fn resume(&self) {
        self.paused.store(false, Ordering::Relaxed);
        if let Some(waker) = self.waker.lock().unwrap().as_ref() {
            waker.wake_by_ref()
        }
    }
}

impl<St> Pausable<St>
where
    St: Stream,
{
    pub(crate) fn new(stream: St) -> (Self, Valve) {
        let paused = Arc::new(AtomicBool::new(false));
        let waker = Arc::new(Mutex::new(None));
        (
            Pausable {
                stream,
                paused: paused.clone(),
                waker: waker.clone(),
            },
            Valve { paused, waker },
        )
    }
}

impl<St> Stream for Pausable<St>
where
    St: Stream,
{
    type Item = St::Item;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        let this = self.project();
        if this.paused.load(Ordering::Relaxed) {
            let mut waker = this.waker.lock().unwrap();
            *waker = Some(cx.waker().clone());
            Poll::Pending
        } else {
            this.stream.poll_next(cx)
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.stream.size_hint()
    }
}