mirror of
https://github.com/fafhrd91/actix-net
synced 2025-08-14 16:50:32 +02:00
Compare commits
50 Commits
tls-v2.0.0
...
rt-1.1.1
Author | SHA1 | Date | |
---|---|---|---|
|
343b3c09fc | ||
|
8a10580663 | ||
|
1b4a117063 | ||
|
700997fe48 | ||
|
4c5568ed70 | ||
|
7d0cfe1b4d | ||
|
e35c261c9f | ||
|
115ef3fcb3 | ||
|
c0482e2532 | ||
|
6906f25e01 | ||
|
06bca19524 | ||
|
e9e2185296 | ||
|
aae52a80ab | ||
|
65e2e8052e | ||
|
783880bb0a | ||
|
69e8df9d62 | ||
|
9addf1a36b | ||
|
187a58472d | ||
|
30aa0b7bb6 | ||
|
e775d08d76 | ||
|
d5f95b54b7 | ||
|
904f90abc2 | ||
|
950c73077c | ||
|
732731a9c8 | ||
|
0dd5a7ce1d | ||
|
7105091e51 | ||
|
08959dfc21 | ||
|
2792433ad6 | ||
|
437a7b05c6 | ||
|
3d125c5381 | ||
|
fbf7d6ef33 | ||
|
e6b6f08369 | ||
|
4e806b3e3f | ||
|
f5b07053fc | ||
|
dd3bec83bf | ||
|
f955e49930 | ||
|
4be11b541b | ||
|
baba533407 | ||
|
2bf50826b0 | ||
|
41b2a3b2e2 | ||
|
7fdd4a1118 | ||
|
cb30f9e86a | ||
|
873f69be51 | ||
|
0967061f30 | ||
|
59902cb3a3 | ||
|
857e50120b | ||
|
36a2edf1cd | ||
|
346bd072d3 | ||
|
8d3d58b3b7 | ||
|
c41b5d8dd4 |
5
.github/workflows/linux.yml
vendored
5
.github/workflows/linux.yml
vendored
@@ -59,16 +59,15 @@ jobs:
|
|||||||
args: --all --all-features --no-fail-fast -- --nocapture
|
args: --all --all-features --no-fail-fast -- --nocapture
|
||||||
|
|
||||||
- name: Generate coverage file
|
- name: Generate coverage file
|
||||||
if: matrix.version == 'stable' && github.ref == 'refs/heads/master'
|
if: matrix.version == 'stable' && (github.ref == 'refs/heads/master' || github.event_name == 'pull_request')
|
||||||
run: |
|
run: |
|
||||||
cargo install cargo-tarpaulin
|
cargo install cargo-tarpaulin
|
||||||
cargo tarpaulin --out Xml --workspace --all-features
|
cargo tarpaulin --out Xml --workspace --all-features
|
||||||
|
|
||||||
- name: Upload to Codecov
|
- name: Upload to Codecov
|
||||||
if: matrix.version == 'stable' && github.ref == 'refs/heads/master'
|
if: matrix.version == 'stable' && (github.ref == 'refs/heads/master' || github.event_name == 'pull_request')
|
||||||
uses: codecov/codecov-action@v1
|
uses: codecov/codecov-action@v1
|
||||||
with:
|
with:
|
||||||
token: ${{ secrets.CODECOV_TOKEN }}
|
|
||||||
file: cobertura.xml
|
file: cobertura.xml
|
||||||
|
|
||||||
- name: Clear the cargo caches
|
- name: Clear the cargo caches
|
||||||
|
@@ -19,8 +19,9 @@ path = "src/lib.rs"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
bitflags = "1.2.1"
|
bitflags = "1.2.1"
|
||||||
bytes = "0.5.2"
|
bytes = "0.5.2"
|
||||||
futures-core = "0.3.1"
|
futures-core = { version = "0.3.4", default-features = false }
|
||||||
futures-sink = "0.3.1"
|
futures-sink = { version = "0.3.4", default-features = false }
|
||||||
tokio = { version = "0.2.4", default-features=false }
|
tokio = { version = "0.2.4", default-features=false }
|
||||||
tokio-util = { version = "0.2.0", default-features=false, features=["codec"] }
|
tokio-util = { version = "0.2.0", default-features=false, features=["codec"] }
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
|
pin-project = "0.4.8"
|
||||||
|
@@ -5,6 +5,7 @@ use std::{fmt, io};
|
|||||||
use bytes::{Buf, BytesMut};
|
use bytes::{Buf, BytesMut};
|
||||||
use futures_core::{ready, Stream};
|
use futures_core::{ready, Stream};
|
||||||
use futures_sink::Sink;
|
use futures_sink::Sink;
|
||||||
|
use pin_project::pin_project;
|
||||||
|
|
||||||
use crate::{AsyncRead, AsyncWrite, Decoder, Encoder};
|
use crate::{AsyncRead, AsyncWrite, Decoder, Encoder};
|
||||||
|
|
||||||
@@ -20,7 +21,9 @@ bitflags::bitflags! {
|
|||||||
|
|
||||||
/// A unified `Stream` and `Sink` interface to an underlying I/O object, using
|
/// A unified `Stream` and `Sink` interface to an underlying I/O object, using
|
||||||
/// the `Encoder` and `Decoder` traits to encode and decode frames.
|
/// the `Encoder` and `Decoder` traits to encode and decode frames.
|
||||||
|
#[pin_project]
|
||||||
pub struct Framed<T, U> {
|
pub struct Framed<T, U> {
|
||||||
|
#[pin]
|
||||||
io: T,
|
io: T,
|
||||||
codec: U,
|
codec: U,
|
||||||
flags: Flags,
|
flags: Flags,
|
||||||
@@ -28,8 +31,6 @@ pub struct Framed<T, U> {
|
|||||||
write_buf: BytesMut,
|
write_buf: BytesMut,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, U> Unpin for Framed<T, U> {}
|
|
||||||
|
|
||||||
impl<T, U> Framed<T, U>
|
impl<T, U> Framed<T, U>
|
||||||
where
|
where
|
||||||
T: AsyncRead + AsyncWrite,
|
T: AsyncRead + AsyncWrite,
|
||||||
@@ -185,17 +186,18 @@ impl<T, U> Framed<T, U> {
|
|||||||
|
|
||||||
impl<T, U> Framed<T, U> {
|
impl<T, U> Framed<T, U> {
|
||||||
/// Serialize item and Write to the inner buffer
|
/// Serialize item and Write to the inner buffer
|
||||||
pub fn write(&mut self, item: <U as Encoder>::Item) -> Result<(), <U as Encoder>::Error>
|
pub fn write(mut self: Pin<&mut Self>, item: <U as Encoder>::Item) -> Result<(), <U as Encoder>::Error>
|
||||||
where
|
where
|
||||||
T: AsyncWrite,
|
T: AsyncWrite,
|
||||||
U: Encoder,
|
U: Encoder,
|
||||||
{
|
{
|
||||||
let remaining = self.write_buf.capacity() - self.write_buf.len();
|
let this = self.as_mut().project();
|
||||||
|
let remaining = this.write_buf.capacity() - this.write_buf.len();
|
||||||
if remaining < LW {
|
if remaining < LW {
|
||||||
self.write_buf.reserve(HW - remaining);
|
this.write_buf.reserve(HW - remaining);
|
||||||
}
|
}
|
||||||
|
|
||||||
self.codec.encode(item, &mut self.write_buf)?;
|
this.codec.encode(item, this.write_buf)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -207,21 +209,22 @@ impl<T, U> Framed<T, U> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Try to read underlying I/O stream and decode item.
|
/// Try to read underlying I/O stream and decode item.
|
||||||
pub fn next_item(&mut self, cx: &mut Context<'_>) -> Poll<Option<Result<U::Item, U::Error>>>
|
pub fn next_item(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Result<U::Item, U::Error>>>
|
||||||
where
|
where
|
||||||
T: AsyncRead,
|
T: AsyncRead,
|
||||||
U: Decoder,
|
U: Decoder,
|
||||||
{
|
{
|
||||||
loop {
|
loop {
|
||||||
|
let mut this = self.as_mut().project();
|
||||||
// Repeatedly call `decode` or `decode_eof` as long as it is
|
// Repeatedly call `decode` or `decode_eof` as long as it is
|
||||||
// "readable". Readable is defined as not having returned `None`. If
|
// "readable". Readable is defined as not having returned `None`. If
|
||||||
// the upstream has returned EOF, and the decoder is no longer
|
// the upstream has returned EOF, and the decoder is no longer
|
||||||
// readable, it can be assumed that the decoder will never become
|
// readable, it can be assumed that the decoder will never become
|
||||||
// readable again, at which point the stream is terminated.
|
// readable again, at which point the stream is terminated.
|
||||||
|
|
||||||
if self.flags.contains(Flags::READABLE) {
|
if this.flags.contains(Flags::READABLE) {
|
||||||
if self.flags.contains(Flags::EOF) {
|
if this.flags.contains(Flags::EOF) {
|
||||||
match self.codec.decode_eof(&mut self.read_buf) {
|
match this.codec.decode_eof(&mut this.read_buf) {
|
||||||
Ok(Some(frame)) => return Poll::Ready(Some(Ok(frame))),
|
Ok(Some(frame)) => return Poll::Ready(Some(Ok(frame))),
|
||||||
Ok(None) => return Poll::Ready(None),
|
Ok(None) => return Poll::Ready(None),
|
||||||
Err(e) => return Poll::Ready(Some(Err(e))),
|
Err(e) => return Poll::Ready(Some(Err(e))),
|
||||||
@@ -230,7 +233,7 @@ impl<T, U> Framed<T, U> {
|
|||||||
|
|
||||||
log::trace!("attempting to decode a frame");
|
log::trace!("attempting to decode a frame");
|
||||||
|
|
||||||
match self.codec.decode(&mut self.read_buf) {
|
match this.codec.decode(&mut this.read_buf) {
|
||||||
Ok(Some(frame)) => {
|
Ok(Some(frame)) => {
|
||||||
log::trace!("frame decoded from buffer");
|
log::trace!("frame decoded from buffer");
|
||||||
return Poll::Ready(Some(Ok(frame)));
|
return Poll::Ready(Some(Ok(frame)));
|
||||||
@@ -239,45 +242,44 @@ impl<T, U> Framed<T, U> {
|
|||||||
_ => (), // Need more data
|
_ => (), // Need more data
|
||||||
}
|
}
|
||||||
|
|
||||||
self.flags.remove(Flags::READABLE);
|
this.flags.remove(Flags::READABLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
debug_assert!(!self.flags.contains(Flags::EOF));
|
debug_assert!(!this.flags.contains(Flags::EOF));
|
||||||
|
|
||||||
// Otherwise, try to read more data and try again. Make sure we've got room
|
// Otherwise, try to read more data and try again. Make sure we've got room
|
||||||
let remaining = self.read_buf.capacity() - self.read_buf.len();
|
let remaining = this.read_buf.capacity() - this.read_buf.len();
|
||||||
if remaining < LW {
|
if remaining < LW {
|
||||||
self.read_buf.reserve(HW - remaining)
|
this.read_buf.reserve(HW - remaining)
|
||||||
}
|
}
|
||||||
let cnt = match unsafe {
|
let cnt = match this.io.poll_read_buf(cx, &mut this.read_buf) {
|
||||||
Pin::new_unchecked(&mut self.io).poll_read_buf(cx, &mut self.read_buf)
|
|
||||||
} {
|
|
||||||
Poll::Pending => return Poll::Pending,
|
Poll::Pending => return Poll::Pending,
|
||||||
Poll::Ready(Err(e)) => return Poll::Ready(Some(Err(e.into()))),
|
Poll::Ready(Err(e)) => return Poll::Ready(Some(Err(e.into()))),
|
||||||
Poll::Ready(Ok(cnt)) => cnt,
|
Poll::Ready(Ok(cnt)) => cnt,
|
||||||
};
|
};
|
||||||
|
|
||||||
if cnt == 0 {
|
if cnt == 0 {
|
||||||
self.flags.insert(Flags::EOF);
|
this.flags.insert(Flags::EOF);
|
||||||
}
|
}
|
||||||
self.flags.insert(Flags::READABLE);
|
this.flags.insert(Flags::READABLE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Flush write buffer to underlying I/O stream.
|
/// Flush write buffer to underlying I/O stream.
|
||||||
pub fn flush(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), U::Error>>
|
pub fn flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), U::Error>>
|
||||||
where
|
where
|
||||||
T: AsyncWrite,
|
T: AsyncWrite,
|
||||||
U: Encoder,
|
U: Encoder,
|
||||||
{
|
{
|
||||||
|
let mut this = self.as_mut().project();
|
||||||
log::trace!("flushing framed transport");
|
log::trace!("flushing framed transport");
|
||||||
|
|
||||||
while !self.write_buf.is_empty() {
|
while !this.write_buf.is_empty() {
|
||||||
log::trace!("writing; remaining={}", self.write_buf.len());
|
log::trace!("writing; remaining={}", this.write_buf.len());
|
||||||
|
|
||||||
let n = ready!(unsafe {
|
let n = ready!(
|
||||||
Pin::new_unchecked(&mut self.io).poll_write(cx, &self.write_buf)
|
this.io.as_mut().poll_write(cx, this.write_buf)
|
||||||
})?;
|
)?;
|
||||||
|
|
||||||
if n == 0 {
|
if n == 0 {
|
||||||
return Poll::Ready(Err(io::Error::new(
|
return Poll::Ready(Err(io::Error::new(
|
||||||
@@ -288,26 +290,25 @@ impl<T, U> Framed<T, U> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// remove written data
|
// remove written data
|
||||||
self.write_buf.advance(n);
|
this.write_buf.advance(n);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try flushing the underlying IO
|
// Try flushing the underlying IO
|
||||||
ready!(unsafe { Pin::new_unchecked(&mut self.io).poll_flush(cx) })?;
|
ready!(this.io.poll_flush(cx))?;
|
||||||
|
|
||||||
log::trace!("framed transport flushed");
|
log::trace!("framed transport flushed");
|
||||||
Poll::Ready(Ok(()))
|
Poll::Ready(Ok(()))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Flush write buffer and shutdown underlying I/O stream.
|
/// Flush write buffer and shutdown underlying I/O stream.
|
||||||
pub fn close(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), U::Error>>
|
pub fn close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), U::Error>>
|
||||||
where
|
where
|
||||||
T: AsyncWrite,
|
T: AsyncWrite,
|
||||||
U: Encoder,
|
U: Encoder,
|
||||||
{
|
{
|
||||||
unsafe {
|
let mut this = self.as_mut().project();
|
||||||
ready!(Pin::new_unchecked(&mut self.io).poll_flush(cx))?;
|
ready!(this.io.as_mut().poll_flush(cx))?;
|
||||||
ready!(Pin::new_unchecked(&mut self.io).poll_shutdown(cx))?;
|
ready!(this.io.as_mut().poll_shutdown(cx))?;
|
||||||
}
|
|
||||||
Poll::Ready(Ok(()))
|
Poll::Ready(Ok(()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -319,7 +320,7 @@ where
|
|||||||
{
|
{
|
||||||
type Item = Result<U::Item, U::Error>;
|
type Item = Result<U::Item, U::Error>;
|
||||||
|
|
||||||
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||||
self.next_item(cx)
|
self.next_item(cx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -341,21 +342,21 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn start_send(
|
fn start_send(
|
||||||
mut self: Pin<&mut Self>,
|
self: Pin<&mut Self>,
|
||||||
item: <U as Encoder>::Item,
|
item: <U as Encoder>::Item,
|
||||||
) -> Result<(), Self::Error> {
|
) -> Result<(), Self::Error> {
|
||||||
self.write(item)
|
self.write(item)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn poll_flush(
|
fn poll_flush(
|
||||||
mut self: Pin<&mut Self>,
|
self: Pin<&mut Self>,
|
||||||
cx: &mut Context<'_>,
|
cx: &mut Context<'_>,
|
||||||
) -> Poll<Result<(), Self::Error>> {
|
) -> Poll<Result<(), Self::Error>> {
|
||||||
self.flush(cx)
|
self.flush(cx)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn poll_close(
|
fn poll_close(
|
||||||
mut self: Pin<&mut Self>,
|
self: Pin<&mut Self>,
|
||||||
cx: &mut Context<'_>,
|
cx: &mut Context<'_>,
|
||||||
) -> Poll<Result<(), Self::Error>> {
|
) -> Poll<Result<(), Self::Error>> {
|
||||||
self.close(cx)
|
self.close(cx)
|
||||||
|
@@ -1,5 +1,16 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
|
## [2.0.0-alpha.2] - 2020-03-08
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
* Update `trust-dns-proto` dependency to 0.19. [#116]
|
||||||
|
* Update `trust-dns-resolver` dependency to 0.19. [#116]
|
||||||
|
* `Address` trait is now required to have static lifetime. [#116]
|
||||||
|
* `start_resolver` and `start_default_resolver` are now `async` and may return a `ConnectError`. [#116]
|
||||||
|
|
||||||
|
[#116]: https://github.com/actix/actix-net/pull/116
|
||||||
|
|
||||||
## [2.0.0-alpha.1] - 2020-03-03
|
## [2.0.0-alpha.1] - 2020-03-03
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-connect"
|
name = "actix-connect"
|
||||||
version = "2.0.0-alpha.1"
|
version = "2.0.0-alpha.2"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Actix connect - tcp connector service"
|
description = "Actix connect - tcp connector service"
|
||||||
keywords = ["network", "framework", "async", "futures"]
|
keywords = ["network", "framework", "async", "futures"]
|
||||||
@@ -37,11 +37,11 @@ actix-utils = "1.0.6"
|
|||||||
actix-rt = "1.0.0"
|
actix-rt = "1.0.0"
|
||||||
derive_more = "0.99.2"
|
derive_more = "0.99.2"
|
||||||
either = "1.5.3"
|
either = "1.5.3"
|
||||||
futures = "0.3.1"
|
futures-util = { version = "0.3.4", default-features = false }
|
||||||
http = { version = "0.2.0", optional = true }
|
http = { version = "0.2.0", optional = true }
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
trust-dns-proto = "=0.18.0-alpha.2"
|
trust-dns-proto = { version = "0.19", default-features = false, features = ["tokio-runtime"] }
|
||||||
trust-dns-resolver = "=0.18.0-alpha.2"
|
trust-dns-resolver = { version = "0.19", default-features = false, features = ["tokio-runtime", "system-config"] }
|
||||||
|
|
||||||
# openssl
|
# openssl
|
||||||
open-ssl = { version="0.10", package = "openssl", optional = true }
|
open-ssl = { version="0.10", package = "openssl", optional = true }
|
||||||
|
@@ -6,7 +6,7 @@ use std::net::SocketAddr;
|
|||||||
use either::Either;
|
use either::Either;
|
||||||
|
|
||||||
/// Connect request
|
/// Connect request
|
||||||
pub trait Address: Unpin {
|
pub trait Address: Unpin + 'static {
|
||||||
/// Host name of the request
|
/// Host name of the request
|
||||||
fn host(&self) -> &str;
|
fn host(&self) -> &str;
|
||||||
|
|
||||||
|
@@ -8,7 +8,7 @@ use std::task::{Context, Poll};
|
|||||||
|
|
||||||
use actix_rt::net::TcpStream;
|
use actix_rt::net::TcpStream;
|
||||||
use actix_service::{Service, ServiceFactory};
|
use actix_service::{Service, ServiceFactory};
|
||||||
use futures::future::{err, ok, BoxFuture, Either, FutureExt, Ready};
|
use futures_util::future::{err, ok, BoxFuture, Either, FutureExt, Ready};
|
||||||
|
|
||||||
use super::connect::{Address, Connect, Connection};
|
use super::connect::{Address, Connect, Connection};
|
||||||
use super::error::ConnectError;
|
use super::error::ConnectError;
|
||||||
|
@@ -25,7 +25,7 @@ use actix_rt::{net::TcpStream, Arbiter};
|
|||||||
use actix_service::{pipeline, pipeline_factory, Service, ServiceFactory};
|
use actix_service::{pipeline, pipeline_factory, Service, ServiceFactory};
|
||||||
use trust_dns_resolver::config::{ResolverConfig, ResolverOpts};
|
use trust_dns_resolver::config::{ResolverConfig, ResolverOpts};
|
||||||
use trust_dns_resolver::system_conf::read_system_conf;
|
use trust_dns_resolver::system_conf::read_system_conf;
|
||||||
use trust_dns_resolver::AsyncResolver;
|
use trust_dns_resolver::TokioAsyncResolver as AsyncResolver;
|
||||||
|
|
||||||
pub mod resolver {
|
pub mod resolver {
|
||||||
pub use trust_dns_resolver::config::{ResolverConfig, ResolverOpts};
|
pub use trust_dns_resolver::config::{ResolverConfig, ResolverOpts};
|
||||||
@@ -39,17 +39,18 @@ pub use self::error::ConnectError;
|
|||||||
pub use self::resolve::{Resolver, ResolverFactory};
|
pub use self::resolve::{Resolver, ResolverFactory};
|
||||||
pub use self::service::{ConnectService, ConnectServiceFactory, TcpConnectService};
|
pub use self::service::{ConnectService, ConnectServiceFactory, TcpConnectService};
|
||||||
|
|
||||||
pub fn start_resolver(cfg: ResolverConfig, opts: ResolverOpts) -> AsyncResolver {
|
pub async fn start_resolver(
|
||||||
let (resolver, bg) = AsyncResolver::new(cfg, opts);
|
cfg: ResolverConfig,
|
||||||
actix_rt::spawn(bg);
|
opts: ResolverOpts,
|
||||||
resolver
|
) -> Result<AsyncResolver, ConnectError> {
|
||||||
|
Ok(AsyncResolver::tokio(cfg, opts).await?)
|
||||||
}
|
}
|
||||||
|
|
||||||
struct DefaultResolver(AsyncResolver);
|
struct DefaultResolver(AsyncResolver);
|
||||||
|
|
||||||
pub(crate) fn get_default_resolver() -> AsyncResolver {
|
pub(crate) async fn get_default_resolver() -> Result<AsyncResolver, ConnectError> {
|
||||||
if Arbiter::contains_item::<DefaultResolver>() {
|
if Arbiter::contains_item::<DefaultResolver>() {
|
||||||
Arbiter::get_item(|item: &DefaultResolver| item.0.clone())
|
Ok(Arbiter::get_item(|item: &DefaultResolver| item.0.clone()))
|
||||||
} else {
|
} else {
|
||||||
let (cfg, opts) = match read_system_conf() {
|
let (cfg, opts) = match read_system_conf() {
|
||||||
Ok((cfg, opts)) => (cfg, opts),
|
Ok((cfg, opts)) => (cfg, opts),
|
||||||
@@ -59,16 +60,15 @@ pub(crate) fn get_default_resolver() -> AsyncResolver {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let (resolver, bg) = AsyncResolver::new(cfg, opts);
|
let resolver = AsyncResolver::tokio(cfg, opts).await?;
|
||||||
actix_rt::spawn(bg);
|
|
||||||
|
|
||||||
Arbiter::set_item(DefaultResolver(resolver.clone()));
|
Arbiter::set_item(DefaultResolver(resolver.clone()));
|
||||||
resolver
|
Ok(resolver)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn start_default_resolver() -> AsyncResolver {
|
pub async fn start_default_resolver() -> Result<AsyncResolver, ConnectError> {
|
||||||
get_default_resolver()
|
get_default_resolver().await
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create tcp connector service
|
/// Create tcp connector service
|
||||||
|
@@ -5,9 +5,9 @@ use std::pin::Pin;
|
|||||||
use std::task::{Context, Poll};
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
use actix_service::{Service, ServiceFactory};
|
use actix_service::{Service, ServiceFactory};
|
||||||
use futures::future::{ok, Either, Ready};
|
use futures_util::future::{ok, Either, Ready};
|
||||||
use trust_dns_resolver::lookup_ip::LookupIpFuture;
|
use trust_dns_resolver::TokioAsyncResolver as AsyncResolver;
|
||||||
use trust_dns_resolver::{AsyncResolver, Background};
|
use trust_dns_resolver::{error::ResolveError, lookup_ip::LookupIp};
|
||||||
|
|
||||||
use crate::connect::{Address, Connect};
|
use crate::connect::{Address, Connect};
|
||||||
use crate::error::ConnectError;
|
use crate::error::ConnectError;
|
||||||
@@ -106,7 +106,10 @@ impl<T: Address> Service for Resolver<T> {
|
|||||||
type Request = Connect<T>;
|
type Request = Connect<T>;
|
||||||
type Response = Connect<T>;
|
type Response = Connect<T>;
|
||||||
type Error = ConnectError;
|
type Error = ConnectError;
|
||||||
type Future = Either<ResolverFuture<T>, Ready<Result<Connect<T>, Self::Error>>>;
|
type Future = Either<
|
||||||
|
Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>,
|
||||||
|
Ready<Result<Connect<T>, Self::Error>>,
|
||||||
|
>;
|
||||||
|
|
||||||
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
Poll::Ready(Ok(()))
|
Poll::Ready(Ok(()))
|
||||||
@@ -119,32 +122,48 @@ impl<T: Address> Service for Resolver<T> {
|
|||||||
req.addr = Some(either::Either::Left(SocketAddr::new(ip, req.port())));
|
req.addr = Some(either::Either::Left(SocketAddr::new(ip, req.port())));
|
||||||
Either::Right(ok(req))
|
Either::Right(ok(req))
|
||||||
} else {
|
} else {
|
||||||
trace!("DNS resolver: resolving host {:?}", req.host());
|
let resolver = self.resolver.as_ref().map(AsyncResolver::clone);
|
||||||
if self.resolver.is_none() {
|
Either::Left(Box::pin(async move {
|
||||||
self.resolver = Some(get_default_resolver());
|
trace!("DNS resolver: resolving host {:?}", req.host());
|
||||||
}
|
let resolver = if let Some(resolver) = resolver {
|
||||||
Either::Left(ResolverFuture::new(req, self.resolver.as_ref().unwrap()))
|
resolver
|
||||||
|
} else {
|
||||||
|
get_default_resolver()
|
||||||
|
.await
|
||||||
|
.expect("Failed to get default resolver")
|
||||||
|
};
|
||||||
|
ResolverFuture::new(req, &resolver).await
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type LookupIpFuture = Pin<Box<dyn Future<Output = Result<LookupIp, ResolveError>>>>;
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
/// Resolver future
|
/// Resolver future
|
||||||
pub struct ResolverFuture<T: Address> {
|
pub struct ResolverFuture<T: Address> {
|
||||||
req: Option<Connect<T>>,
|
req: Option<Connect<T>>,
|
||||||
lookup: Background<LookupIpFuture>,
|
lookup: LookupIpFuture,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Address> ResolverFuture<T> {
|
impl<T: Address> ResolverFuture<T> {
|
||||||
pub fn new(req: Connect<T>, resolver: &AsyncResolver) -> Self {
|
pub fn new(req: Connect<T>, resolver: &AsyncResolver) -> Self {
|
||||||
let lookup = if let Some(host) = req.host().splitn(2, ':').next() {
|
let host = if let Some(host) = req.host().splitn(2, ':').next() {
|
||||||
resolver.lookup_ip(host)
|
host
|
||||||
} else {
|
} else {
|
||||||
resolver.lookup_ip(req.host())
|
req.host()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Clone data to be moved to the lookup future
|
||||||
|
let host_clone = host.to_owned();
|
||||||
|
let resolver_clone = resolver.clone();
|
||||||
|
|
||||||
ResolverFuture {
|
ResolverFuture {
|
||||||
lookup,
|
lookup: Box::pin(async move {
|
||||||
|
let resolver = resolver_clone;
|
||||||
|
resolver.lookup_ip(host_clone).await
|
||||||
|
}),
|
||||||
req: Some(req),
|
req: Some(req),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -5,8 +5,8 @@ use std::task::{Context, Poll};
|
|||||||
use actix_rt::net::TcpStream;
|
use actix_rt::net::TcpStream;
|
||||||
use actix_service::{Service, ServiceFactory};
|
use actix_service::{Service, ServiceFactory};
|
||||||
use either::Either;
|
use either::Either;
|
||||||
use futures::future::{ok, Ready};
|
use futures_util::future::{ok, Ready};
|
||||||
use trust_dns_resolver::AsyncResolver;
|
use trust_dns_resolver::TokioAsyncResolver as AsyncResolver;
|
||||||
|
|
||||||
use crate::connect::{Address, Connect, Connection};
|
use crate::connect::{Address, Connect, Connection};
|
||||||
use crate::connector::{TcpConnector, TcpConnectorFactory};
|
use crate::connector::{TcpConnector, TcpConnectorFactory};
|
||||||
|
@@ -10,8 +10,8 @@ pub use tokio_openssl::{HandshakeError, SslStream};
|
|||||||
use actix_codec::{AsyncRead, AsyncWrite};
|
use actix_codec::{AsyncRead, AsyncWrite};
|
||||||
use actix_rt::net::TcpStream;
|
use actix_rt::net::TcpStream;
|
||||||
use actix_service::{Service, ServiceFactory};
|
use actix_service::{Service, ServiceFactory};
|
||||||
use futures::future::{err, ok, Either, FutureExt, LocalBoxFuture, Ready};
|
use futures_util::future::{err, ok, Either, FutureExt, LocalBoxFuture, Ready};
|
||||||
use trust_dns_resolver::AsyncResolver;
|
use trust_dns_resolver::TokioAsyncResolver as AsyncResolver;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Address, Connect, ConnectError, ConnectService, ConnectServiceFactory, Connection,
|
Address, Connect, ConnectError, ConnectService, ConnectServiceFactory, Connection,
|
||||||
@@ -243,7 +243,7 @@ impl<T: Address> Future for OpensslConnectServiceResponse<T> {
|
|||||||
|
|
||||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
if let Some(ref mut fut) = self.fut1 {
|
if let Some(ref mut fut) = self.fut1 {
|
||||||
match futures::ready!(Pin::new(fut).poll(cx)) {
|
match futures_util::ready!(Pin::new(fut).poll(cx)) {
|
||||||
Ok(res) => {
|
Ok(res) => {
|
||||||
let _ = self.fut1.take();
|
let _ = self.fut1.take();
|
||||||
self.fut2 = Some(self.openssl.call(res));
|
self.fut2 = Some(self.openssl.call(res));
|
||||||
@@ -253,7 +253,7 @@ impl<T: Address> Future for OpensslConnectServiceResponse<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if let Some(ref mut fut) = self.fut2 {
|
if let Some(ref mut fut) = self.fut2 {
|
||||||
match futures::ready!(Pin::new(fut).poll(cx)) {
|
match futures_util::ready!(Pin::new(fut).poll(cx)) {
|
||||||
Ok(connect) => Poll::Ready(Ok(connect.into_parts().0)),
|
Ok(connect) => Poll::Ready(Ok(connect.into_parts().0)),
|
||||||
Err(e) => Poll::Ready(Err(ConnectError::Io(io::Error::new(
|
Err(e) => Poll::Ready(Err(ConnectError::Io(io::Error::new(
|
||||||
io::ErrorKind::Other,
|
io::ErrorKind::Other,
|
||||||
|
@@ -10,7 +10,7 @@ pub use tokio_rustls::{client::TlsStream, rustls::ClientConfig};
|
|||||||
|
|
||||||
use actix_codec::{AsyncRead, AsyncWrite};
|
use actix_codec::{AsyncRead, AsyncWrite};
|
||||||
use actix_service::{Service, ServiceFactory};
|
use actix_service::{Service, ServiceFactory};
|
||||||
use futures::future::{ok, Ready};
|
use futures_util::future::{ok, Ready};
|
||||||
use tokio_rustls::{Connect, TlsConnector};
|
use tokio_rustls::{Connect, TlsConnector};
|
||||||
use webpki::DNSNameRef;
|
use webpki::DNSNameRef;
|
||||||
|
|
||||||
@@ -126,7 +126,7 @@ where
|
|||||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
let this = self.get_mut();
|
let this = self.get_mut();
|
||||||
Poll::Ready(
|
Poll::Ready(
|
||||||
futures::ready!(Pin::new(&mut this.fut).poll(cx)).map(|stream| {
|
futures_util::ready!(Pin::new(&mut this.fut).poll(cx)).map(|stream| {
|
||||||
let s = this.stream.take().unwrap();
|
let s = this.stream.take().unwrap();
|
||||||
trace!("SSL Handshake success: {:?}", s.host());
|
trace!("SSL Handshake success: {:?}", s.host());
|
||||||
s.replace(stream).1
|
s.replace(stream).1
|
||||||
|
@@ -5,7 +5,7 @@ use actix_rt::net::TcpStream;
|
|||||||
use actix_service::{fn_service, Service, ServiceFactory};
|
use actix_service::{fn_service, Service, ServiceFactory};
|
||||||
use actix_testing::TestServer;
|
use actix_testing::TestServer;
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use futures::SinkExt;
|
use futures_util::sink::SinkExt;
|
||||||
|
|
||||||
use actix_connect::resolver::{ResolverConfig, ResolverOpts};
|
use actix_connect::resolver::{ResolverConfig, ResolverOpts};
|
||||||
use actix_connect::Connect;
|
use actix_connect::Connect;
|
||||||
@@ -14,12 +14,10 @@ use actix_connect::Connect;
|
|||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_string() {
|
async fn test_string() {
|
||||||
let srv = TestServer::with(|| {
|
let srv = TestServer::with(|| {
|
||||||
fn_service(|io: TcpStream| {
|
fn_service(|io: TcpStream| async {
|
||||||
async {
|
let mut framed = Framed::new(io, BytesCodec);
|
||||||
let mut framed = Framed::new(io, BytesCodec);
|
framed.send(Bytes::from_static(b"test")).await?;
|
||||||
framed.send(Bytes::from_static(b"test")).await?;
|
Ok::<_, io::Error>(())
|
||||||
Ok::<_, io::Error>(())
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -33,12 +31,10 @@ async fn test_string() {
|
|||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_rustls_string() {
|
async fn test_rustls_string() {
|
||||||
let srv = TestServer::with(|| {
|
let srv = TestServer::with(|| {
|
||||||
fn_service(|io: TcpStream| {
|
fn_service(|io: TcpStream| async {
|
||||||
async {
|
let mut framed = Framed::new(io, BytesCodec);
|
||||||
let mut framed = Framed::new(io, BytesCodec);
|
framed.send(Bytes::from_static(b"test")).await?;
|
||||||
framed.send(Bytes::from_static(b"test")).await?;
|
Ok::<_, io::Error>(())
|
||||||
Ok::<_, io::Error>(())
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -51,16 +47,14 @@ async fn test_rustls_string() {
|
|||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_static_str() {
|
async fn test_static_str() {
|
||||||
let srv = TestServer::with(|| {
|
let srv = TestServer::with(|| {
|
||||||
fn_service(|io: TcpStream| {
|
fn_service(|io: TcpStream| async {
|
||||||
async {
|
let mut framed = Framed::new(io, BytesCodec);
|
||||||
let mut framed = Framed::new(io, BytesCodec);
|
framed.send(Bytes::from_static(b"test")).await?;
|
||||||
framed.send(Bytes::from_static(b"test")).await?;
|
Ok::<_, io::Error>(())
|
||||||
Ok::<_, io::Error>(())
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
let resolver = actix_connect::start_default_resolver();
|
let resolver = actix_connect::start_default_resolver().await.unwrap();
|
||||||
let mut conn = actix_connect::new_connector(resolver.clone());
|
let mut conn = actix_connect::new_connector(resolver.clone());
|
||||||
|
|
||||||
let con = conn.call(Connect::with("10", srv.addr())).await.unwrap();
|
let con = conn.call(Connect::with("10", srv.addr())).await.unwrap();
|
||||||
@@ -75,17 +69,17 @@ async fn test_static_str() {
|
|||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_new_service() {
|
async fn test_new_service() {
|
||||||
let srv = TestServer::with(|| {
|
let srv = TestServer::with(|| {
|
||||||
fn_service(|io: TcpStream| {
|
fn_service(|io: TcpStream| async {
|
||||||
async {
|
let mut framed = Framed::new(io, BytesCodec);
|
||||||
let mut framed = Framed::new(io, BytesCodec);
|
framed.send(Bytes::from_static(b"test")).await?;
|
||||||
framed.send(Bytes::from_static(b"test")).await?;
|
Ok::<_, io::Error>(())
|
||||||
Ok::<_, io::Error>(())
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
let resolver =
|
let resolver =
|
||||||
actix_connect::start_resolver(ResolverConfig::default(), ResolverOpts::default());
|
actix_connect::start_resolver(ResolverConfig::default(), ResolverOpts::default())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let factory = actix_connect::new_connector_factory(resolver);
|
let factory = actix_connect::new_connector_factory(resolver);
|
||||||
|
|
||||||
@@ -100,12 +94,10 @@ async fn test_uri() {
|
|||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
let srv = TestServer::with(|| {
|
let srv = TestServer::with(|| {
|
||||||
fn_service(|io: TcpStream| {
|
fn_service(|io: TcpStream| async {
|
||||||
async {
|
let mut framed = Framed::new(io, BytesCodec);
|
||||||
let mut framed = Framed::new(io, BytesCodec);
|
framed.send(Bytes::from_static(b"test")).await?;
|
||||||
framed.send(Bytes::from_static(b"test")).await?;
|
Ok::<_, io::Error>(())
|
||||||
Ok::<_, io::Error>(())
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -121,12 +113,10 @@ async fn test_rustls_uri() {
|
|||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
let srv = TestServer::with(|| {
|
let srv = TestServer::with(|| {
|
||||||
fn_service(|io: TcpStream| {
|
fn_service(|io: TcpStream| async {
|
||||||
async {
|
let mut framed = Framed::new(io, BytesCodec);
|
||||||
let mut framed = Framed::new(io, BytesCodec);
|
framed.send(Bytes::from_static(b"test")).await?;
|
||||||
framed.send(Bytes::from_static(b"test")).await?;
|
Ok::<_, io::Error>(())
|
||||||
Ok::<_, io::Error>(())
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@@ -22,10 +22,12 @@ actix-utils = "1.0.4"
|
|||||||
actix-rt = "1.0.0"
|
actix-rt = "1.0.0"
|
||||||
bytes = "0.5.3"
|
bytes = "0.5.3"
|
||||||
either = "1.5.3"
|
either = "1.5.3"
|
||||||
futures = "0.3.1"
|
futures-sink = { version = "0.3.4", default-features = false }
|
||||||
|
futures-core = { version = "0.3.4", default-features = false }
|
||||||
pin-project = "0.4.6"
|
pin-project = "0.4.6"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-connect = "2.0.0-alpha.1"
|
actix-connect = "2.0.0-alpha.2"
|
||||||
actix-testing = "1.0.0"
|
actix-testing = "1.0.0"
|
||||||
|
futures-util = { version = "0.3.4", default-features = false }
|
||||||
|
@@ -4,7 +4,7 @@ use std::task::{Context, Poll};
|
|||||||
|
|
||||||
use actix_codec::{AsyncRead, AsyncWrite, Decoder, Encoder, Framed};
|
use actix_codec::{AsyncRead, AsyncWrite, Decoder, Encoder, Framed};
|
||||||
use actix_utils::mpsc::Receiver;
|
use actix_utils::mpsc::Receiver;
|
||||||
use futures::Stream;
|
use futures_core::stream::Stream;
|
||||||
|
|
||||||
pub struct Connect<Io, Codec>
|
pub struct Connect<Io, Codec>
|
||||||
where
|
where
|
||||||
@@ -42,6 +42,7 @@ where
|
|||||||
pub struct ConnectResult<Io, St, Codec: Encoder + Decoder, Out> {
|
pub struct ConnectResult<Io, St, Codec: Encoder + Decoder, Out> {
|
||||||
pub(crate) state: St,
|
pub(crate) state: St,
|
||||||
pub(crate) out: Option<Out>,
|
pub(crate) out: Option<Out>,
|
||||||
|
#[pin]
|
||||||
pub(crate) framed: Framed<Io, Codec>,
|
pub(crate) framed: Framed<Io, Codec>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,7 +90,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Io, St, Codec, Out> futures::Sink<<Codec as Encoder>::Item>
|
impl<Io, St, Codec, Out> futures_sink::Sink<<Codec as Encoder>::Item>
|
||||||
for ConnectResult<Io, St, Codec, Out>
|
for ConnectResult<Io, St, Codec, Out>
|
||||||
where
|
where
|
||||||
Io: AsyncRead + AsyncWrite,
|
Io: AsyncRead + AsyncWrite,
|
||||||
@@ -97,8 +98,8 @@ where
|
|||||||
{
|
{
|
||||||
type Error = <Codec as Encoder>::Error;
|
type Error = <Codec as Encoder>::Error;
|
||||||
|
|
||||||
fn poll_ready(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
fn poll_ready(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
if self.framed.is_write_ready() {
|
if self.as_mut().project().framed.is_write_ready() {
|
||||||
Poll::Ready(Ok(()))
|
Poll::Ready(Ok(()))
|
||||||
} else {
|
} else {
|
||||||
Poll::Pending
|
Poll::Pending
|
||||||
@@ -112,11 +113,11 @@ where
|
|||||||
self.project().framed.write(item)
|
self.project().framed.write(item)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
self.get_mut().framed.flush(cx)
|
self.as_mut().project().framed.flush(cx)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
self.get_mut().framed.close(cx)
|
self.as_mut().project().framed.close(cx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -5,7 +5,8 @@ use std::task::{Context, Poll};
|
|||||||
use actix_codec::{AsyncRead, AsyncWrite, Decoder, Encoder, Framed};
|
use actix_codec::{AsyncRead, AsyncWrite, Decoder, Encoder, Framed};
|
||||||
use actix_service::Service;
|
use actix_service::Service;
|
||||||
use actix_utils::mpsc;
|
use actix_utils::mpsc;
|
||||||
use futures::Stream;
|
use futures_core::stream::Stream;
|
||||||
|
use pin_project::pin_project;
|
||||||
use log::debug;
|
use log::debug;
|
||||||
|
|
||||||
use crate::error::ServiceError;
|
use crate::error::ServiceError;
|
||||||
@@ -15,6 +16,7 @@ type Response<U> = <U as Encoder>::Item;
|
|||||||
|
|
||||||
/// FramedTransport - is a future that reads frames from Framed object
|
/// FramedTransport - is a future that reads frames from Framed object
|
||||||
/// and pass then to the service.
|
/// and pass then to the service.
|
||||||
|
#[pin_project]
|
||||||
pub(crate) struct Dispatcher<S, T, U, Out>
|
pub(crate) struct Dispatcher<S, T, U, Out>
|
||||||
where
|
where
|
||||||
S: Service<Request = Request<U>, Response = Option<Response<U>>>,
|
S: Service<Request = Request<U>, Response = Option<Response<U>>>,
|
||||||
@@ -29,6 +31,7 @@ where
|
|||||||
service: S,
|
service: S,
|
||||||
sink: Option<Out>,
|
sink: Option<Out>,
|
||||||
state: FramedState<S, U>,
|
state: FramedState<S, U>,
|
||||||
|
#[pin]
|
||||||
framed: Framed<T, U>,
|
framed: Framed<T, U>,
|
||||||
rx: mpsc::Receiver<Result<<U as Encoder>::Item, S::Error>>,
|
rx: mpsc::Receiver<Result<<U as Encoder>::Item, S::Error>>,
|
||||||
}
|
}
|
||||||
@@ -90,26 +93,27 @@ where
|
|||||||
<U as Encoder>::Error: std::fmt::Debug,
|
<U as Encoder>::Error: std::fmt::Debug,
|
||||||
Out: Stream<Item = <U as Encoder>::Item> + Unpin,
|
Out: Stream<Item = <U as Encoder>::Item> + Unpin,
|
||||||
{
|
{
|
||||||
fn poll_read(&mut self, cx: &mut Context<'_>) -> bool {
|
fn poll_read(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> bool {
|
||||||
loop {
|
loop {
|
||||||
match self.service.poll_ready(cx) {
|
let this = self.as_mut().project();
|
||||||
|
match this.service.poll_ready(cx) {
|
||||||
Poll::Ready(Ok(_)) => {
|
Poll::Ready(Ok(_)) => {
|
||||||
let item = match self.framed.next_item(cx) {
|
let item = match this.framed.next_item(cx) {
|
||||||
Poll::Ready(Some(Ok(el))) => el,
|
Poll::Ready(Some(Ok(el))) => el,
|
||||||
Poll::Ready(Some(Err(err))) => {
|
Poll::Ready(Some(Err(err))) => {
|
||||||
self.state = FramedState::FramedError(ServiceError::Decoder(err));
|
*this.state = FramedState::FramedError(ServiceError::Decoder(err));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
Poll::Pending => return false,
|
Poll::Pending => return false,
|
||||||
Poll::Ready(None) => {
|
Poll::Ready(None) => {
|
||||||
log::trace!("Client disconnected");
|
log::trace!("Client disconnected");
|
||||||
self.state = FramedState::Stopping;
|
*this.state = FramedState::Stopping;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let tx = self.rx.sender();
|
let tx = this.rx.sender();
|
||||||
let fut = self.service.call(item);
|
let fut = this.service.call(item);
|
||||||
actix_rt::spawn(async move {
|
actix_rt::spawn(async move {
|
||||||
let item = fut.await;
|
let item = fut.await;
|
||||||
let item = match item {
|
let item = match item {
|
||||||
@@ -122,7 +126,7 @@ where
|
|||||||
}
|
}
|
||||||
Poll::Pending => return false,
|
Poll::Pending => return false,
|
||||||
Poll::Ready(Err(err)) => {
|
Poll::Ready(Err(err)) => {
|
||||||
self.state = FramedState::Error(ServiceError::Service(err));
|
*this.state = FramedState::Error(ServiceError::Service(err));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -130,37 +134,38 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// write to framed object
|
/// write to framed object
|
||||||
fn poll_write(&mut self, cx: &mut Context<'_>) -> bool {
|
fn poll_write(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> bool {
|
||||||
loop {
|
loop {
|
||||||
while !self.framed.is_write_buf_full() {
|
let mut this = self.as_mut().project();
|
||||||
match Pin::new(&mut self.rx).poll_next(cx) {
|
while !this.framed.is_write_buf_full() {
|
||||||
|
match Pin::new(&mut this.rx).poll_next(cx) {
|
||||||
Poll::Ready(Some(Ok(msg))) => {
|
Poll::Ready(Some(Ok(msg))) => {
|
||||||
if let Err(err) = self.framed.write(msg) {
|
if let Err(err) = this.framed.as_mut().write(msg) {
|
||||||
self.state = FramedState::FramedError(ServiceError::Encoder(err));
|
*this.state = FramedState::FramedError(ServiceError::Encoder(err));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
Poll::Ready(Some(Err(err))) => {
|
Poll::Ready(Some(Err(err))) => {
|
||||||
self.state = FramedState::Error(ServiceError::Service(err));
|
*this.state = FramedState::Error(ServiceError::Service(err));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
Poll::Ready(None) | Poll::Pending => (),
|
Poll::Ready(None) | Poll::Pending => (),
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.sink.is_some() {
|
if this.sink.is_some() {
|
||||||
match Pin::new(self.sink.as_mut().unwrap()).poll_next(cx) {
|
match Pin::new(this.sink.as_mut().unwrap()).poll_next(cx) {
|
||||||
Poll::Ready(Some(msg)) => {
|
Poll::Ready(Some(msg)) => {
|
||||||
if let Err(err) = self.framed.write(msg) {
|
if let Err(err) = this.framed.as_mut().write(msg) {
|
||||||
self.state =
|
*this.state =
|
||||||
FramedState::FramedError(ServiceError::Encoder(err));
|
FramedState::FramedError(ServiceError::Encoder(err));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
Poll::Ready(None) => {
|
Poll::Ready(None) => {
|
||||||
let _ = self.sink.take();
|
let _ = this.sink.take();
|
||||||
self.state = FramedState::FlushAndStop;
|
*this.state = FramedState::FlushAndStop;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
Poll::Pending => (),
|
Poll::Pending => (),
|
||||||
@@ -169,13 +174,13 @@ where
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if !self.framed.is_write_buf_empty() {
|
if !this.framed.is_write_buf_empty() {
|
||||||
match self.framed.flush(cx) {
|
match this.framed.as_mut().flush(cx) {
|
||||||
Poll::Pending => break,
|
Poll::Pending => break,
|
||||||
Poll::Ready(Ok(_)) => (),
|
Poll::Ready(Ok(_)) => (),
|
||||||
Poll::Ready(Err(err)) => {
|
Poll::Ready(Err(err)) => {
|
||||||
debug!("Error sending data: {:?}", err);
|
debug!("Error sending data: {:?}", err);
|
||||||
self.state = FramedState::FramedError(ServiceError::Encoder(err));
|
*this.state = FramedState::FramedError(ServiceError::Encoder(err));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -187,13 +192,14 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn poll(
|
pub(crate) fn poll(
|
||||||
&mut self,
|
mut self: Pin<&mut Self>,
|
||||||
cx: &mut Context<'_>,
|
cx: &mut Context<'_>,
|
||||||
) -> Poll<Result<(), ServiceError<S::Error, U>>> {
|
) -> Poll<Result<(), ServiceError<S::Error, U>>> {
|
||||||
match self.state {
|
let mut this = self.as_mut().project();
|
||||||
|
match this.state {
|
||||||
FramedState::Processing => loop {
|
FramedState::Processing => loop {
|
||||||
let read = self.poll_read(cx);
|
let read = self.as_mut().poll_read(cx);
|
||||||
let write = self.poll_write(cx);
|
let write = self.as_mut().poll_write(cx);
|
||||||
if read || write {
|
if read || write {
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
@@ -202,18 +208,18 @@ where
|
|||||||
},
|
},
|
||||||
FramedState::Error(_) => {
|
FramedState::Error(_) => {
|
||||||
// flush write buffer
|
// flush write buffer
|
||||||
if !self.framed.is_write_buf_empty() {
|
if !this.framed.is_write_buf_empty() {
|
||||||
if let Poll::Pending = self.framed.flush(cx) {
|
if let Poll::Pending = this.framed.flush(cx) {
|
||||||
return Poll::Pending;
|
return Poll::Pending;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Poll::Ready(Err(self.state.take_error()))
|
Poll::Ready(Err(this.state.take_error()))
|
||||||
}
|
}
|
||||||
FramedState::FlushAndStop => {
|
FramedState::FlushAndStop => {
|
||||||
// drain service responses
|
// drain service responses
|
||||||
match Pin::new(&mut self.rx).poll_next(cx) {
|
match Pin::new(this.rx).poll_next(cx) {
|
||||||
Poll::Ready(Some(Ok(msg))) => {
|
Poll::Ready(Some(Ok(msg))) => {
|
||||||
if self.framed.write(msg).is_err() {
|
if this.framed.as_mut().write(msg).is_err() {
|
||||||
return Poll::Ready(Ok(()));
|
return Poll::Ready(Ok(()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -222,8 +228,8 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
// flush io
|
// flush io
|
||||||
if !self.framed.is_write_buf_empty() {
|
if !this.framed.is_write_buf_empty() {
|
||||||
match self.framed.flush(cx) {
|
match this.framed.flush(cx) {
|
||||||
Poll::Ready(Err(err)) => {
|
Poll::Ready(Err(err)) => {
|
||||||
debug!("Error sending data: {:?}", err);
|
debug!("Error sending data: {:?}", err);
|
||||||
}
|
}
|
||||||
@@ -235,7 +241,7 @@ where
|
|||||||
};
|
};
|
||||||
Poll::Ready(Ok(()))
|
Poll::Ready(Ok(()))
|
||||||
}
|
}
|
||||||
FramedState::FramedError(_) => Poll::Ready(Err(self.state.take_framed_error())),
|
FramedState::FramedError(_) => Poll::Ready(Err(this.state.take_framed_error())),
|
||||||
FramedState::Stopping => Poll::Ready(Ok(())),
|
FramedState::Stopping => Poll::Ready(Ok(())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -7,7 +7,7 @@ use std::task::{Context, Poll};
|
|||||||
use actix_codec::{AsyncRead, AsyncWrite, Decoder, Encoder, Framed};
|
use actix_codec::{AsyncRead, AsyncWrite, Decoder, Encoder, Framed};
|
||||||
use actix_service::{IntoService, IntoServiceFactory, Service, ServiceFactory};
|
use actix_service::{IntoService, IntoServiceFactory, Service, ServiceFactory};
|
||||||
use either::Either;
|
use either::Either;
|
||||||
use futures::{ready, Stream};
|
use futures_core::{ready, stream::Stream};
|
||||||
use pin_project::project;
|
use pin_project::project;
|
||||||
|
|
||||||
use crate::connect::{Connect, ConnectResult};
|
use crate::connect::{Connect, ConnectResult};
|
||||||
@@ -357,7 +357,7 @@ where
|
|||||||
{
|
{
|
||||||
Connect(#[pin] C::Future, Rc<T>),
|
Connect(#[pin] C::Future, Rc<T>),
|
||||||
Handler(#[pin] T::Future, Option<Framed<Io, Codec>>, Option<Out>),
|
Handler(#[pin] T::Future, Option<Framed<Io, Codec>>, Option<Out>),
|
||||||
Dispatcher(Dispatcher<T::Service, Io, Codec, Out>),
|
Dispatcher(#[pin] Dispatcher<T::Service, Io, Codec, Out>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<St, Io, Codec, Out, C, T> FramedServiceImplResponseInner<St, Io, Codec, Out, C, T>
|
impl<St, Io, Codec, Out, C, T> FramedServiceImplResponseInner<St, Io, Codec, Out, C, T>
|
||||||
@@ -408,7 +408,7 @@ where
|
|||||||
Poll::Ready(Err(e)) => Either::Right(Poll::Ready(Err(e.into()))),
|
Poll::Ready(Err(e)) => Either::Right(Poll::Ready(Err(e.into()))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
FramedServiceImplResponseInner::Dispatcher(ref mut fut) => {
|
FramedServiceImplResponseInner::Dispatcher(fut) => {
|
||||||
Either::Right(fut.poll(cx))
|
Either::Right(fut.poll(cx))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -6,7 +6,7 @@ use actix_service::{fn_factory_with_config, fn_service, IntoService, Service};
|
|||||||
use actix_testing::TestServer;
|
use actix_testing::TestServer;
|
||||||
use actix_utils::mpsc;
|
use actix_utils::mpsc;
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
use futures::future::ok;
|
use futures_util::future::ok;
|
||||||
|
|
||||||
use actix_ioframe::{Builder, Connect, FactoryBuilder};
|
use actix_ioframe::{Builder, Connect, FactoryBuilder};
|
||||||
|
|
||||||
|
1
actix-macros/.gitignore
vendored
Normal file
1
actix-macros/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
/wip
|
@@ -8,14 +8,16 @@ documentation = "https://docs.rs/actix-macros/"
|
|||||||
categories = ["network-programming", "asynchronous"]
|
categories = ["network-programming", "asynchronous"]
|
||||||
license = "MIT/Apache-2.0"
|
license = "MIT/Apache-2.0"
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
workspace = ".."
|
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
proc-macro = true
|
proc-macro = true
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
quote = "^1"
|
quote = "1.0.3"
|
||||||
syn = { version = "^1", features = ["full"] }
|
syn = { version = "^1", features = ["full"] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-rt = { version = "1.0.0" }
|
actix-rt = "1.0"
|
||||||
|
|
||||||
|
futures = "0.3"
|
||||||
|
trybuild = "1"
|
||||||
|
@@ -55,12 +55,11 @@ pub fn main(_: TokenStream, item: TokenStream) -> TokenStream {
|
|||||||
/// ```
|
/// ```
|
||||||
#[proc_macro_attribute]
|
#[proc_macro_attribute]
|
||||||
pub fn test(_: TokenStream, item: TokenStream) -> TokenStream {
|
pub fn test(_: TokenStream, item: TokenStream) -> TokenStream {
|
||||||
let input = syn::parse_macro_input!(item as syn::ItemFn);
|
let mut input = syn::parse_macro_input!(item as syn::ItemFn);
|
||||||
|
|
||||||
let ret = &input.sig.output;
|
|
||||||
let name = &input.sig.ident;
|
|
||||||
let body = &input.block;
|
|
||||||
let attrs = &input.attrs;
|
let attrs = &input.attrs;
|
||||||
|
let vis = &input.vis;
|
||||||
|
let sig = &mut input.sig;
|
||||||
|
let body = &input.block;
|
||||||
let mut has_test_attr = false;
|
let mut has_test_attr = false;
|
||||||
|
|
||||||
for attr in attrs {
|
for attr in attrs {
|
||||||
@@ -69,7 +68,7 @@ pub fn test(_: TokenStream, item: TokenStream) -> TokenStream {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if input.sig.asyncness.is_none() {
|
if sig.asyncness.is_none() {
|
||||||
return syn::Error::new_spanned(
|
return syn::Error::new_spanned(
|
||||||
input.sig.fn_token,
|
input.sig.fn_token,
|
||||||
format!("only async fn is supported, {}", input.sig.ident),
|
format!("only async fn is supported, {}", input.sig.ident),
|
||||||
@@ -78,10 +77,12 @@ pub fn test(_: TokenStream, item: TokenStream) -> TokenStream {
|
|||||||
.into();
|
.into();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sig.asyncness = None;
|
||||||
|
|
||||||
let result = if has_test_attr {
|
let result = if has_test_attr {
|
||||||
quote! {
|
quote! {
|
||||||
#(#attrs)*
|
#(#attrs)*
|
||||||
fn #name() #ret {
|
#vis #sig {
|
||||||
actix_rt::System::new("test")
|
actix_rt::System::new("test")
|
||||||
.block_on(async { #body })
|
.block_on(async { #body })
|
||||||
}
|
}
|
||||||
@@ -90,7 +91,7 @@ pub fn test(_: TokenStream, item: TokenStream) -> TokenStream {
|
|||||||
quote! {
|
quote! {
|
||||||
#[test]
|
#[test]
|
||||||
#(#attrs)*
|
#(#attrs)*
|
||||||
fn #name() #ret {
|
#vis #sig {
|
||||||
actix_rt::System::new("test")
|
actix_rt::System::new("test")
|
||||||
.block_on(async { #body })
|
.block_on(async { #body })
|
||||||
}
|
}
|
||||||
|
9
actix-macros/tests/trybuild.rs
Normal file
9
actix-macros/tests/trybuild.rs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
#[test]
|
||||||
|
fn compile_macros() {
|
||||||
|
let t = trybuild::TestCases::new();
|
||||||
|
t.pass("tests/trybuild/main-01-basic.rs");
|
||||||
|
t.compile_fail("tests/trybuild/main-02-only-async.rs");
|
||||||
|
|
||||||
|
t.pass("tests/trybuild/test-01-basic.rs");
|
||||||
|
t.pass("tests/trybuild/test-02-keep-attrs.rs");
|
||||||
|
}
|
4
actix-macros/tests/trybuild/main-01-basic.rs
Normal file
4
actix-macros/tests/trybuild/main-01-basic.rs
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
#[actix_rt::main]
|
||||||
|
async fn main() {
|
||||||
|
println!("Hello world");
|
||||||
|
}
|
4
actix-macros/tests/trybuild/main-02-only-async.rs
Normal file
4
actix-macros/tests/trybuild/main-02-only-async.rs
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
#[actix_rt::main]
|
||||||
|
fn main() {
|
||||||
|
futures::future::ready(()).await
|
||||||
|
}
|
14
actix-macros/tests/trybuild/main-02-only-async.stderr
Normal file
14
actix-macros/tests/trybuild/main-02-only-async.stderr
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
error: only async fn is supported
|
||||||
|
--> $DIR/main-02-only-async.rs:2:1
|
||||||
|
|
|
||||||
|
2 | fn main() {
|
||||||
|
| ^^
|
||||||
|
|
||||||
|
error[E0601]: `main` function not found in crate `$CRATE`
|
||||||
|
--> $DIR/main-02-only-async.rs:1:1
|
||||||
|
|
|
||||||
|
1 | / #[actix_rt::main]
|
||||||
|
2 | | fn main() {
|
||||||
|
3 | | futures::future::ready(()).await
|
||||||
|
4 | | }
|
||||||
|
| |_^ consider adding a `main` function to `$DIR/tests/trybuild/main-02-only-async.rs`
|
6
actix-macros/tests/trybuild/test-01-basic.rs
Normal file
6
actix-macros/tests/trybuild/test-01-basic.rs
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#[actix_rt::test]
|
||||||
|
async fn my_test() {
|
||||||
|
assert!(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {}
|
7
actix-macros/tests/trybuild/test-02-keep-attrs.rs
Normal file
7
actix-macros/tests/trybuild/test-02-keep-attrs.rs
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
#[actix_rt::test]
|
||||||
|
#[should_panic]
|
||||||
|
async fn my_test() {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {}
|
@@ -1,10 +1,27 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
## [TBD] - [TBD]
|
## [1.1.1] - 2020-04-30
|
||||||
|
|
||||||
- Expose `System::is_set` to check if current system is running
|
### Fixed
|
||||||
|
|
||||||
- Add `Arbiter::local_join` associated function to get be able to `await` for spawned futures
|
* Fix memory leak due to [#94] (see [#129] for more detail)
|
||||||
|
|
||||||
|
[#129]: https://github.com/actix/actix-net/issues/129
|
||||||
|
|
||||||
|
## [1.1.0] - 2020-04-08
|
||||||
|
|
||||||
|
**This version has been yanked.**
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
* Expose `System::is_set` to check if current system has ben started [#99]
|
||||||
|
* Add `Arbiter::is_running` to check if event loop is running [#124]
|
||||||
|
* Add `Arbiter::local_join` associated function
|
||||||
|
to get be able to `await` for spawned futures [#94]
|
||||||
|
|
||||||
|
[#94]: https://github.com/actix/actix-net/pull/94
|
||||||
|
[#99]: https://github.com/actix/actix-net/pull/99
|
||||||
|
[#124]: https://github.com/actix/actix-net/pull/124
|
||||||
|
|
||||||
## [1.0.0] - 2019-12-11
|
## [1.0.0] - 2019-12-11
|
||||||
|
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-rt"
|
name = "actix-rt"
|
||||||
version = "1.0.0"
|
version = "1.1.1"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Actix runtime"
|
description = "Actix runtime"
|
||||||
keywords = ["network", "framework", "async", "futures"]
|
keywords = ["network", "framework", "async", "futures"]
|
||||||
@@ -18,7 +18,8 @@ path = "src/lib.rs"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
actix-macros = "0.1.0"
|
actix-macros = "0.1.0"
|
||||||
actix-threadpool = "0.3"
|
actix-threadpool = "0.3"
|
||||||
futures-channel = { version = "0.3.1", default-features = false }
|
futures-channel = { version = "0.3.4", default-features = false }
|
||||||
futures-util = { version = "0.3.1", default-features = false }
|
futures-util = { version = "0.3.4", default-features = false, features = ["alloc"] }
|
||||||
copyless = "0.1.4"
|
copyless = "0.1.4"
|
||||||
|
smallvec = "1"
|
||||||
tokio = { version = "0.2.6", default-features = false, features = ["rt-core", "rt-util", "io-driver", "tcp", "uds", "udp", "time", "signal", "stream"] }
|
tokio = { version = "0.2.6", default-features = false, features = ["rt-core", "rt-util", "io-driver", "tcp", "uds", "udp", "time", "signal", "stream"] }
|
||||||
|
@@ -8,20 +8,24 @@ use std::{fmt, thread};
|
|||||||
|
|
||||||
use futures_channel::mpsc::{unbounded, UnboundedReceiver, UnboundedSender};
|
use futures_channel::mpsc::{unbounded, UnboundedReceiver, UnboundedSender};
|
||||||
use futures_channel::oneshot::{channel, Canceled, Sender};
|
use futures_channel::oneshot::{channel, Canceled, Sender};
|
||||||
use futures_util::{future::{self, Future, FutureExt}, stream::Stream};
|
use futures_util::{
|
||||||
|
future::{self, Future, FutureExt},
|
||||||
|
stream::Stream,
|
||||||
|
};
|
||||||
|
|
||||||
use crate::runtime::Runtime;
|
use crate::runtime::Runtime;
|
||||||
use crate::system::System;
|
use crate::system::System;
|
||||||
|
|
||||||
use copyless::BoxHelper;
|
use copyless::BoxHelper;
|
||||||
|
|
||||||
|
use smallvec::SmallVec;
|
||||||
pub use tokio::task::JoinHandle;
|
pub use tokio::task::JoinHandle;
|
||||||
|
|
||||||
thread_local!(
|
thread_local!(
|
||||||
static ADDR: RefCell<Option<Arbiter>> = RefCell::new(None);
|
static ADDR: RefCell<Option<Arbiter>> = RefCell::new(None);
|
||||||
static RUNNING: Cell<bool> = Cell::new(false);
|
static RUNNING: Cell<bool> = Cell::new(false);
|
||||||
static Q: RefCell<Vec<Pin<Box<dyn Future<Output = ()>>>>> = RefCell::new(Vec::new());
|
static Q: RefCell<Vec<Pin<Box<dyn Future<Output = ()>>>>> = RefCell::new(Vec::new());
|
||||||
static PENDING: RefCell<Vec<JoinHandle<()>>> = RefCell::new(Vec::new());
|
static PENDING: RefCell<SmallVec<[JoinHandle<()>; 8]>> = RefCell::new(SmallVec::new());
|
||||||
static STORAGE: RefCell<HashMap<TypeId, Box<dyn Any>>> = RefCell::new(HashMap::new());
|
static STORAGE: RefCell<HashMap<TypeId, Box<dyn Any>>> = RefCell::new(HashMap::new());
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -86,6 +90,11 @@ impl Arbiter {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Check if current arbiter is running.
|
||||||
|
pub fn is_running() -> bool {
|
||||||
|
RUNNING.with(|cell| cell.get())
|
||||||
|
}
|
||||||
|
|
||||||
/// Stop arbiter from continuing it's event loop.
|
/// Stop arbiter from continuing it's event loop.
|
||||||
pub fn stop(&self) {
|
pub fn stop(&self) {
|
||||||
let _ = self.sender.unbounded_send(ArbiterCommand::Stop);
|
let _ = self.sender.unbounded_send(ArbiterCommand::Stop);
|
||||||
@@ -173,15 +182,20 @@ impl Arbiter {
|
|||||||
RUNNING.with(move |cell| {
|
RUNNING.with(move |cell| {
|
||||||
if cell.get() {
|
if cell.get() {
|
||||||
// Spawn the future on running executor
|
// Spawn the future on running executor
|
||||||
PENDING.with(move |cell| {
|
let len = PENDING.with(move |cell| {
|
||||||
cell.borrow_mut().push(tokio::task::spawn_local(future));
|
let mut p = cell.borrow_mut();
|
||||||
})
|
p.push(tokio::task::spawn_local(future));
|
||||||
|
p.len()
|
||||||
|
});
|
||||||
|
if len > 7 {
|
||||||
|
// Before reaching the inline size
|
||||||
|
tokio::task::spawn_local(CleanupPending);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// Box the future and push it to the queue, this results in double boxing
|
// Box the future and push it to the queue, this results in double boxing
|
||||||
// because the executor boxes the future again, but works for now
|
// because the executor boxes the future again, but works for now
|
||||||
Q.with(move |cell| {
|
Q.with(move |cell| {
|
||||||
cell.borrow_mut()
|
cell.borrow_mut().push(Pin::from(Box::alloc().init(future)))
|
||||||
.push(unsafe { Pin::new_unchecked(Box::alloc().init(future)) })
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -304,12 +318,36 @@ impl Arbiter {
|
|||||||
/// have completed.
|
/// have completed.
|
||||||
pub fn local_join() -> impl Future<Output = ()> {
|
pub fn local_join() -> impl Future<Output = ()> {
|
||||||
PENDING.with(move |cell| {
|
PENDING.with(move |cell| {
|
||||||
let current = cell.replace(Vec::new());
|
let current = cell.replace(SmallVec::new());
|
||||||
future::join_all(current).map(|_| ())
|
future::join_all(current).map(|_| ())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Future used for cleaning-up already finished `JoinHandle`s
|
||||||
|
/// from the `PENDING` list so the vector doesn't grow indefinitely
|
||||||
|
struct CleanupPending;
|
||||||
|
|
||||||
|
impl Future for CleanupPending {
|
||||||
|
type Output = ();
|
||||||
|
|
||||||
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
|
PENDING.with(move |cell| {
|
||||||
|
let mut pending = cell.borrow_mut();
|
||||||
|
let mut i = 0;
|
||||||
|
while i != pending.len() {
|
||||||
|
if let Poll::Ready(_) = Pin::new(&mut pending[i]).poll(cx) {
|
||||||
|
pending.remove(i);
|
||||||
|
} else {
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Poll::Ready(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
struct ArbiterController {
|
struct ArbiterController {
|
||||||
stop: Option<Sender<i32>>,
|
stop: Option<Sender<i32>>,
|
||||||
rx: UnboundedReceiver<ArbiterCommand>,
|
rx: UnboundedReceiver<ArbiterCommand>,
|
||||||
@@ -343,9 +381,15 @@ impl Future for ArbiterController {
|
|||||||
return Poll::Ready(());
|
return Poll::Ready(());
|
||||||
}
|
}
|
||||||
ArbiterCommand::Execute(fut) => {
|
ArbiterCommand::Execute(fut) => {
|
||||||
PENDING.with(move |cell| {
|
let len = PENDING.with(move |cell| {
|
||||||
cell.borrow_mut().push(tokio::task::spawn_local(fut));
|
let mut p = cell.borrow_mut();
|
||||||
|
p.push(tokio::task::spawn_local(fut));
|
||||||
|
p.len()
|
||||||
});
|
});
|
||||||
|
if len > 7 {
|
||||||
|
// Before reaching the inline size
|
||||||
|
tokio::task::spawn_local(CleanupPending);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
ArbiterCommand::ExecuteFn(f) => {
|
ArbiterCommand::ExecuteFn(f) => {
|
||||||
f.call_box();
|
f.call_box();
|
||||||
|
@@ -79,7 +79,7 @@ impl System {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check if current system is running.
|
/// Check if current system is set, i.e., as already been started.
|
||||||
pub fn is_set() -> bool {
|
pub fn is_set() -> bool {
|
||||||
CURRENT.with(|cell| cell.borrow().is_some())
|
CURRENT.with(|cell| cell.borrow().is_some())
|
||||||
}
|
}
|
||||||
|
@@ -1,5 +1,19 @@
|
|||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn start_and_stop() {
|
||||||
|
actix_rt::System::new("start_and_stop").block_on(async move {
|
||||||
|
assert!(
|
||||||
|
actix_rt::Arbiter::is_running(),
|
||||||
|
"System doesn't seem to have started"
|
||||||
|
);
|
||||||
|
});
|
||||||
|
assert!(
|
||||||
|
!actix_rt::Arbiter::is_running(),
|
||||||
|
"System doesn't seem to have stopped"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn await_for_timer() {
|
fn await_for_timer() {
|
||||||
let time = Duration::from_secs(2);
|
let time = Duration::from_secs(2);
|
@@ -30,7 +30,8 @@ log = "0.4"
|
|||||||
num_cpus = "1.11"
|
num_cpus = "1.11"
|
||||||
mio = "0.6.19"
|
mio = "0.6.19"
|
||||||
net2 = "0.2"
|
net2 = "0.2"
|
||||||
futures = "0.3.1"
|
futures-channel = { version = "0.3.4", default-features = false }
|
||||||
|
futures-util = { version = "0.3.4", default-features = false, features = ["sink"] }
|
||||||
slab = "0.4"
|
slab = "0.4"
|
||||||
|
|
||||||
# unix domain sockets
|
# unix domain sockets
|
||||||
@@ -40,4 +41,4 @@ mio-uds = { version = "0.6.7" }
|
|||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
bytes = "0.5"
|
bytes = "0.5"
|
||||||
env_logger = "0.7"
|
env_logger = "0.7"
|
||||||
actix-testing = "1.0.0"
|
actix-testing = "1.0.0"
|
||||||
|
@@ -6,11 +6,11 @@ use std::{io, mem, net};
|
|||||||
use actix_rt::net::TcpStream;
|
use actix_rt::net::TcpStream;
|
||||||
use actix_rt::time::{delay_until, Instant};
|
use actix_rt::time::{delay_until, Instant};
|
||||||
use actix_rt::{spawn, System};
|
use actix_rt::{spawn, System};
|
||||||
use futures::channel::mpsc::{unbounded, UnboundedReceiver};
|
use futures_channel::mpsc::{unbounded, UnboundedReceiver};
|
||||||
use futures::channel::oneshot;
|
use futures_channel::oneshot;
|
||||||
use futures::future::ready;
|
use futures_util::future::ready;
|
||||||
use futures::stream::FuturesUnordered;
|
use futures_util::stream::FuturesUnordered;
|
||||||
use futures::{ready, Future, FutureExt, Stream, StreamExt};
|
use futures_util::{ready, future::Future, FutureExt, stream::Stream, StreamExt};
|
||||||
use log::{error, info};
|
use log::{error, info};
|
||||||
use net2::TcpBuilder;
|
use net2::TcpBuilder;
|
||||||
|
|
||||||
|
@@ -4,7 +4,7 @@ use std::{fmt, io, net};
|
|||||||
use actix_rt::net::TcpStream;
|
use actix_rt::net::TcpStream;
|
||||||
use actix_service as actix;
|
use actix_service as actix;
|
||||||
use actix_utils::counter::CounterGuard;
|
use actix_utils::counter::CounterGuard;
|
||||||
use futures::future::{ok, Future, FutureExt, LocalBoxFuture};
|
use futures_util::future::{ok, Future, FutureExt, LocalBoxFuture};
|
||||||
use log::error;
|
use log::error;
|
||||||
|
|
||||||
use super::builder::bind_addr;
|
use super::builder::bind_addr;
|
||||||
@@ -218,7 +218,7 @@ impl ServiceRuntime {
|
|||||||
// let name = name.to_owned();
|
// let name = name.to_owned();
|
||||||
if let Some(token) = self.names.get(name) {
|
if let Some(token) = self.names.get(name) {
|
||||||
self.services.insert(
|
self.services.insert(
|
||||||
token.clone(),
|
*token,
|
||||||
Box::new(ServiceFactory {
|
Box::new(ServiceFactory {
|
||||||
inner: service.into_factory(),
|
inner: service.into_factory(),
|
||||||
}),
|
}),
|
||||||
|
@@ -3,9 +3,9 @@ use std::io;
|
|||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::task::{Context, Poll};
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
use futures::channel::mpsc::UnboundedSender;
|
use futures_channel::mpsc::UnboundedSender;
|
||||||
use futures::channel::oneshot;
|
use futures_channel::oneshot;
|
||||||
use futures::FutureExt;
|
use futures_util::FutureExt;
|
||||||
|
|
||||||
use crate::builder::ServerBuilder;
|
use crate::builder::ServerBuilder;
|
||||||
use crate::signals::Signal;
|
use crate::signals::Signal;
|
||||||
|
@@ -6,8 +6,8 @@ use std::time::Duration;
|
|||||||
use actix_rt::spawn;
|
use actix_rt::spawn;
|
||||||
use actix_service::{self as actix, Service, ServiceFactory as ActixServiceFactory};
|
use actix_service::{self as actix, Service, ServiceFactory as ActixServiceFactory};
|
||||||
use actix_utils::counter::CounterGuard;
|
use actix_utils::counter::CounterGuard;
|
||||||
use futures::future::{err, ok, LocalBoxFuture, Ready};
|
use futures_util::future::{err, ok, LocalBoxFuture, Ready};
|
||||||
use futures::{FutureExt, TryFutureExt};
|
use futures_util::{FutureExt, TryFutureExt};
|
||||||
use log::error;
|
use log::error;
|
||||||
|
|
||||||
use super::Token;
|
use super::Token;
|
||||||
|
@@ -3,7 +3,7 @@ use std::io;
|
|||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::task::{Context, Poll};
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
use futures::future::lazy;
|
use futures_util::future::lazy;
|
||||||
|
|
||||||
use crate::server::Server;
|
use crate::server::Server;
|
||||||
|
|
||||||
|
@@ -7,10 +7,10 @@ use std::time;
|
|||||||
use actix_rt::time::{delay_until, Delay, Instant};
|
use actix_rt::time::{delay_until, Delay, Instant};
|
||||||
use actix_rt::{spawn, Arbiter};
|
use actix_rt::{spawn, Arbiter};
|
||||||
use actix_utils::counter::Counter;
|
use actix_utils::counter::Counter;
|
||||||
use futures::channel::mpsc::{unbounded, UnboundedReceiver, UnboundedSender};
|
use futures_channel::mpsc::{unbounded, UnboundedReceiver, UnboundedSender};
|
||||||
use futures::channel::oneshot;
|
use futures_channel::oneshot;
|
||||||
use futures::future::{join_all, LocalBoxFuture, MapOk};
|
use futures_util::future::{join_all, LocalBoxFuture, MapOk};
|
||||||
use futures::{Future, FutureExt, Stream, TryFutureExt};
|
use futures_util::{future::Future, FutureExt, stream::Stream, TryFutureExt};
|
||||||
use log::{error, info, trace};
|
use log::{error, info, trace};
|
||||||
|
|
||||||
use crate::accept::AcceptNotify;
|
use crate::accept::AcceptNotify;
|
||||||
|
@@ -4,7 +4,7 @@ use std::{net, thread, time};
|
|||||||
|
|
||||||
use actix_server::Server;
|
use actix_server::Server;
|
||||||
use actix_service::fn_service;
|
use actix_service::fn_service;
|
||||||
use futures::future::{lazy, ok};
|
use futures_util::future::{lazy, ok};
|
||||||
use net2::TcpBuilder;
|
use net2::TcpBuilder;
|
||||||
|
|
||||||
fn unused_addr() -> net::SocketAddr {
|
fn unused_addr() -> net::SocketAddr {
|
||||||
@@ -71,7 +71,7 @@ fn test_start() {
|
|||||||
use actix_codec::{BytesCodec, Framed};
|
use actix_codec::{BytesCodec, Framed};
|
||||||
use actix_rt::net::TcpStream;
|
use actix_rt::net::TcpStream;
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use futures::SinkExt;
|
use futures_util::sink::SinkExt;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
|
|
||||||
let addr = unused_addr();
|
let addr = unused_addr();
|
||||||
|
@@ -24,4 +24,3 @@ actix-service = "1.0.0"
|
|||||||
|
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
net2 = "0.2"
|
net2 = "0.2"
|
||||||
futures = "0.3.1"
|
|
||||||
|
@@ -48,6 +48,8 @@ pub enum BlockingError<E: fmt::Debug> {
|
|||||||
Canceled,
|
Canceled,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<E: fmt::Debug> std::error::Error for BlockingError<E> {}
|
||||||
|
|
||||||
/// Execute blocking function on a thread pool, returns future that resolves
|
/// Execute blocking function on a thread pool, returns future that resolves
|
||||||
/// to result of the function execution.
|
/// to result of the function execution.
|
||||||
pub fn run<F, I, E>(f: F) -> CpuFuture<I, E>
|
pub fn run<F, I, E>(f: F) -> CpuFuture<I, E>
|
||||||
|
@@ -38,7 +38,7 @@ actix-utils = "1.0.0"
|
|||||||
actix-rt = "1.0.0"
|
actix-rt = "1.0.0"
|
||||||
derive_more = "0.99.2"
|
derive_more = "0.99.2"
|
||||||
either = "1.5.2"
|
either = "1.5.2"
|
||||||
futures = "0.3.1"
|
futures-util = { version = "0.3.4", default-features = false }
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
|
|
||||||
# openssl
|
# openssl
|
||||||
|
@@ -4,7 +4,7 @@ use std::task::{Context, Poll};
|
|||||||
use actix_codec::{AsyncRead, AsyncWrite};
|
use actix_codec::{AsyncRead, AsyncWrite};
|
||||||
use actix_service::{Service, ServiceFactory};
|
use actix_service::{Service, ServiceFactory};
|
||||||
use actix_utils::counter::Counter;
|
use actix_utils::counter::Counter;
|
||||||
use futures::future::{self, FutureExt, LocalBoxFuture, TryFutureExt};
|
use futures_util::future::{self, FutureExt, LocalBoxFuture, TryFutureExt};
|
||||||
pub use native_tls::Error;
|
pub use native_tls::Error;
|
||||||
pub use tokio_tls::{TlsAcceptor, TlsStream};
|
pub use tokio_tls::{TlsAcceptor, TlsStream};
|
||||||
|
|
||||||
|
@@ -9,7 +9,7 @@ pub use tokio_openssl::{HandshakeError, SslStream};
|
|||||||
use actix_codec::{AsyncRead, AsyncWrite};
|
use actix_codec::{AsyncRead, AsyncWrite};
|
||||||
use actix_service::{Service, ServiceFactory};
|
use actix_service::{Service, ServiceFactory};
|
||||||
use actix_utils::counter::{Counter, CounterGuard};
|
use actix_utils::counter::{Counter, CounterGuard};
|
||||||
use futures::future::{ok, FutureExt, LocalBoxFuture, Ready};
|
use futures_util::future::{ok, FutureExt, LocalBoxFuture, Ready};
|
||||||
|
|
||||||
use crate::MAX_CONN_COUNTER;
|
use crate::MAX_CONN_COUNTER;
|
||||||
|
|
||||||
@@ -105,7 +105,7 @@ impl<T: AsyncRead + AsyncWrite + Unpin> Future for AcceptorServiceResponse<T> {
|
|||||||
type Output = Result<SslStream<T>, HandshakeError<T>>;
|
type Output = Result<SslStream<T>, HandshakeError<T>>;
|
||||||
|
|
||||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
let io = futures::ready!(Pin::new(&mut self.fut).poll(cx))?;
|
let io = futures_util::ready!(Pin::new(&mut self.fut).poll(cx))?;
|
||||||
Poll::Ready(Ok(io))
|
Poll::Ready(Ok(io))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -8,7 +8,7 @@ use std::task::{Context, Poll};
|
|||||||
use actix_codec::{AsyncRead, AsyncWrite};
|
use actix_codec::{AsyncRead, AsyncWrite};
|
||||||
use actix_service::{Service, ServiceFactory};
|
use actix_service::{Service, ServiceFactory};
|
||||||
use actix_utils::counter::{Counter, CounterGuard};
|
use actix_utils::counter::{Counter, CounterGuard};
|
||||||
use futures::future::{ok, Ready};
|
use futures_util::future::{ok, Ready};
|
||||||
use tokio_rustls::{Accept, TlsAcceptor};
|
use tokio_rustls::{Accept, TlsAcceptor};
|
||||||
|
|
||||||
pub use rust_tls::{ServerConfig, Session};
|
pub use rust_tls::{ServerConfig, Session};
|
||||||
@@ -108,7 +108,7 @@ impl<T: AsyncRead + AsyncWrite + Unpin> Future for AcceptorServiceFut<T> {
|
|||||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
let this = self.get_mut();
|
let this = self.get_mut();
|
||||||
|
|
||||||
let res = futures::ready!(Pin::new(&mut this.fut).poll(cx));
|
let res = futures_util::ready!(Pin::new(&mut this.fut).poll(cx));
|
||||||
match res {
|
match res {
|
||||||
Ok(io) => Poll::Ready(Ok(io)),
|
Ok(io) => Poll::Ready(Ok(io)),
|
||||||
Err(e) => Poll::Ready(Err(e)),
|
Err(e) => Poll::Ready(Err(e)),
|
||||||
|
@@ -17,7 +17,7 @@ path = "src/lib.rs"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-service = "1.0.4"
|
actix-service = "1.0.4"
|
||||||
futures-util = "0.3.1"
|
futures-util = { version = "0.3.4", default-features = false }
|
||||||
tracing = "0.1"
|
tracing = "0.1"
|
||||||
tracing-futures = "0.2"
|
tracing-futures = "0.2"
|
||||||
|
|
||||||
|
@@ -22,7 +22,9 @@ actix-codec = "0.2.0"
|
|||||||
bitflags = "1.2"
|
bitflags = "1.2"
|
||||||
bytes = "0.5.3"
|
bytes = "0.5.3"
|
||||||
either = "1.5.3"
|
either = "1.5.3"
|
||||||
futures = "0.3.1"
|
futures-channel = { version = "0.3.4", default-features = false }
|
||||||
|
futures-sink = { version = "0.3.4", default-features = false }
|
||||||
|
futures-util = { version = "0.3.4", default-features = false }
|
||||||
pin-project = "0.4.6"
|
pin-project = "0.4.6"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
slab = "0.4"
|
slab = "0.4"
|
||||||
|
@@ -96,7 +96,7 @@ impl Drop for Waiter {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use futures::future::lazy;
|
use futures_util::future::lazy;
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_condition() {
|
async fn test_condition() {
|
||||||
|
@@ -3,7 +3,7 @@ use std::pin::Pin;
|
|||||||
use std::task::{Context, Poll};
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
use actix_service::{Service, ServiceFactory};
|
use actix_service::{Service, ServiceFactory};
|
||||||
use futures::{future, ready, Future};
|
use futures_util::{future, ready, future::Future};
|
||||||
|
|
||||||
/// Combine two different service types into a single type.
|
/// Combine two different service types into a single type.
|
||||||
///
|
///
|
||||||
|
@@ -6,7 +6,7 @@ use std::{fmt, mem};
|
|||||||
|
|
||||||
use actix_codec::{AsyncRead, AsyncWrite, Decoder, Encoder, Framed};
|
use actix_codec::{AsyncRead, AsyncWrite, Decoder, Encoder, Framed};
|
||||||
use actix_service::{IntoService, Service};
|
use actix_service::{IntoService, Service};
|
||||||
use futures::{Future, FutureExt, Stream};
|
use futures_util::{future::Future, FutureExt, stream::Stream};
|
||||||
use log::debug;
|
use log::debug;
|
||||||
|
|
||||||
use crate::mpsc;
|
use crate::mpsc;
|
||||||
@@ -77,6 +77,7 @@ where
|
|||||||
{
|
{
|
||||||
service: S,
|
service: S,
|
||||||
state: State<S, U>,
|
state: State<S, U>,
|
||||||
|
#[pin]
|
||||||
framed: Framed<T, U>,
|
framed: Framed<T, U>,
|
||||||
rx: mpsc::Receiver<Result<Message<<U as Encoder>::Item>, S::Error>>,
|
rx: mpsc::Receiver<Result<Message<<U as Encoder>::Item>, S::Error>>,
|
||||||
tx: mpsc::Sender<Result<Message<<U as Encoder>::Item>, S::Error>>,
|
tx: mpsc::Sender<Result<Message<<U as Encoder>::Item>, S::Error>>,
|
||||||
@@ -169,7 +170,7 @@ where
|
|||||||
&mut self.framed
|
&mut self.framed
|
||||||
}
|
}
|
||||||
|
|
||||||
fn poll_read(&mut self, cx: &mut Context<'_>) -> bool
|
fn poll_read(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> bool
|
||||||
where
|
where
|
||||||
S: Service<Request = Request<U>, Response = Response<U>>,
|
S: Service<Request = Request<U>, Response = Response<U>>,
|
||||||
S::Error: 'static,
|
S::Error: 'static,
|
||||||
@@ -180,29 +181,30 @@ where
|
|||||||
<U as Encoder>::Error: std::fmt::Debug,
|
<U as Encoder>::Error: std::fmt::Debug,
|
||||||
{
|
{
|
||||||
loop {
|
loop {
|
||||||
match self.service.poll_ready(cx) {
|
let this = self.as_mut().project();
|
||||||
|
match this.service.poll_ready(cx) {
|
||||||
Poll::Ready(Ok(_)) => {
|
Poll::Ready(Ok(_)) => {
|
||||||
let item = match self.framed.next_item(cx) {
|
let item = match this.framed.next_item(cx) {
|
||||||
Poll::Ready(Some(Ok(el))) => el,
|
Poll::Ready(Some(Ok(el))) => el,
|
||||||
Poll::Ready(Some(Err(err))) => {
|
Poll::Ready(Some(Err(err))) => {
|
||||||
self.state = State::FramedError(DispatcherError::Decoder(err));
|
*this.state = State::FramedError(DispatcherError::Decoder(err));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
Poll::Pending => return false,
|
Poll::Pending => return false,
|
||||||
Poll::Ready(None) => {
|
Poll::Ready(None) => {
|
||||||
self.state = State::Stopping;
|
*this.state = State::Stopping;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let tx = self.tx.clone();
|
let tx = this.tx.clone();
|
||||||
actix_rt::spawn(self.service.call(item).map(move |item| {
|
actix_rt::spawn(this.service.call(item).map(move |item| {
|
||||||
let _ = tx.send(item.map(Message::Item));
|
let _ = tx.send(item.map(Message::Item));
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
Poll::Pending => return false,
|
Poll::Pending => return false,
|
||||||
Poll::Ready(Err(err)) => {
|
Poll::Ready(Err(err)) => {
|
||||||
self.state = State::Error(DispatcherError::Service(err));
|
*this.state = State::Error(DispatcherError::Service(err));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -210,7 +212,7 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// write to framed object
|
/// write to framed object
|
||||||
fn poll_write(&mut self, cx: &mut Context<'_>) -> bool
|
fn poll_write(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> bool
|
||||||
where
|
where
|
||||||
S: Service<Request = Request<U>, Response = Response<U>>,
|
S: Service<Request = Request<U>, Response = Response<U>>,
|
||||||
S::Error: 'static,
|
S::Error: 'static,
|
||||||
@@ -221,33 +223,34 @@ where
|
|||||||
<U as Encoder>::Error: std::fmt::Debug,
|
<U as Encoder>::Error: std::fmt::Debug,
|
||||||
{
|
{
|
||||||
loop {
|
loop {
|
||||||
while !self.framed.is_write_buf_full() {
|
let mut this = self.as_mut().project();
|
||||||
match Pin::new(&mut self.rx).poll_next(cx) {
|
while !this.framed.is_write_buf_full() {
|
||||||
|
match Pin::new(&mut this.rx).poll_next(cx) {
|
||||||
Poll::Ready(Some(Ok(Message::Item(msg)))) => {
|
Poll::Ready(Some(Ok(Message::Item(msg)))) => {
|
||||||
if let Err(err) = self.framed.write(msg) {
|
if let Err(err) = this.framed.as_mut().write(msg) {
|
||||||
self.state = State::FramedError(DispatcherError::Encoder(err));
|
*this.state = State::FramedError(DispatcherError::Encoder(err));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Poll::Ready(Some(Ok(Message::Close))) => {
|
Poll::Ready(Some(Ok(Message::Close))) => {
|
||||||
self.state = State::FlushAndStop;
|
*this.state = State::FlushAndStop;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
Poll::Ready(Some(Err(err))) => {
|
Poll::Ready(Some(Err(err))) => {
|
||||||
self.state = State::Error(DispatcherError::Service(err));
|
*this.state = State::Error(DispatcherError::Service(err));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
Poll::Ready(None) | Poll::Pending => break,
|
Poll::Ready(None) | Poll::Pending => break,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !self.framed.is_write_buf_empty() {
|
if !this.framed.is_write_buf_empty() {
|
||||||
match self.framed.flush(cx) {
|
match this.framed.flush(cx) {
|
||||||
Poll::Pending => break,
|
Poll::Pending => break,
|
||||||
Poll::Ready(Ok(_)) => (),
|
Poll::Ready(Ok(_)) => (),
|
||||||
Poll::Ready(Err(err)) => {
|
Poll::Ready(Err(err)) => {
|
||||||
debug!("Error sending data: {:?}", err);
|
debug!("Error sending data: {:?}", err);
|
||||||
self.state = State::FramedError(DispatcherError::Encoder(err));
|
*this.state = State::FramedError(DispatcherError::Encoder(err));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -279,7 +282,7 @@ where
|
|||||||
|
|
||||||
return match this.state {
|
return match this.state {
|
||||||
State::Processing => {
|
State::Processing => {
|
||||||
if self.poll_read(cx) || self.poll_write(cx) {
|
if self.as_mut().poll_read(cx) || self.as_mut().poll_write(cx) {
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
Poll::Pending
|
Poll::Pending
|
||||||
@@ -287,12 +290,12 @@ where
|
|||||||
}
|
}
|
||||||
State::Error(_) => {
|
State::Error(_) => {
|
||||||
// flush write buffer
|
// flush write buffer
|
||||||
if !self.framed.is_write_buf_empty() {
|
if !this.framed.is_write_buf_empty() {
|
||||||
if let Poll::Pending = self.framed.flush(cx) {
|
if let Poll::Pending = this.framed.flush(cx) {
|
||||||
return Poll::Pending;
|
return Poll::Pending;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Poll::Ready(Err(self.state.take_error()))
|
Poll::Ready(Err(this.state.take_error()))
|
||||||
}
|
}
|
||||||
State::FlushAndStop => {
|
State::FlushAndStop => {
|
||||||
if !this.framed.is_write_buf_empty() {
|
if !this.framed.is_write_buf_empty() {
|
||||||
|
@@ -4,7 +4,7 @@ use std::pin::Pin;
|
|||||||
use std::task::{Context, Poll};
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
use actix_service::{IntoService, Service, Transform};
|
use actix_service::{IntoService, Service, Transform};
|
||||||
use futures::future::{ok, Ready};
|
use futures_util::future::{ok, Ready};
|
||||||
|
|
||||||
use super::counter::{Counter, CounterGuard};
|
use super::counter::{Counter, CounterGuard};
|
||||||
|
|
||||||
@@ -116,7 +116,7 @@ mod tests {
|
|||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use actix_service::{apply, fn_factory, Service, ServiceFactory};
|
use actix_service::{apply, fn_factory, Service, ServiceFactory};
|
||||||
use futures::future::{lazy, ok, FutureExt, LocalBoxFuture};
|
use futures_util::future::{lazy, ok, FutureExt, LocalBoxFuture};
|
||||||
|
|
||||||
struct SleepService(Duration);
|
struct SleepService(Duration);
|
||||||
|
|
||||||
|
@@ -7,7 +7,7 @@ use std::time::Duration;
|
|||||||
|
|
||||||
use actix_rt::time::{delay_until, Delay, Instant};
|
use actix_rt::time::{delay_until, Delay, Instant};
|
||||||
use actix_service::{Service, ServiceFactory};
|
use actix_service::{Service, ServiceFactory};
|
||||||
use futures::future::{ok, Ready};
|
use futures_util::future::{ok, Ready};
|
||||||
|
|
||||||
use super::time::{LowResTime, LowResTimeService};
|
use super::time::{LowResTime, LowResTimeService};
|
||||||
|
|
||||||
|
@@ -6,7 +6,8 @@ use std::fmt;
|
|||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::task::{Context, Poll};
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
use futures::{Sink, Stream};
|
use futures_sink::Sink;
|
||||||
|
use futures_util::stream::Stream;
|
||||||
|
|
||||||
use crate::cell::Cell;
|
use crate::cell::Cell;
|
||||||
use crate::task::LocalWaker;
|
use crate::task::LocalWaker;
|
||||||
@@ -180,8 +181,8 @@ impl<T> SendError<T> {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use futures::future::lazy;
|
use futures_util::future::lazy;
|
||||||
use futures::{Stream, StreamExt};
|
use futures_util::{stream::Stream, StreamExt};
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_mpsc() {
|
async fn test_mpsc() {
|
||||||
|
@@ -3,7 +3,7 @@ use std::future::Future;
|
|||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::task::{Context, Poll};
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
pub use futures::channel::oneshot::Canceled;
|
pub use futures_channel::oneshot::Canceled;
|
||||||
use slab::Slab;
|
use slab::Slab;
|
||||||
|
|
||||||
use crate::cell::Cell;
|
use crate::cell::Cell;
|
||||||
@@ -253,7 +253,7 @@ impl<T> Future for PReceiver<T> {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use futures::future::lazy;
|
use futures_util::future::lazy;
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_oneshot() {
|
async fn test_oneshot() {
|
||||||
|
@@ -8,7 +8,7 @@ use std::rc::Rc;
|
|||||||
use std::task::{Context, Poll};
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
use actix_service::{IntoService, Service, Transform};
|
use actix_service::{IntoService, Service, Transform};
|
||||||
use futures::future::{ok, Ready};
|
use futures_util::future::{ok, Ready};
|
||||||
|
|
||||||
use crate::oneshot;
|
use crate::oneshot;
|
||||||
use crate::task::LocalWaker;
|
use crate::task::LocalWaker;
|
||||||
@@ -210,8 +210,8 @@ mod tests {
|
|||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use actix_service::Service;
|
use actix_service::Service;
|
||||||
use futures::channel::oneshot;
|
use futures_channel::oneshot;
|
||||||
use futures::future::{lazy, poll_fn, FutureExt, LocalBoxFuture};
|
use futures_util::future::{lazy, poll_fn, FutureExt, LocalBoxFuture};
|
||||||
|
|
||||||
struct Srv;
|
struct Srv;
|
||||||
|
|
||||||
|
@@ -3,7 +3,7 @@ use std::pin::Pin;
|
|||||||
use std::task::{Context, Poll};
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
use actix_service::{IntoService, Service};
|
use actix_service::{IntoService, Service};
|
||||||
use futures::{FutureExt, Stream};
|
use futures_util::{FutureExt, stream::Stream};
|
||||||
|
|
||||||
use crate::mpsc;
|
use crate::mpsc;
|
||||||
|
|
||||||
|
@@ -4,7 +4,7 @@ use std::time::{self, Duration, Instant};
|
|||||||
|
|
||||||
use actix_rt::time::delay_for;
|
use actix_rt::time::delay_for;
|
||||||
use actix_service::{Service, ServiceFactory};
|
use actix_service::{Service, ServiceFactory};
|
||||||
use futures::future::{ok, ready, FutureExt, Ready};
|
use futures_util::future::{ok, ready, FutureExt, Ready};
|
||||||
|
|
||||||
use super::cell::Cell;
|
use super::cell::Cell;
|
||||||
|
|
||||||
|
@@ -10,7 +10,7 @@ use std::{fmt, time};
|
|||||||
|
|
||||||
use actix_rt::time::{delay_for, Delay};
|
use actix_rt::time::{delay_for, Delay};
|
||||||
use actix_service::{IntoService, Service, Transform};
|
use actix_service::{IntoService, Service, Transform};
|
||||||
use futures::future::{ok, Ready};
|
use futures_util::future::{ok, Ready};
|
||||||
|
|
||||||
/// Applies a timeout to requests.
|
/// Applies a timeout to requests.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@@ -183,7 +183,7 @@ mod tests {
|
|||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use actix_service::{apply, fn_factory, Service, ServiceFactory};
|
use actix_service::{apply, fn_factory, Service, ServiceFactory};
|
||||||
use futures::future::{ok, FutureExt, LocalBoxFuture};
|
use futures_util::future::{ok, FutureExt, LocalBoxFuture};
|
||||||
|
|
||||||
struct SleepService(Duration);
|
struct SleepService(Duration);
|
||||||
|
|
||||||
|
@@ -11,7 +11,7 @@ use actix_codec::{AsyncRead, AsyncWrite};
|
|||||||
use actix_rt::System;
|
use actix_rt::System;
|
||||||
use actix_server::{Io, Server};
|
use actix_server::{Io, Server};
|
||||||
use actix_service::{service_fn, NewService};
|
use actix_service::{service_fn, NewService};
|
||||||
use futures::{future, Future};
|
use futures_util::{future, Future};
|
||||||
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
|
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
|
||||||
use tokio_openssl::SslAcceptorExt;
|
use tokio_openssl::SslAcceptorExt;
|
||||||
|
|
||||||
|
@@ -7,7 +7,7 @@ use std::sync::{
|
|||||||
use actix_rt::System;
|
use actix_rt::System;
|
||||||
use actix_server::{ssl, Server};
|
use actix_server::{ssl, Server};
|
||||||
use actix_service::NewService;
|
use actix_service::NewService;
|
||||||
use futures::future;
|
use futures_util::future;
|
||||||
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
|
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@@ -31,7 +31,7 @@ fn set_bit(array: &mut [u8], ch: u8) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
thread_local! {
|
thread_local! {
|
||||||
static DEFAULT_QUOTER: Quoter = { Quoter::new(b"@:", b"/+") };
|
static DEFAULT_QUOTER: Quoter = Quoter::new(b"@:", b"/+");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, Clone, Debug)]
|
#[derive(Default, Clone, Debug)]
|
||||||
|
@@ -1,5 +1,9 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
|
## [0.1.5] - 2020-03-30
|
||||||
|
|
||||||
|
* Serde support
|
||||||
|
|
||||||
## [0.1.4] - 2020-01-14
|
## [0.1.4] - 2020-01-14
|
||||||
|
|
||||||
* Fix `AsRef<str>` impl
|
* Fix `AsRef<str>` impl
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "bytestring"
|
name = "bytestring"
|
||||||
version = "0.1.4"
|
version = "0.1.5"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "A UTF-8 encoded string with Bytes as a storage"
|
description = "A UTF-8 encoded string with Bytes as a storage"
|
||||||
keywords = ["actix"]
|
keywords = ["actix"]
|
||||||
@@ -16,3 +16,7 @@ path = "src/lib.rs"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bytes = "0.5.3"
|
bytes = "0.5.3"
|
||||||
|
serde = { version = "1.0", optional = true }
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
serde_json = "1.0"
|
@@ -1,10 +1,11 @@
|
|||||||
//! A utl-8 encoded read-only string with Bytes as a storage.
|
//! A UTF-8 encoded read-only string using Bytes as storage.
|
||||||
|
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
use std::{borrow, fmt, hash, ops, str};
|
use std::{borrow, fmt, hash, ops, str};
|
||||||
|
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
|
|
||||||
/// A utf-8 encoded string with [`Bytes`] as a storage.
|
/// A UTF-8 encoded string with [`Bytes`] as a storage.
|
||||||
///
|
///
|
||||||
/// [`Bytes`]: https://docs.rs/bytes/0.5.3/bytes/struct.Bytes.html
|
/// [`Bytes`]: https://docs.rs/bytes/0.5.3/bytes/struct.Bytes.html
|
||||||
#[derive(Clone, Eq, Ord, PartialOrd, Default)]
|
#[derive(Clone, Eq, Ord, PartialOrd, Default)]
|
||||||
@@ -159,6 +160,34 @@ impl fmt::Display for ByteString {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "serde")]
|
||||||
|
mod serde {
|
||||||
|
use serde::de::{Deserialize, Deserializer};
|
||||||
|
use serde::ser::{Serialize, Serializer};
|
||||||
|
|
||||||
|
use super::ByteString;
|
||||||
|
|
||||||
|
impl Serialize for ByteString {
|
||||||
|
#[inline]
|
||||||
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: Serializer,
|
||||||
|
{
|
||||||
|
serializer.serialize_str(self.as_ref())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'de> Deserialize<'de> for ByteString {
|
||||||
|
#[inline]
|
||||||
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||||
|
where
|
||||||
|
D: Deserializer<'de>,
|
||||||
|
{
|
||||||
|
String::deserialize(deserializer).map(ByteString::from)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
@@ -222,4 +251,18 @@ mod test {
|
|||||||
fn test_try_from_bytesmut() {
|
fn test_try_from_bytesmut() {
|
||||||
let _ = ByteString::try_from(bytes::BytesMut::from(&b"nice bytes"[..])).unwrap();
|
let _ = ByteString::try_from(bytes::BytesMut::from(&b"nice bytes"[..])).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "serde")]
|
||||||
|
#[test]
|
||||||
|
fn test_serialize() {
|
||||||
|
let s: ByteString = serde_json::from_str(r#""nice bytes""#).unwrap();
|
||||||
|
assert_eq!(s, "nice bytes");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "serde")]
|
||||||
|
#[test]
|
||||||
|
fn test_deserialize() {
|
||||||
|
let s = serde_json::to_string(&ByteString::from_static("nice bytes")).unwrap();
|
||||||
|
assert_eq!(s, r#""nice bytes""#);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user