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
use buf::SendBuf;
use flush::Flush;
use Body;

use futures::{Future, Poll};
use h2::client::Connection;
use tokio_io::{AsyncRead, AsyncWrite};

/// Task that performs background tasks for a client.
///
/// This is not used directly by a user of this library.
pub struct Background<T, S>
where
    S: Body,
{
    task: Task<T, S>,
}

/// The specific task to execute
enum Task<T, S>
where
    S: Body,
{
    Connection(Connection<T, SendBuf<S::Data>>),
    Flush(Flush<S>),
}

// ===== impl Background =====

impl<T, S> Background<T, S>
where
    S: Body,
{
    pub(crate) fn connection(connection: Connection<T, SendBuf<S::Data>>) -> Self {
        let task = Task::Connection(connection);
        Background { task }
    }

    pub(crate) fn flush(flush: Flush<S>) -> Self {
        let task = Task::Flush(flush);
        Background { task }
    }
}

impl<T, S> Future for Background<T, S>
where
    T: AsyncRead + AsyncWrite,
    S: Body,
    S::Data: 'static,
    S::Error: Into<Box<dyn std::error::Error>>,
{
    type Item = ();
    type Error = ();

    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        use self::Task::*;

        match self.task {
            Connection(ref mut f) => f.poll().map_err(|err| {
                warn!("error driving HTTP/2 client connection: {:?}", err);
            }),
            Flush(ref mut f) => f.poll(),
        }
    }
}