Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions tower/src/buffer/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ impl<Request> BufferLayer<Request> {
/// `bound` gives the maximal number of requests that can be queued for the service before
/// backpressure is applied to callers.
///
///
/// # Panics
///
/// Panics if `bound` is zero.
///
///
/// # A note on choosing a `bound`
///
/// When [`Buffer`]'s implementation of [`poll_ready`] returns [`Poll::Ready`], it reserves a
Expand All @@ -34,6 +40,7 @@ impl<Request> BufferLayer<Request> {
/// [`call`]: crate::Service::call
/// [`poll_ready`]: crate::Service::poll_ready
pub const fn new(bound: usize) -> Self {
assert!(bound > 0, "buffer bound must be greater than zero");
BufferLayer {
bound,
_p: PhantomData,
Expand Down
9 changes: 9 additions & 0 deletions tower/src/buffer/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ where
/// The default Tokio executor is used to run the given service, which means that this method
/// must be called while on the Tokio runtime.
///
/// # Panics
///
/// Panics if `bound` is zero.
///
/// # A note on choosing a `bound`
///
/// When [`Buffer`]'s implementation of [`poll_ready`] returns [`Poll::Ready`], it reserves a
Expand Down Expand Up @@ -63,13 +67,18 @@ where
/// This is useful if you do not want to spawn directly onto the tokio runtime
/// but instead want to use your own executor. This will return the [`Buffer`] and
/// the background `Worker` that you can then spawn.
///
/// # Panics
///
/// Panics if `bound` is zero.
pub fn pair<S>(service: S, bound: usize) -> (Self, Worker<S, Req>)
where
S: Service<Req, Future = F> + Send + 'static,
F: Send,
S::Error: Into<crate::BoxError> + Send + Sync,
Req: Send + 'static,
{
assert!(bound > 0, "buffer bound must be greater than zero");
let (tx, rx) = mpsc::channel(bound);
let (handle, worker) = Worker::new(service, rx);
let buffer = Self {
Expand Down
17 changes: 17 additions & 0 deletions tower/tests/buffer/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,3 +457,20 @@ fn new_service_with_bound(bound: usize) -> (mock::Spawn<MockBuffer>, Handle) {
svc
})
}

#[test]
#[should_panic(expected = "buffer bound must be greater than zero")]
fn buffer_new_zero_bound_panics() {
let (service, _handle) = mock::pair::<(), ()>();
let _ = Buffer::new(service, 0);
}

#[test]
#[should_panic(expected = "buffer bound must be greater than zero")]
fn buffer_layer_zero_bound_panics() {
use tower::Layer;

let (service, _handle) = mock::pair::<(), ()>();
let layer = tower::buffer::BufferLayer::new(0);
let _ = layer.layer(service);
}