Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Service readiness refactoring #447

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 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.1.0"
version = "4.0.0"
authors = ["ntex contributors <team@ntex.rs>"]
description = "ntex service"
keywords = ["network", "framework", "async", "futures"]
Expand All @@ -17,6 +17,8 @@ path = "src/lib.rs"

[dependencies]
slab = "0.4"
bitflags = "2"
pin-project-lite = "0.2"

[dev-dependencies]
ntex = "2"
Expand Down
6 changes: 4 additions & 2 deletions ntex-service/src/and_then.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::future::Future;

use super::{util, Service, ServiceCtx, ServiceFactory};

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -26,8 +28,8 @@
type Error = A::Error;

#[inline]
async fn ready(&self, ctx: ServiceCtx<'_, Self>) -> Result<(), Self::Error> {
util::ready(&self.svc1, &self.svc2, ctx).await
async fn ready(&self) -> Option<impl Future<Output = Result<(), Self::Error>>> {
util::ready(&self.svc1, &self.svc2).await
}

#[inline]
Expand Down Expand Up @@ -94,7 +96,7 @@
type Response = &'static str;
type Error = ();

async fn ready(&self, _: ServiceCtx<'_, Self>) -> Result<(), Self::Error> {

Check failure on line 99 in ntex-service/src/and_then.rs

View workflow job for this annotation

GitHub Actions / Clippy

method `ready` has 2 parameters but the declaration in trait `Service::ready` has 1

Check failure on line 99 in ntex-service/src/and_then.rs

View workflow job for this annotation

GitHub Actions / stable - aarch64-apple-darwin

method `ready` has 2 parameters but the declaration in trait `Service::ready` has 1

Check failure on line 99 in ntex-service/src/and_then.rs

View workflow job for this annotation

GitHub Actions / nightly - aarch64-apple-darwin

method `ready` has 2 parameters but the declaration in trait `Service::ready` has 1

Check failure on line 99 in ntex-service/src/and_then.rs

View workflow job for this annotation

GitHub Actions / nightly - x86_64-unknown-linux-gnu

method `ready` has 2 parameters but the declaration in trait `Service::ready` has 1

Check failure on line 99 in ntex-service/src/and_then.rs

View workflow job for this annotation

GitHub Actions / stable - x86_64-unknown-linux-gnu

method `ready` has 2 parameters but the declaration in trait `Service::ready` has 1

Check failure on line 99 in ntex-service/src/and_then.rs

View workflow job for this annotation

GitHub Actions / 1.75.0 - x86_64-unknown-linux-gnu

method `ready` has 2 parameters but the declaration in trait `Service::ready` has 1

Check failure on line 99 in ntex-service/src/and_then.rs

View workflow job for this annotation

GitHub Actions / coverage

method `ready` has 2 parameters but the declaration in trait `Service::ready` has 1

Check failure on line 99 in ntex-service/src/and_then.rs

View workflow job for this annotation

GitHub Actions / coverage

method `ready` has 2 parameters but the declaration in trait `Service::ready` has 1

Check failure on line 99 in ntex-service/src/and_then.rs

View workflow job for this annotation

GitHub Actions / nightly - x86_64-pc-windows-msvc

method `ready` has 2 parameters but the declaration in trait `Service::ready` has 1

Check failure on line 99 in ntex-service/src/and_then.rs

View workflow job for this annotation

GitHub Actions / stable - x86_64-pc-windows-msvc

method `ready` has 2 parameters but the declaration in trait `Service::ready` has 1
self.0.set(self.0.get() + 1);
Ok(())
}
Expand All @@ -119,7 +121,7 @@
type Response = (&'static str, &'static str);
type Error = ();

async fn ready(&self, _: ServiceCtx<'_, Self>) -> Result<(), Self::Error> {

Check failure on line 124 in ntex-service/src/and_then.rs

View workflow job for this annotation

GitHub Actions / Clippy

method `ready` has 2 parameters but the declaration in trait `Service::ready` has 1

Check failure on line 124 in ntex-service/src/and_then.rs

View workflow job for this annotation

GitHub Actions / stable - aarch64-apple-darwin

method `ready` has 2 parameters but the declaration in trait `Service::ready` has 1

Check failure on line 124 in ntex-service/src/and_then.rs

View workflow job for this annotation

GitHub Actions / nightly - aarch64-apple-darwin

method `ready` has 2 parameters but the declaration in trait `Service::ready` has 1

Check failure on line 124 in ntex-service/src/and_then.rs

View workflow job for this annotation

GitHub Actions / nightly - x86_64-unknown-linux-gnu

method `ready` has 2 parameters but the declaration in trait `Service::ready` has 1

Check failure on line 124 in ntex-service/src/and_then.rs

View workflow job for this annotation

GitHub Actions / stable - x86_64-unknown-linux-gnu

method `ready` has 2 parameters but the declaration in trait `Service::ready` has 1

Check failure on line 124 in ntex-service/src/and_then.rs

View workflow job for this annotation

GitHub Actions / 1.75.0 - x86_64-unknown-linux-gnu

method `ready` has 2 parameters but the declaration in trait `Service::ready` has 1

Check failure on line 124 in ntex-service/src/and_then.rs

View workflow job for this annotation

GitHub Actions / coverage

method `ready` has 2 parameters but the declaration in trait `Service::ready` has 1

Check failure on line 124 in ntex-service/src/and_then.rs

View workflow job for this annotation

GitHub Actions / coverage

method `ready` has 2 parameters but the declaration in trait `Service::ready` has 1

Check failure on line 124 in ntex-service/src/and_then.rs

View workflow job for this annotation

GitHub Actions / nightly - x86_64-pc-windows-msvc

method `ready` has 2 parameters but the declaration in trait `Service::ready` has 1

Check failure on line 124 in ntex-service/src/and_then.rs

View workflow job for this annotation

GitHub Actions / stable - x86_64-pc-windows-msvc

method `ready` has 2 parameters but the declaration in trait `Service::ready` has 1
self.0.set(self.0.get() + 1);
Ok(())
}
Expand Down Expand Up @@ -148,7 +150,7 @@
)
.into_pipeline();
let res = srv.ready().await;
assert_eq!(res, Ok(()));

Check failure on line 153 in ntex-service/src/and_then.rs

View workflow job for this annotation

GitHub Actions / Clippy

binary operation `==` cannot be applied to type `Option<impl Future<Output = Result<(), <and_then::AndThen<and_then::tests::Srv1, and_then::tests::Srv2> as Service<&'static str>>::Error>>>`

Check failure on line 153 in ntex-service/src/and_then.rs

View workflow job for this annotation

GitHub Actions / stable - aarch64-apple-darwin

binary operation `==` cannot be applied to type `Option<impl Future<Output = Result<(), <and_then::AndThen<and_then::tests::Srv1, and_then::tests::Srv2> as Service<&'static str>>::Error>>>`

Check failure on line 153 in ntex-service/src/and_then.rs

View workflow job for this annotation

GitHub Actions / nightly - aarch64-apple-darwin

binary operation `==` cannot be applied to type `Option<impl Future<Output = Result<(), <and_then::AndThen<and_then::tests::Srv1, and_then::tests::Srv2> as Service<&'static str>>::Error>>>`

Check failure on line 153 in ntex-service/src/and_then.rs

View workflow job for this annotation

GitHub Actions / nightly - x86_64-unknown-linux-gnu

binary operation `==` cannot be applied to type `Option<impl Future<Output = Result<(), <and_then::AndThen<and_then::tests::Srv1, and_then::tests::Srv2> as Service<&'static str>>::Error>>>`

Check failure on line 153 in ntex-service/src/and_then.rs

View workflow job for this annotation

GitHub Actions / stable - x86_64-unknown-linux-gnu

binary operation `==` cannot be applied to type `Option<impl Future<Output = Result<(), <and_then::AndThen<and_then::tests::Srv1, and_then::tests::Srv2> as Service<&'static str>>::Error>>>`

Check failure on line 153 in ntex-service/src/and_then.rs

View workflow job for this annotation

GitHub Actions / 1.75.0 - x86_64-unknown-linux-gnu

binary operation `==` cannot be applied to type `Option<impl Future<Output = Result<(), <and_then::AndThen<and_then::tests::Srv1, and_then::tests::Srv2> as Service<&'static str>>::Error>>>`

Check failure on line 153 in ntex-service/src/and_then.rs

View workflow job for this annotation

GitHub Actions / coverage

binary operation `==` cannot be applied to type `Option<impl Future<Output = Result<(), <and_then::AndThen<and_then::tests::Srv1, and_then::tests::Srv2> as Service<&'static str>>::Error>>>`

Check failure on line 153 in ntex-service/src/and_then.rs

View workflow job for this annotation

GitHub Actions / coverage

binary operation `==` cannot be applied to type `Option<impl Future<Output = Result<(), <and_then::AndThen<and_then::tests::Srv1, and_then::tests::Srv2> as Service<&'static str>>::Error>>>`

Check failure on line 153 in ntex-service/src/and_then.rs

View workflow job for this annotation

GitHub Actions / nightly - x86_64-pc-windows-msvc

binary operation `==` cannot be applied to type `Option<impl Future<Output = Result<(), <and_then::AndThen<and_then::tests::Srv1, and_then::tests::Srv2> as Service<&'static str>>::Error>>>`

Check failure on line 153 in ntex-service/src/and_then.rs

View workflow job for this annotation

GitHub Actions / stable - x86_64-pc-windows-msvc

binary operation `==` cannot be applied to type `Option<impl Future<Output = Result<(), <and_then::AndThen<and_then::tests::Srv1, and_then::tests::Srv2> as Service<&'static str>>::Error>>>`
assert_eq!(cnt.get(), 2);
srv.shutdown().await;
assert_eq!(cnt_sht.get(), 2);
Expand Down
7 changes: 5 additions & 2 deletions ntex-service/src/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,11 @@ where
type Error = Err;

#[inline]
async fn ready(&self, _: ServiceCtx<'_, Self>) -> Result<(), Err> {
self.service.ready().await.map_err(From::from)
async fn ready(&self) -> Option<impl Future<Output = Result<(), Err>>> {
self.service
.ready()
.await
.map(|fut| async move { fut.await.map_err(From::from) })
}

#[inline]
Expand Down
31 changes: 13 additions & 18 deletions ntex-service/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{fmt, future::Future, pin::Pin};
use crate::ctx::{ServiceCtx, WaitersRef};

type BoxFuture<'a, I, E> = Pin<Box<dyn Future<Output = Result<I, E>> + 'a>>;
type BoxFutureOne<'a, T> = Pin<Box<dyn Future<Output = T> + 'a>>;
pub struct BoxService<Req, Res, Err>(Box<dyn ServiceObj<Req, Response = Res, Error = Err>>);
pub struct BoxServiceFactory<Cfg, Req, Res, Err, InitErr>(
Box<dyn ServiceFactoryObj<Req, Cfg, Response = Res, Error = Err, InitError = InitErr>>,
Expand Down Expand Up @@ -48,16 +49,12 @@ trait ServiceObj<Req> {
type Response;
type Error;

fn ready<'a>(
&'a self,
idx: usize,
waiters: &'a WaitersRef,
) -> BoxFuture<'a, (), Self::Error>;
fn ready<'a>(&'a self) -> BoxFutureOne<'a, Option<BoxFuture<'a, (), Self::Error>>>;

fn call<'a>(
&'a self,
req: Req,
idx: usize,
idx: u32,
waiters: &'a WaitersRef,
) -> BoxFuture<'a, Self::Response, Self::Error>;

Expand All @@ -73,15 +70,14 @@ where
type Error = S::Error;

#[inline]
fn ready<'a>(
&'a self,
idx: usize,
waiters: &'a WaitersRef,
) -> BoxFuture<'a, (), Self::Error> {
fn ready<'a>(&'a self) -> BoxFutureOne<'a, Option<BoxFuture<'a, (), Self::Error>>> {
Box::pin(async move {
ServiceCtx::<'a, S>::from_ref(idx, waiters)
.ready(self)
.await
if let Some(fut) = self.ready().await {
let r: BoxFuture<'a, (), Self::Error> = Box::pin(fut);
Some(r)
} else {
None
}
})
}

Expand All @@ -94,7 +90,7 @@ where
fn call<'a>(
&'a self,
req: Req,
idx: usize,
idx: u32,
waiters: &'a WaitersRef,
) -> BoxFuture<'a, Self::Response, Self::Error> {
Box::pin(async move {
Expand Down Expand Up @@ -150,9 +146,8 @@ where
type Error = Err;

#[inline]
async fn ready(&self, ctx: ServiceCtx<'_, Self>) -> Result<(), Self::Error> {
let (idx, waiters) = ctx.inner();
self.0.ready(idx, waiters).await
async fn ready(&self) -> Option<impl Future<Output = Result<(), Self::Error>>> {
self.0.ready().await
}

#[inline]
Expand Down
Loading
Loading