diff --git a/guides/building-a-middleware-from-scratch.md b/guides/building-a-middleware-from-scratch.md index 6bbfb66a6..d6a008148 100644 --- a/guides/building-a-middleware-from-scratch.md +++ b/guides/building-a-middleware-from-scratch.md @@ -74,7 +74,7 @@ where type Future = S::Future; fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { - // Our middleware doesn't care about backpressure so its ready as long + // Our middleware doesn't care about backpressure, so it's ready as long // as the inner service is ready. self.inner.poll_ready(cx) } @@ -192,9 +192,9 @@ where Ideally we want to write something like this: -1. First poll `self.response_future` and if its ready return the response or error it +1. First poll `self.response_future`, and if it's ready, return the response or error it resolved to. -2. Otherwise poll `self.sleep` and if its ready return an error. +2. Otherwise, poll `self.sleep`, and if it's ready, return an error. 3. If neither future is ready return `Poll::Pending`. We might try: diff --git a/tower/src/balance/p2c/service.rs b/tower/src/balance/p2c/service.rs index 81fe9a174..30572fc8d 100644 --- a/tower/src/balance/p2c/service.rs +++ b/tower/src/balance/p2c/service.rs @@ -5,16 +5,13 @@ use crate::ready_cache::{error::Failed, ReadyCache}; use crate::util::rng::{sample_floyd2, HasherRng, Rng}; use futures_core::ready; use futures_util::future::{self, TryFutureExt}; -use pin_project_lite::pin_project; use std::hash::Hash; use std::marker::PhantomData; use std::{ fmt, - future::Future, pin::Pin, task::{Context, Poll}, }; -use tokio::sync::oneshot; use tower_service::Service; use tracing::{debug, trace}; @@ -58,25 +55,6 @@ where } } -pin_project! { - /// A Future that becomes satisfied when an `S`-typed service is ready. - /// - /// May fail due to cancelation, i.e., if [`Discover`] removes the service from the service set. - struct UnreadyService { - key: Option, - #[pin] - cancel: oneshot::Receiver<()>, - service: Option, - - _req: PhantomData, - } -} - -enum Error { - Inner(E), - Canceled, -} - impl Balance where D: Discover, @@ -279,50 +257,3 @@ where .map_err(Into::into) } } - -impl, Req> Future for UnreadyService { - type Output = Result<(K, S), (K, Error)>; - - fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - let this = self.project(); - - if let Poll::Ready(Ok(())) = this.cancel.poll(cx) { - let key = this.key.take().expect("polled after ready"); - return Poll::Ready(Err((key, Error::Canceled))); - } - - let res = ready!(this - .service - .as_mut() - .expect("poll after ready") - .poll_ready(cx)); - - let key = this.key.take().expect("polled after ready"); - let svc = this.service.take().expect("polled after ready"); - - match res { - Ok(()) => Poll::Ready(Ok((key, svc))), - Err(e) => Poll::Ready(Err((key, Error::Inner(e)))), - } - } -} - -impl fmt::Debug for UnreadyService -where - K: fmt::Debug, - S: fmt::Debug, -{ - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let Self { - key, - cancel, - service, - _req, - } = self; - f.debug_struct("UnreadyService") - .field("key", key) - .field("cancel", cancel) - .field("service", service) - .finish() - } -} diff --git a/tower/src/retry/backoff.rs b/tower/src/retry/backoff.rs index 306723eda..685063ec3 100644 --- a/tower/src/retry/backoff.rs +++ b/tower/src/retry/backoff.rs @@ -1,4 +1,4 @@ -//! This module contains generic [backoff] utlities to be used with the retry +//! This module contains generic [backoff] utilities to be used with the retry //! layer. //! //! The [`Backoff`] trait is a generic way to represent backoffs that can use diff --git a/tower/src/retry/budget/tps_budget.rs b/tower/src/retry/budget/tps_budget.rs index 3f1d530bc..d6949980b 100644 --- a/tower/src/retry/budget/tps_budget.rs +++ b/tower/src/retry/budget/tps_budget.rs @@ -31,7 +31,7 @@ pub struct TpsBudget { slots: Box<[AtomicIsize]>, /// The amount of time represented by each slot. window: Duration, - /// The changers for the current slot to be commited + /// The changers for the current slot to be committed /// after the slot expires. writer: AtomicIsize, /// Amount of tokens to deposit for each put(). diff --git a/tower/src/util/rng.rs b/tower/src/util/rng.rs index d520ffce6..cf5932d60 100644 --- a/tower/src/util/rng.rs +++ b/tower/src/util/rng.rs @@ -3,7 +3,7 @@ //! This module provides a generic [`Rng`] trait and a [`HasherRng`] that //! implements the trait based on [`RandomState`] or any other [`Hasher`]. //! -//! These utlities replace tower's internal usage of `rand` with these smaller, +//! These utilities replace tower's internal usage of `rand` with these smaller, //! more lightweight methods. Most of the implementations are extracted from //! their corresponding `rand` implementations. //! @@ -111,7 +111,7 @@ where /// A sampler modified from the Rand implementation for use internally for the balance middleware. /// -/// It's an implemenetation of Floyd's combination algorithm. with amount fixed at 2. This uses no allocated +/// It's an implementation of Floyd's combination algorithm. with amount fixed at 2. This uses no allocated /// memory and finishes in constant time (only 2 random calls) /// /// ref: This was borrowed and modified from the following Rand implementation diff --git a/tower/tests/ready_cache/main.rs b/tower/tests/ready_cache/main.rs index 56be6a9cb..cdcfcbbf9 100644 --- a/tower/tests/ready_cache/main.rs +++ b/tower/tests/ready_cache/main.rs @@ -200,7 +200,7 @@ async fn cancelation_observed() { let mut handles = vec![]; // NOTE This test passes at 129 items, but fails at 130 items (if coop - // schedulding interferes with cancelation). + // scheduling interferes with cancelation). for _ in 0..130 { let (svc, mut handle) = tower_test::mock::pair::<(), ()>(); handle.allow(1);