Promote service_fn to tower crate (#248)
This commit is contained in:
parent
23ce9133b5
commit
d1c891d1ba
|
@ -6,9 +6,7 @@ use hdrsample::Histogram;
|
|||
use rand::{self, Rng};
|
||||
use std::time::{Duration, Instant};
|
||||
use tokio::{runtime, timer};
|
||||
use tower::{
|
||||
discover::Discover, limit::concurrency::ConcurrencyLimit, util::ServiceFn, Service, ServiceExt,
|
||||
};
|
||||
use tower::{discover::Discover, limit::concurrency::ConcurrencyLimit, Service, ServiceExt};
|
||||
use tower_balance as lb;
|
||||
|
||||
const REQUESTS: usize = 50_000;
|
||||
|
@ -124,7 +122,7 @@ fn gen_disco() -> impl Discover<
|
|||
.zip(WEIGHTS.iter())
|
||||
.enumerate()
|
||||
.map(|(instance, (latency, weight))| {
|
||||
let svc = ServiceFn::new(move |_| {
|
||||
let svc = tower::service_fn(move |_| {
|
||||
let start = Instant::now();
|
||||
|
||||
let maxms = u64::from(latency.subsec_nanos() / 1_000 / 1_000)
|
||||
|
|
|
@ -20,37 +20,15 @@
|
|||
//! # use futures::*;
|
||||
//! # use futures::future::FutureResult;
|
||||
//! # use tower_service::Service;
|
||||
//! # use tower_util::BoxService;
|
||||
//! // Respond to requests using a closure. Since closures cannot be named,
|
||||
//! // `ServiceFn` cannot be named either
|
||||
//! pub struct ServiceFn<F> {
|
||||
//! f: F,
|
||||
//! }
|
||||
//! # use tower_util::{BoxService, service_fn};
|
||||
//! // Respond to requests using a closure, but closures cannot be named...
|
||||
//! # pub fn main() {
|
||||
//! let svc = service_fn(|mut request: String| {
|
||||
//! request.push_str(" response");
|
||||
//! Ok(request)
|
||||
//! });
|
||||
//!
|
||||
//! impl<F> Service<String> for ServiceFn<F>
|
||||
//! where F: Fn(String) -> String,
|
||||
//! {
|
||||
//! type Response = String;
|
||||
//! type Error = ();
|
||||
//! type Future = FutureResult<String, ()>;
|
||||
//!
|
||||
//! fn poll_ready(&mut self) -> Poll<(), ()> {
|
||||
//! Ok(().into())
|
||||
//! }
|
||||
//!
|
||||
//! fn call(&mut self, request: String) -> FutureResult<String, ()> {
|
||||
//! future::ok((self.f)(request))
|
||||
//! }
|
||||
//! }
|
||||
//!
|
||||
//! pub fn main() {
|
||||
//! let f = |mut request: String| {
|
||||
//! request.push_str(" response");
|
||||
//! request
|
||||
//! };
|
||||
//!
|
||||
//! let service: BoxService<String, String, ()> =
|
||||
//! BoxService::new(ServiceFn { f });
|
||||
//! let service: BoxService<String, String, ()> = BoxService::new(svc);
|
||||
//! # drop(service);
|
||||
//! }
|
||||
//! ```
|
||||
|
|
|
@ -25,7 +25,7 @@ pub use crate::{
|
|||
oneshot::Oneshot,
|
||||
optional::Optional,
|
||||
ready::Ready,
|
||||
service_fn::ServiceFn,
|
||||
service_fn::{service_fn, ServiceFn},
|
||||
};
|
||||
|
||||
pub mod error {
|
||||
|
|
|
@ -1,19 +1,17 @@
|
|||
use futures::{IntoFuture, Poll};
|
||||
use tower_service::Service;
|
||||
|
||||
/// Returns a new `ServiceFn` with the given closure.
|
||||
pub fn service_fn<T>(f: T) -> ServiceFn<T> {
|
||||
ServiceFn { f }
|
||||
}
|
||||
|
||||
/// A `Service` implemented by a closure.
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub struct ServiceFn<T> {
|
||||
f: T,
|
||||
}
|
||||
|
||||
impl<T> ServiceFn<T> {
|
||||
/// Returns a new `ServiceFn` with the given closure.
|
||||
pub fn new(f: T) -> Self {
|
||||
ServiceFn { f }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, F, Request> Service<Request> for ServiceFn<T>
|
||||
where
|
||||
T: Fn(Request) -> F,
|
||||
|
|
|
@ -19,4 +19,4 @@ pub mod util;
|
|||
|
||||
pub use crate::{builder::ServiceBuilder, util::ServiceExt};
|
||||
pub use tower_service::Service;
|
||||
pub use tower_util::{MakeConnection, MakeService};
|
||||
pub use tower_util::{service_fn, MakeConnection, MakeService};
|
||||
|
|
|
@ -3,8 +3,7 @@
|
|||
use futures::Stream;
|
||||
use tower_service::Service;
|
||||
pub use tower_util::{
|
||||
BoxService, CallAll, CallAllUnordered, Either, Oneshot, Optional, Ready, ServiceFn,
|
||||
UnsyncBoxService,
|
||||
BoxService, CallAll, CallAllUnordered, Either, Oneshot, Optional, Ready, UnsyncBoxService,
|
||||
};
|
||||
|
||||
impl<T: ?Sized, Request> ServiceExt<Request> for T where T: Service<Request> {}
|
||||
|
|
Loading…
Reference in New Issue