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

Synchronized buffer #216

Merged
merged 8 commits into from
Jun 26, 2023
Merged
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: 4 additions & 0 deletions ntex-service/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changes

## [1.2.2] - 2023-06-24

* Added `ServiceCall::advance_to_call`

## [1.2.1] - 2023-06-23

* Make `PipelineCall` static
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 = "1.2.1"
version = "1.2.2"
authors = ["ntex contributors <team@ntex.rs>"]
description = "ntex service"
keywords = ["network", "framework", "async", "futures"]
Expand Down
82 changes: 82 additions & 0 deletions ntex-service/src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,26 @@
}
}

impl<'a, S, Req> ServiceCall<'a, S, Req>
where
S: Service<Req>,
S: 'a,
S: ?Sized,
Req: 'a,
{
pub fn advance_to_call(self) -> ServiceCallToCall<'a, S, Req> {
match self.state {
ServiceCallState::Ready { .. } => {}
ServiceCallState::Call { .. } | ServiceCallState::Empty => {
panic!(
"`ServiceCall::advance_to_call` must be called before `ServiceCall::poll`"
)

Check warning on line 182 in ntex-service/src/ctx.rs

View check run for this annotation

Codecov / codecov/patch

ntex-service/src/ctx.rs#L180-L182

Added lines #L180 - L182 were not covered by tests
}
}
ServiceCallToCall { state: self.state }
}
}

pin_project_lite::pin_project! {
#[project = ServiceCallStateProject]
enum ServiceCallState<'a, S, Req>
Expand Down Expand Up @@ -234,6 +254,68 @@
}
}

pin_project_lite::pin_project! {
#[must_use = "futures do nothing unless polled"]
pub struct ServiceCallToCall<'a, S, Req>
where
S: Service<Req>,
S: 'a,
S: ?Sized,
Req: 'a,
{
#[pin]
state: ServiceCallState<'a, S, Req>,
}
}

impl<'a, S, Req> Future for ServiceCallToCall<'a, S, Req>
where
S: Service<Req> + ?Sized,
{
type Output = Result<S::Future<'a>, S::Error>;

fn poll(
mut self: Pin<&mut Self>,
cx: &mut task::Context<'_>,
) -> task::Poll<Self::Output> {
let mut this = self.as_mut().project();

match this.state.as_mut().project() {
ServiceCallStateProject::Ready {
req,
svc,
idx,
waiters,
} => match svc.poll_ready(cx)? {
task::Poll::Ready(()) => {
waiters.notify();

let fut = svc.call(
req.take().unwrap(),
ServiceCtx {
idx: *idx,
waiters,
_t: marker::PhantomData,
},
);
this.state.set(ServiceCallState::Empty);
task::Poll::Ready(Ok(fut))
}
task::Poll::Pending => {
waiters.register(*idx, cx);
task::Poll::Pending

Check warning on line 306 in ntex-service/src/ctx.rs

View check run for this annotation

Codecov / codecov/patch

ntex-service/src/ctx.rs#L305-L306

Added lines #L305 - L306 were not covered by tests
}
},
ServiceCallStateProject::Call { .. } => {
unreachable!("`ServiceCallToCall` can only be constructed in `Ready` state")

Check warning on line 310 in ntex-service/src/ctx.rs

View check run for this annotation

Codecov / codecov/patch

ntex-service/src/ctx.rs#L310

Added line #L310 was not covered by tests
}
ServiceCallStateProject::Empty => {
panic!("future must not be polled after it returned `Poll::Ready`")

Check warning on line 313 in ntex-service/src/ctx.rs

View check run for this annotation

Codecov / codecov/patch

ntex-service/src/ctx.rs#L313

Added line #L313 was not covered by tests
}
}
}
}

#[cfg(test)]
mod tests {
use ntex_util::future::{lazy, poll_fn, Ready};
Expand Down
2 changes: 1 addition & 1 deletion ntex-service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ mod then;

pub use self::apply::{apply_fn, apply_fn_factory};
pub use self::chain::{chain, chain_factory};
pub use self::ctx::{ServiceCall, ServiceCtx};
pub use self::ctx::{ServiceCall, ServiceCallToCall, ServiceCtx};
pub use self::fn_service::{fn_factory, fn_factory_with_config, fn_service};
pub use self::fn_shutdown::fn_shutdown;
pub use self::map_config::{map_config, unit_config};
Expand Down
6 changes: 6 additions & 0 deletions ntex-util/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changes

## [0.3.1] - 2023-06-24

* Changed `BufferService` to maintain order

* Buffer error type changed to indicate cancellation

## [0.3.0] - 2023-06-22

* Release v0.3.0
Expand Down
4 changes: 2 additions & 2 deletions ntex-util/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ntex-util"
version = "0.3.0"
version = "0.3.1"
authors = ["ntex contributors <team@ntex.rs>"]
description = "Utilities for ntex framework"
keywords = ["network", "framework", "async", "futures"]
Expand All @@ -17,7 +17,7 @@ path = "src/lib.rs"

[dependencies]
ntex-rt = "0.4.7"
ntex-service = "1.2.0"
ntex-service = "1.2.2"
bitflags = "1.3"
fxhash = "0.2.1"
log = "0.4"
Expand Down
Loading
Loading