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