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
use futures::{future::Ready, Future};

use crate::ServerPrepare;

pub struct NoGraceful;
pub struct Graceful<Fut>(Fut);

impl<Fut> FetchGraceful for Graceful<Fut>
where
    Fut: Future<Output = ()> + Send + 'static,
{
    type Future = Fut;

    fn get_graceful(self) -> Option<Self::Future> {
        Some(self.0)
    }
}

pub trait FetchGraceful {
    type Future: Future<Output = ()> + Send + 'static;
    fn get_graceful(self) -> Option<Self::Future>;
}

impl FetchGraceful for NoGraceful {
    type Future = Ready<()>;

    fn get_graceful(self) -> Option<Self::Future> {
        None
    }
}

impl<C, FutEffect, Log, State, Decorator>
    ServerPrepare<C, FutEffect, Log, State, NoGraceful, Decorator>
{
    /// set the graceful shutdown signal
    pub fn graceful_shutdown<Fut>(
        self,
        future: Fut,
    ) -> ServerPrepare<C, FutEffect, Log, State, Graceful<Fut>, Decorator>
    where
        Fut: Future<Output = ()>,
    {
        ServerPrepare::new(self.prepares, Graceful(future), self.state, self.span)
    }
}