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
//! Tower service layer for batch processing.

use std::{fmt, marker::PhantomData};

use tower::layer::Layer;
use tower::Service;

use super::{service::Batch, BatchControl};

/// Adds a layer performing batch processing of requests.
///
/// The default Tokio executor is used to run the given service,
/// which means that this layer can only be used on the Tokio runtime.
///
/// See the module documentation for more details.
pub struct BatchLayer<Request> {
    max_items_in_batch: usize,
    max_batches: Option<usize>,
    max_latency: std::time::Duration,

    // TODO: is the variance correct here?
    // https://doc.rust-lang.org/1.33.0/nomicon/subtyping.html#variance
    // https://doc.rust-lang.org/nomicon/phantom-data.html#table-of-phantomdata-patterns
    _handles_requests: PhantomData<fn(Request)>,
}

impl<Request> BatchLayer<Request> {
    /// Creates a new `BatchLayer`.
    ///
    /// The wrapper is responsible for telling the inner service when to flush a
    /// batch of requests. See [`Batch::new()`] for details.
    pub fn new(
        max_items_in_batch: usize,
        max_batches: impl Into<Option<usize>>,
        max_latency: std::time::Duration,
    ) -> Self {
        BatchLayer {
            max_items_in_batch,
            max_batches: max_batches.into(),
            max_latency,
            _handles_requests: PhantomData,
        }
    }
}

impl<S, Request> Layer<S> for BatchLayer<Request>
where
    S: Service<BatchControl<Request>> + Send + 'static,
    S::Future: Send,
    S::Response: Send,
    S::Error: Into<crate::BoxError> + Send + Sync,
    Request: Send + 'static,
{
    type Service = Batch<S, Request>;

    fn layer(&self, service: S) -> Self::Service {
        Batch::new(
            service,
            self.max_items_in_batch,
            self.max_batches,
            self.max_latency,
        )
    }
}

impl<Request> fmt::Debug for BatchLayer<Request> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("BufferLayer")
            .field("max_items_in_batch", &self.max_items_in_batch)
            .field("max_batches", &self.max_batches)
            .field("max_latency", &self.max_latency)
            .finish()
    }
}