Skip to content

Commit

Permalink
Added Service::poll() method (#481)
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 authored Dec 4, 2024
1 parent 80d20e4 commit e33149d
Show file tree
Hide file tree
Showing 42 changed files with 229 additions and 391 deletions.
4 changes: 4 additions & 0 deletions ntex-io/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changes

## [2.9.0] - 2024-12-04

* Use updated Service trait

## [2.8.3] - 2024-11-10

* Check service readiness once per decoded item
Expand Down
6 changes: 3 additions & 3 deletions ntex-io/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ntex-io"
version = "2.8.3"
version = "2.9.0"
authors = ["ntex contributors <[email protected]>"]
description = "Utilities for encoding and decoding frames"
keywords = ["network", "framework", "async", "futures"]
Expand All @@ -18,8 +18,8 @@ path = "src/lib.rs"
[dependencies]
ntex-codec = "0.6"
ntex-bytes = "0.1"
ntex-util = "2.5"
ntex-service = "3.3.3"
ntex-util = "2.8"
ntex-service = "3.4"
ntex-rt = "0.4"

bitflags = "2"
Expand Down
35 changes: 1 addition & 34 deletions ntex-io/src/dispatcher.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Framed transport dispatcher
#![allow(clippy::let_underscore_future)]
use std::task::{ready, Context, Poll};
use std::{cell::Cell, future::poll_fn, future::Future, pin::Pin, rc::Rc};
use std::{cell::Cell, future::Future, pin::Pin, rc::Rc};

use ntex_codec::{Decoder, Encoder};
use ntex_service::{IntoService, Pipeline, PipelineBinding, PipelineCall, Service};
Expand Down Expand Up @@ -131,7 +131,6 @@ bitflags::bitflags! {
const KA_ENABLED = 0b0000100;
const KA_TIMEOUT = 0b0001000;
const READ_TIMEOUT = 0b0010000;
const READY_TASK = 0b1000000;
}
}

Expand Down Expand Up @@ -284,12 +283,6 @@ where
}
}

// ready task
if slf.flags.contains(Flags::READY_TASK) {
slf.flags.insert(Flags::READY_TASK);
ntex_rt::spawn(not_ready(slf.shared.clone()));
}

loop {
match slf.st {
DispatcherState::Processing => {
Expand Down Expand Up @@ -628,30 +621,6 @@ where
}
}

async fn not_ready<S, U>(slf: Rc<DispatcherShared<S, U>>)
where
S: Service<DispatchItem<U>, Response = Option<Response<U>>> + 'static,
U: Encoder + Decoder + 'static,
{
let pl = slf.service.clone();
loop {
if !pl.is_shutdown() {
if let Err(err) = poll_fn(|cx| pl.poll_ready(cx)).await {
log::trace!("{}: Service readiness check failed, stopping", slf.io.tag());
slf.error.set(Some(DispatcherError::Service(err)));
break;
}
if !pl.is_shutdown() {
poll_fn(|cx| pl.poll_not_ready(cx)).await;
slf.ready.set(false);
slf.io.wake();
continue;
}
}
break;
}
}

#[cfg(test)]
mod tests {
use std::sync::{atomic::AtomicBool, atomic::Ordering::Relaxed, Arc, Mutex};
Expand Down Expand Up @@ -902,8 +871,6 @@ mod tests {
Err("test")
}

async fn not_ready(&self) {}

async fn call(
&self,
_: DispatchItem<BytesCodec>,
Expand Down
4 changes: 4 additions & 0 deletions ntex-server/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changes

## [2.6.0] - 2024-12-04

* Use updated Service trait

## [2.5.0] - 2024-11-04

* Use updated Service trait
Expand Down
6 changes: 3 additions & 3 deletions ntex-server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ntex-server"
version = "2.5.0"
version = "2.6.0"
authors = ["ntex contributors <[email protected]>"]
description = "Server for ntex framework"
keywords = ["network", "framework", "async", "futures"]
Expand All @@ -18,9 +18,9 @@ path = "src/lib.rs"
[dependencies]
ntex-bytes = "0.1"
ntex-net = "2"
ntex-service = "3.3"
ntex-service = "3.4"
ntex-rt = "0.4"
ntex-util = "2.5"
ntex-util = "2.8"

async-channel = "2"
async-broadcast = "0.7"
Expand Down
26 changes: 5 additions & 21 deletions ntex-server/src/net/service.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fmt, future::poll_fn, future::Future, pin::Pin, task::Poll};
use std::{fmt, task::Context};

use ntex_bytes::{Pool, PoolRef};
use ntex_net::Io;
Expand Down Expand Up @@ -170,27 +170,11 @@ impl Service<Connection> for StreamServiceImpl {
}

#[inline]
async fn not_ready(&self) {
if self.conns.is_available() {
let mut futs: Vec<_> = self
.services
.iter()
.map(|s| Box::pin(s.not_ready()))
.collect();

ntex_util::future::select(
self.conns.unavailable(),
poll_fn(move |cx| {
for f in &mut futs {
if Pin::new(f).poll(cx).is_ready() {
return Poll::Ready(());
}
}
Poll::Pending
}),
)
.await;
fn poll(&self, cx: &mut Context<'_>) -> Result<(), Self::Error> {
for svc in &self.services {
svc.poll(cx)?;
}
Ok(())
}

async fn shutdown(&self) {
Expand Down
4 changes: 4 additions & 0 deletions ntex-service/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changes

## [3.4.0] - 2024-12-04

* Added Service::poll() method

## [3.3.3] - 2024-11-10

* Add Pipeline::is_shutdown() helper
Expand Down
2 changes: 1 addition & 1 deletion ntex-service/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ntex-service"
version = "3.3.3"
version = "3.4.0"
authors = ["ntex contributors <[email protected]>"]
description = "ntex service"
keywords = ["network", "framework", "async", "futures"]
Expand Down
23 changes: 10 additions & 13 deletions ntex-service/src/and_then.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ where
}

#[inline]
async fn not_ready(&self) {
util::select(self.svc1.not_ready(), self.svc2.not_ready()).await
fn poll(&self, cx: &mut std::task::Context<'_>) -> Result<(), Self::Error> {
self.svc1.poll(cx)?;
self.svc2.poll(cx)
}

#[inline]
Expand Down Expand Up @@ -88,8 +89,8 @@ where

#[cfg(test)]
mod tests {
use ntex_util::time;
use std::{cell::Cell, rc::Rc};
use ntex_util::future::lazy;
use std::{cell::Cell, rc::Rc, task::Context};

use crate::{chain, chain_factory, fn_factory, Service, ServiceCtx};

Expand All @@ -105,9 +106,9 @@ mod tests {
Ok(())
}

async fn not_ready(&self) {
fn poll(&self, _: &mut Context<'_>) -> Result<(), Self::Error> {
self.0.set(self.0.get() + 1);
std::future::pending().await
Ok(())
}

async fn call(
Expand Down Expand Up @@ -135,9 +136,9 @@ mod tests {
Ok(())
}

async fn not_ready(&self) {
fn poll(&self, _: &mut Context<'_>) -> Result<(), Self::Error> {
self.0.set(self.0.get() + 1);
std::future::pending().await
Ok(())
}

async fn call(
Expand Down Expand Up @@ -165,11 +166,7 @@ mod tests {
assert_eq!(res, Ok(()));
assert_eq!(cnt.get(), 2);

let srv2 = srv.clone();
ntex::rt::spawn(async move {
let _ = srv2.not_ready().await;
});
time::sleep(time::Millis(25)).await;
lazy(|cx| srv.clone().poll(cx)).await.unwrap();
assert_eq!(cnt.get(), 4);

srv.shutdown().await;
Expand Down
10 changes: 6 additions & 4 deletions ntex-service/src/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ where
(self.f)(req, self.service.clone()).await
}

crate::forward_notready!(service);
crate::forward_poll!(service);
crate::forward_shutdown!(service);
}

Expand Down Expand Up @@ -205,7 +205,8 @@ where

#[cfg(test)]
mod tests {
use std::{cell::Cell, rc::Rc};
use ntex_util::future::lazy;
use std::{cell::Cell, rc::Rc, task::Context};

use super::*;
use crate::{chain, chain_factory, fn_factory};
Expand All @@ -221,8 +222,9 @@ mod tests {
Ok(())
}

async fn not_ready(&self) {
fn poll(&self, _: &mut Context<'_>) -> Result<(), Self::Error> {
self.0.set(self.0.get() + 1);
Ok(())
}

async fn shutdown(&self) {
Expand Down Expand Up @@ -253,7 +255,7 @@ mod tests {

assert_eq!(srv.ready().await, Ok::<_, Err>(()));

srv.not_ready().await;
lazy(|cx| srv.poll(cx)).await.unwrap();
assert_eq!(cnt_sht.get(), 1);

srv.shutdown().await;
Expand Down
26 changes: 13 additions & 13 deletions ntex-service/src/boxed.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fmt, future::Future, pin::Pin};
use std::{fmt, future::Future, pin::Pin, task::Context};

use crate::ctx::{ServiceCtx, WaitersRef};

Expand Down Expand Up @@ -54,8 +54,6 @@ trait ServiceObj<Req> {
waiters: &'a WaitersRef,
) -> BoxFuture<'a, (), Self::Error>;

fn not_ready<'a>(&'a self) -> Pin<Box<dyn Future<Output = ()> + 'a>>;

fn call<'a>(
&'a self,
req: Req,
Expand All @@ -64,6 +62,8 @@ trait ServiceObj<Req> {
) -> BoxFuture<'a, Self::Response, Self::Error>;

fn shutdown<'a>(&'a self) -> Pin<Box<dyn Future<Output = ()> + 'a>>;

fn poll(&self, cx: &mut Context<'_>) -> Result<(), Self::Error>;
}

impl<S, Req> ServiceObj<Req> for S
Expand All @@ -83,11 +83,6 @@ where
Box::pin(async move { ServiceCtx::<'a, S>::new(idx, waiters).ready(self).await })
}

#[inline]
fn not_ready<'a>(&'a self) -> Pin<Box<dyn Future<Output = ()> + 'a>> {
Box::pin(crate::Service::not_ready(self))
}

#[inline]
fn shutdown<'a>(&'a self) -> Pin<Box<dyn Future<Output = ()> + 'a>> {
Box::pin(crate::Service::shutdown(self))
Expand All @@ -106,6 +101,11 @@ where
.await
})
}

#[inline]
fn poll(&self, cx: &mut Context<'_>) -> Result<(), Self::Error> {
crate::Service::poll(self, cx)
}
}

trait ServiceFactoryObj<Req, Cfg> {
Expand Down Expand Up @@ -158,11 +158,6 @@ where
self.0.ready(idx, waiters).await
}

#[inline]
async fn not_ready(&self) {
self.0.not_ready().await
}

#[inline]
async fn shutdown(&self) {
self.0.shutdown().await
Expand All @@ -173,6 +168,11 @@ where
let (idx, waiters) = ctx.inner();
self.0.call(req, idx, waiters).await
}

#[inline]
fn poll(&self, cx: &mut Context<'_>) -> Result<(), Self::Error> {
self.0.poll(cx)
}
}

impl<C, Req, Res, Err, InitErr> crate::ServiceFactory<Req, C>
Expand Down
1 change: 1 addition & 0 deletions ntex-service/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ impl<Svc: Service<Req>, Req> Service<Req> for ServiceChain<Svc, Req> {
type Response = Svc::Response;
type Error = Svc::Error;

crate::forward_poll!(service);
crate::forward_ready!(service);
crate::forward_shutdown!(service);

Expand Down
15 changes: 0 additions & 15 deletions ntex-service/src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ impl WaitersRef {
pub(crate) fn new() -> (u32, Self) {
let mut waiters = slab::Slab::new();

// first insert for wake ups from services
let _ = waiters.insert(None);

(
waiters.insert(Default::default()) as u32,
WaitersRef {
Expand Down Expand Up @@ -68,18 +65,6 @@ impl WaitersRef {
self.get()[idx as usize] = Some(cx.waker().clone());
}

pub(crate) fn register_unready(&self, cx: &mut Context<'_>) {
self.get()[0] = Some(cx.waker().clone());
}

pub(crate) fn notify_unready(&self) {
if let Some(item) = self.get().get_mut(0) {
if let Some(waker) = item.take() {
waker.wake();
}
}
}

pub(crate) fn notify(&self) {
let wakers = self.get_wakers();
if !wakers.is_empty() {
Expand Down
Loading

0 comments on commit e33149d

Please sign in to comment.