1
0
mirror of https://github.com/fafhrd91/actix-net synced 2025-01-19 21:04:40 +01:00
actix-net/src/inflight.rs

138 lines
3.3 KiB
Rust
Raw Normal View History

2018-12-06 14:04:42 -08:00
use futures::{try_ready, Async, Future, Poll};
2018-09-14 13:30:29 -07:00
use super::counter::{Counter, CounterGuard};
use super::service::{IntoNewService, IntoService, NewService, Service};
/// InFlight - new service for service that can limit number of in-flight
/// async requests.
///
/// Default number of in-flight requests is 15
pub struct InFlight<T> {
factory: T,
max_inflight: usize,
}
2018-11-29 16:56:15 -10:00
impl<T> InFlight<T> {
pub fn new<F, Request>(factory: F) -> Self
where
T: NewService<Request>,
F: IntoNewService<T, Request>,
{
2018-09-14 13:30:29 -07:00
Self {
factory: factory.into_new_service(),
max_inflight: 15,
}
}
/// Set max number of in-flight requests.
///
/// By default max in-flight requests is 15.
pub fn max_inflight(mut self, max: usize) -> Self {
self.max_inflight = max;
self
}
}
2018-11-29 16:56:15 -10:00
impl<T, Request> NewService<Request> for InFlight<T>
2018-09-14 13:30:29 -07:00
where
2018-11-29 16:56:15 -10:00
T: NewService<Request>,
2018-09-14 13:30:29 -07:00
{
type Response = T::Response;
type Error = T::Error;
type InitError = T::InitError;
type Service = InFlightService<T::Service>;
2018-11-29 16:56:15 -10:00
type Future = InFlightResponseFuture<T, Request>;
2018-09-14 13:30:29 -07:00
fn new_service(&self) -> Self::Future {
InFlightResponseFuture {
fut: self.factory.new_service(),
max_inflight: self.max_inflight,
}
}
}
2018-11-29 16:56:15 -10:00
pub struct InFlightResponseFuture<T: NewService<Request>, Request> {
2018-09-14 13:30:29 -07:00
fut: T::Future,
max_inflight: usize,
}
2018-11-29 16:56:15 -10:00
impl<T: NewService<Request>, Request> Future for InFlightResponseFuture<T, Request> {
2018-09-14 13:30:29 -07:00
type Item = InFlightService<T::Service>;
type Error = T::InitError;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
Ok(Async::Ready(InFlightService::with_max_inflight(
self.max_inflight,
try_ready!(self.fut.poll()),
)))
}
}
pub struct InFlightService<T> {
service: T,
count: Counter,
}
2018-11-29 16:56:15 -10:00
impl<T> InFlightService<T> {
pub fn new<F, Request>(service: F) -> Self
where
T: Service<Request>,
F: IntoService<T, Request>,
{
2018-09-14 13:30:29 -07:00
Self {
service: service.into_service(),
count: Counter::new(15),
}
}
2018-11-29 16:56:15 -10:00
pub fn with_max_inflight<F, Request>(max: usize, service: F) -> Self
where
T: Service<Request>,
F: IntoService<T, Request>,
{
2018-09-14 13:30:29 -07:00
Self {
service: service.into_service(),
count: Counter::new(max),
}
}
}
2018-11-29 16:56:15 -10:00
impl<T, Request> Service<Request> for InFlightService<T>
where
T: Service<Request>,
{
2018-09-14 13:30:29 -07:00
type Response = T::Response;
type Error = T::Error;
2018-11-29 16:56:15 -10:00
type Future = InFlightServiceResponse<T, Request>;
2018-09-14 13:30:29 -07:00
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
let res = self.service.poll_ready();
if res.is_ok() && !self.count.available() {
return Ok(Async::NotReady);
}
res
}
2018-11-29 16:56:15 -10:00
fn call(&mut self, req: Request) -> Self::Future {
2018-09-14 13:30:29 -07:00
InFlightServiceResponse {
fut: self.service.call(req),
2018-11-29 16:56:15 -10:00
_guard: self.count.get(),
2018-09-14 13:30:29 -07:00
}
}
}
#[doc(hidden)]
2018-11-29 16:56:15 -10:00
pub struct InFlightServiceResponse<T: Service<Request>, Request> {
2018-09-14 13:30:29 -07:00
fut: T::Future,
2018-11-29 16:56:15 -10:00
_guard: CounterGuard,
2018-09-14 13:30:29 -07:00
}
2018-11-29 16:56:15 -10:00
impl<T: Service<Request>, Request> Future for InFlightServiceResponse<T, Request> {
2018-09-14 13:30:29 -07:00
type Item = T::Response;
type Error = T::Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
self.fut.poll()
}
}