mirror of
https://github.com/fafhrd91/actix-net
synced 2025-08-13 23:38:23 +02:00
Compare commits
25 Commits
server-v1.
...
connect-v2
Author | SHA1 | Date | |
---|---|---|---|
|
4be11b541b | ||
|
baba533407 | ||
|
2bf50826b0 | ||
|
41b2a3b2e2 | ||
|
7fdd4a1118 | ||
|
cb30f9e86a | ||
|
873f69be51 | ||
|
0967061f30 | ||
|
59902cb3a3 | ||
|
857e50120b | ||
|
36a2edf1cd | ||
|
346bd072d3 | ||
|
8d3d58b3b7 | ||
|
c41b5d8dd4 | ||
|
693d5132a9 | ||
|
f7dac3feb4 | ||
|
ebc11d03f2 | ||
|
e3ad5de270 | ||
|
91118bb2ce | ||
|
6628688bcf | ||
|
b9567359fd | ||
|
7dbc0264b1 | ||
|
1b7c969f6a | ||
|
f1685d8253 | ||
|
e3b6a33b97 |
54
.github/workflows/windows-mingw.yml
vendored
54
.github/workflows/windows-mingw.yml
vendored
@@ -1,54 +0,0 @@
|
|||||||
name: CI (Windows-mingw)
|
|
||||||
|
|
||||||
on: [push, pull_request]
|
|
||||||
|
|
||||||
env:
|
|
||||||
OPENSSL_DIR: d:\a\_temp\msys\msys64\usr
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
build_and_test:
|
|
||||||
strategy:
|
|
||||||
fail-fast: false
|
|
||||||
matrix:
|
|
||||||
version:
|
|
||||||
- stable
|
|
||||||
- nightly
|
|
||||||
|
|
||||||
name: ${{ matrix.version }} - x86_64-pc-windows-gnu
|
|
||||||
runs-on: windows-latest
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@master
|
|
||||||
|
|
||||||
- name: Install ${{ matrix.version }}
|
|
||||||
uses: actions-rs/toolchain@v1
|
|
||||||
with:
|
|
||||||
toolchain: ${{ matrix.version }}-x86_64-pc-windows-gnu
|
|
||||||
profile: minimal
|
|
||||||
override: true
|
|
||||||
|
|
||||||
- name: Install MSYS2
|
|
||||||
uses: numworks/setup-msys2@v1
|
|
||||||
|
|
||||||
- name: Install OpenSSL
|
|
||||||
run: |
|
|
||||||
msys2do pacman --noconfirm -S openssl-devel pkg-config
|
|
||||||
|
|
||||||
- name: Copy and check libs
|
|
||||||
run: |
|
|
||||||
Copy-Item d:\a\_temp\msys\msys64\usr\lib\libssl.dll.a d:\a\_temp\msys\msys64\usr\lib\libssl.dll
|
|
||||||
Copy-Item d:\a\_temp\msys\msys64\usr\lib\libcrypto.dll.a d:\a\_temp\msys\msys64\usr\lib\libcrypto.dll
|
|
||||||
Get-ChildItem d:\a\_temp\msys\msys64\usr\lib
|
|
||||||
Get-ChildItem d:\a\_temp\msys\msys64\usr
|
|
||||||
|
|
||||||
- name: check build
|
|
||||||
uses: actions-rs/cargo@v1
|
|
||||||
with:
|
|
||||||
command: check
|
|
||||||
args: --all --bins --examples --tests
|
|
||||||
|
|
||||||
- name: tests
|
|
||||||
uses: actions-rs/cargo@v1
|
|
||||||
with:
|
|
||||||
command: test
|
|
||||||
args: --all --all-features --no-fail-fast -- --nocapture
|
|
@@ -23,4 +23,5 @@ futures-core = "0.3.1"
|
|||||||
futures-sink = "0.3.1"
|
futures-sink = "0.3.1"
|
||||||
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,23 @@
|
|||||||
# 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
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
* Update `rustls` dependency to 0.17
|
||||||
|
* Update `tokio-rustls` dependency to 0.13
|
||||||
|
|
||||||
## [1.0.2] - 2020-01-15
|
## [1.0.2] - 2020-01-15
|
||||||
|
|
||||||
* Fix actix-service 1.0.3 compatibility
|
* Fix actix-service 1.0.3 compatibility
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-connect"
|
name = "actix-connect"
|
||||||
version = "1.0.2"
|
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"]
|
||||||
@@ -40,16 +40,16 @@ either = "1.5.3"
|
|||||||
futures = "0.3.1"
|
futures = "0.3.1"
|
||||||
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 }
|
||||||
tokio-openssl = { version = "0.4.0", optional = true }
|
tokio-openssl = { version = "0.4.0", optional = true }
|
||||||
|
|
||||||
# rustls
|
# rustls
|
||||||
rust-tls = { version = "0.16.0", package = "rustls", optional = true }
|
rust-tls = { version = "0.17.0", package = "rustls", optional = true }
|
||||||
tokio-rustls = { version = "0.12.0", optional = true }
|
tokio-rustls = { version = "0.13.0", optional = true }
|
||||||
webpki = { version = "0.21", optional = true }
|
webpki = { version = "0.21", optional = true }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
@@ -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;
|
||||||
|
|
||||||
|
@@ -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
|
||||||
|
@@ -6,8 +6,8 @@ use std::task::{Context, Poll};
|
|||||||
|
|
||||||
use actix_service::{Service, ServiceFactory};
|
use actix_service::{Service, ServiceFactory};
|
||||||
use futures::future::{ok, Either, Ready};
|
use futures::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),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -6,7 +6,7 @@ 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::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};
|
||||||
|
@@ -11,7 +11,7 @@ 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::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,
|
||||||
|
@@ -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>(())
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@@ -27,5 +27,5 @@ pin-project = "0.4.6"
|
|||||||
log = "0.4"
|
log = "0.4"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-connect = "1.0.0"
|
actix-connect = "2.0.0-alpha.2"
|
||||||
actix-testing = "1.0.0"
|
actix-testing = "1.0.0"
|
||||||
|
@@ -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>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -6,6 +6,7 @@ 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::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(())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -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))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -14,7 +14,7 @@ workspace = ".."
|
|||||||
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]
|
||||||
|
@@ -4,6 +4,8 @@
|
|||||||
|
|
||||||
- Expose `System::is_set` to check if current system is running
|
- Expose `System::is_set` to check if current system is running
|
||||||
|
|
||||||
|
- Add `Arbiter::local_join` associated function to get be able to `await` for spawned futures
|
||||||
|
|
||||||
## [1.0.0] - 2019-12-11
|
## [1.0.0] - 2019-12-11
|
||||||
|
|
||||||
* Update dependencies
|
* Update dependencies
|
||||||
|
@@ -18,6 +18,7 @@ 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 = "0.3.1"
|
futures-channel = { version = "0.3.1", default-features = false }
|
||||||
|
futures-util = { version = "0.3.1", default-features = false }
|
||||||
copyless = "0.1.4"
|
copyless = "0.1.4"
|
||||||
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"] }
|
||||||
|
@@ -6,19 +6,22 @@ use std::sync::atomic::{AtomicUsize, Ordering};
|
|||||||
use std::task::{Context, Poll};
|
use std::task::{Context, Poll};
|
||||||
use std::{fmt, thread};
|
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::{future, Future, FutureExt, 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;
|
||||||
|
|
||||||
|
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 STORAGE: RefCell<HashMap<TypeId, Box<dyn Any>>> = RefCell::new(HashMap::new());
|
static STORAGE: RefCell<HashMap<TypeId, Box<dyn Any>>> = RefCell::new(HashMap::new());
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -170,13 +173,15 @@ 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
|
||||||
tokio::task::spawn_local(future);
|
PENDING.with(move |cell| {
|
||||||
|
cell.borrow_mut().push(tokio::task::spawn_local(future));
|
||||||
|
})
|
||||||
} 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(unsafe { Pin::new_unchecked(Box::alloc().init(future)) })
|
.push(Pin::from(Box::alloc().init(future)))
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -294,6 +299,15 @@ impl Arbiter {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns a future that will be completed once all currently spawned futures
|
||||||
|
/// have completed.
|
||||||
|
pub fn local_join() -> impl Future<Output = ()> {
|
||||||
|
PENDING.with(move |cell| {
|
||||||
|
let current = cell.replace(Vec::new());
|
||||||
|
future::join_all(current).map(|_| ())
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ArbiterController {
|
struct ArbiterController {
|
||||||
@@ -329,7 +343,9 @@ impl Future for ArbiterController {
|
|||||||
return Poll::Ready(());
|
return Poll::Ready(());
|
||||||
}
|
}
|
||||||
ArbiterCommand::Execute(fut) => {
|
ArbiterCommand::Execute(fut) => {
|
||||||
tokio::task::spawn_local(fut);
|
PENDING.with(move |cell| {
|
||||||
|
cell.borrow_mut().push(tokio::task::spawn_local(fut));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
ArbiterCommand::ExecuteFn(f) => {
|
ArbiterCommand::ExecuteFn(f) => {
|
||||||
f.call_box();
|
f.call_box();
|
||||||
|
@@ -1,9 +1,9 @@
|
|||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::io;
|
use std::io;
|
||||||
|
|
||||||
use futures::channel::mpsc::unbounded;
|
use futures_channel::mpsc::unbounded;
|
||||||
use futures::channel::oneshot::{channel, Receiver};
|
use futures_channel::oneshot::{channel, Receiver};
|
||||||
use futures::future::{lazy, Future, FutureExt};
|
use futures_util::future::{lazy, Future, FutureExt};
|
||||||
use tokio::task::LocalSet;
|
use tokio::task::LocalSet;
|
||||||
|
|
||||||
use crate::arbiter::{Arbiter, SystemArbiter};
|
use crate::arbiter::{Arbiter, SystemArbiter};
|
||||||
|
@@ -25,7 +25,7 @@ pub use actix_threadpool as blocking;
|
|||||||
/// This function panics if actix system is not running.
|
/// This function panics if actix system is not running.
|
||||||
pub fn spawn<F>(f: F)
|
pub fn spawn<F>(f: F)
|
||||||
where
|
where
|
||||||
F: futures::Future<Output = ()> + 'static,
|
F: futures_util::future::Future<Output = ()> + 'static,
|
||||||
{
|
{
|
||||||
if !System::is_set() {
|
if !System::is_set() {
|
||||||
panic!("System is not running");
|
panic!("System is not running");
|
||||||
|
@@ -3,7 +3,7 @@ use std::future::Future;
|
|||||||
use std::io;
|
use std::io;
|
||||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||||
|
|
||||||
use futures::channel::mpsc::UnboundedSender;
|
use futures_channel::mpsc::UnboundedSender;
|
||||||
use tokio::task::LocalSet;
|
use tokio::task::LocalSet;
|
||||||
|
|
||||||
use crate::arbiter::{Arbiter, SystemCommand};
|
use crate::arbiter::{Arbiter, SystemCommand};
|
||||||
|
100
actix-rt/tests/wait_spawned.rs
Normal file
100
actix-rt/tests/wait_spawned.rs
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn await_for_timer() {
|
||||||
|
let time = Duration::from_secs(2);
|
||||||
|
let instant = Instant::now();
|
||||||
|
actix_rt::System::new("test_wait_timer").block_on(async move {
|
||||||
|
tokio::time::delay_for(time).await;
|
||||||
|
});
|
||||||
|
assert!(
|
||||||
|
instant.elapsed() >= time,
|
||||||
|
"Block on should poll awaited future to completion"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn join_another_arbiter() {
|
||||||
|
let time = Duration::from_secs(2);
|
||||||
|
let instant = Instant::now();
|
||||||
|
actix_rt::System::new("test_join_another_arbiter").block_on(async move {
|
||||||
|
let mut arbiter = actix_rt::Arbiter::new();
|
||||||
|
arbiter.send(Box::pin(async move {
|
||||||
|
tokio::time::delay_for(time).await;
|
||||||
|
actix_rt::Arbiter::current().stop();
|
||||||
|
}));
|
||||||
|
arbiter.join().unwrap();
|
||||||
|
});
|
||||||
|
assert!(
|
||||||
|
instant.elapsed() >= time,
|
||||||
|
"Join on another arbiter should complete only when it calls stop"
|
||||||
|
);
|
||||||
|
|
||||||
|
let instant = Instant::now();
|
||||||
|
actix_rt::System::new("test_join_another_arbiter").block_on(async move {
|
||||||
|
let mut arbiter = actix_rt::Arbiter::new();
|
||||||
|
arbiter.exec_fn(move || {
|
||||||
|
actix_rt::spawn(async move {
|
||||||
|
tokio::time::delay_for(time).await;
|
||||||
|
actix_rt::Arbiter::current().stop();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
arbiter.join().unwrap();
|
||||||
|
});
|
||||||
|
assert!(
|
||||||
|
instant.elapsed() >= time,
|
||||||
|
"Join on a arbiter that has used actix_rt::spawn should wait for said future"
|
||||||
|
);
|
||||||
|
|
||||||
|
let instant = Instant::now();
|
||||||
|
actix_rt::System::new("test_join_another_arbiter").block_on(async move {
|
||||||
|
let mut arbiter = actix_rt::Arbiter::new();
|
||||||
|
arbiter.send(Box::pin(async move {
|
||||||
|
tokio::time::delay_for(time).await;
|
||||||
|
actix_rt::Arbiter::current().stop();
|
||||||
|
}));
|
||||||
|
arbiter.stop();
|
||||||
|
arbiter.join().unwrap();
|
||||||
|
});
|
||||||
|
assert!(
|
||||||
|
instant.elapsed() < time,
|
||||||
|
"Premature stop of arbiter should conclude regardless of it's current state"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn join_current_arbiter() {
|
||||||
|
let time = Duration::from_secs(2);
|
||||||
|
|
||||||
|
let instant = Instant::now();
|
||||||
|
actix_rt::System::new("test_join_current_arbiter").block_on(async move {
|
||||||
|
actix_rt::spawn(async move {
|
||||||
|
tokio::time::delay_for(time).await;
|
||||||
|
actix_rt::Arbiter::current().stop();
|
||||||
|
});
|
||||||
|
actix_rt::Arbiter::local_join().await;
|
||||||
|
});
|
||||||
|
assert!(
|
||||||
|
instant.elapsed() >= time,
|
||||||
|
"Join on current arbiter should wait for all spawned futures"
|
||||||
|
);
|
||||||
|
|
||||||
|
let large_timer = Duration::from_secs(20);
|
||||||
|
let instant = Instant::now();
|
||||||
|
actix_rt::System::new("test_join_current_arbiter").block_on(async move {
|
||||||
|
actix_rt::spawn(async move {
|
||||||
|
tokio::time::delay_for(time).await;
|
||||||
|
actix_rt::Arbiter::current().stop();
|
||||||
|
});
|
||||||
|
let f = actix_rt::Arbiter::local_join();
|
||||||
|
actix_rt::spawn(async move {
|
||||||
|
tokio::time::delay_for(large_timer).await;
|
||||||
|
actix_rt::Arbiter::current().stop();
|
||||||
|
});
|
||||||
|
f.await;
|
||||||
|
});
|
||||||
|
assert!(
|
||||||
|
instant.elapsed() < large_timer,
|
||||||
|
"local_join should await only for the already spawned futures"
|
||||||
|
);
|
||||||
|
}
|
@@ -1,5 +1,13 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
|
## [2.0.0-alpha.1] - 2020-03-03
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
* Update `rustls` dependency to 0.17
|
||||||
|
* Update `tokio-rustls` dependency to 0.13
|
||||||
|
* Update `webpki-roots` dependency to 0.19
|
||||||
|
|
||||||
## [1.0.0] - 2019-12-11
|
## [1.0.0] - 2019-12-11
|
||||||
|
|
||||||
* 1.0.0 release
|
* 1.0.0 release
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-tls"
|
name = "actix-tls"
|
||||||
version = "1.0.0"
|
version = "2.0.0-alpha.1"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Actix tls services"
|
description = "Actix tls services"
|
||||||
keywords = ["network", "framework", "async", "futures"]
|
keywords = ["network", "framework", "async", "futures"]
|
||||||
@@ -46,10 +46,10 @@ open-ssl = { version="0.10", package = "openssl", optional = true }
|
|||||||
tokio-openssl = { version = "0.4.0", optional = true }
|
tokio-openssl = { version = "0.4.0", optional = true }
|
||||||
|
|
||||||
# rustls
|
# rustls
|
||||||
rust-tls = { version = "0.16.0", package = "rustls", optional = true }
|
rust-tls = { version = "0.17.0", package = "rustls", optional = true }
|
||||||
webpki = { version = "0.21", optional = true }
|
webpki = { version = "0.21", optional = true }
|
||||||
webpki-roots = { version = "0.17", optional = true }
|
webpki-roots = { version = "0.19", optional = true }
|
||||||
tokio-rustls = { version = "0.12.0", optional = true }
|
tokio-rustls = { version = "0.13.0", optional = true }
|
||||||
|
|
||||||
# native-tls
|
# native-tls
|
||||||
native-tls = { version="0.2", optional = true }
|
native-tls = { version="0.2", optional = true }
|
||||||
|
@@ -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() {
|
||||||
|
Reference in New Issue
Block a user