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
use futures::future::FutureExt;
use futures::future::LocalBoxFuture;
use futures::stream::StreamExt;

use crate::event::ShutdownEvent;
use crate::invocation::Invocation;

use super::commits::Commits;
use super::state_keeper::EventStream;

/// A `Node` that is being [`shut_down`][crate::Node::shut_down].
pub trait Shutdown {
    /// Parametrization of the paxakos algorithm.
    type Invocation: Invocation;

    /// Polls the node's event stream, driving the shutdown to conclusion.
    fn poll_shutdown(
        &mut self,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<ShutdownEvent<Self::Invocation>>;
}

/// The default `Shutdown` implementation.
pub struct DefaultShutdown<I: Invocation> {
    trigger: LocalBoxFuture<'static, ()>,
    events: EventStream<I>,
    commits: Commits,
}

impl<I: Invocation> DefaultShutdown<I> {
    pub(crate) fn new(
        trigger: LocalBoxFuture<'static, ()>,
        events: EventStream<I>,
        commits: Commits,
    ) -> Self {
        Self {
            trigger,
            events,
            commits,
        }
    }
}

impl<I: Invocation> super::Shutdown for DefaultShutdown<I> {
    type Invocation = I;

    fn poll_shutdown(
        &mut self,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<ShutdownEvent<Self::Invocation>> {
        let _ = self.trigger.poll_unpin(cx);

        while let std::task::Poll::Ready(Some(())) = self.commits.poll_next(cx) {
            // keep going
        }

        self.events
            .poll_next_unpin(cx)
            .map(|e| e.expect("Event stream ended"))
    }
}