Skip to content

Commit ac72c85

Browse files
authored
Update to Tokio 1.0 (#628)
* WIP: Update to Tokio 1.0 * ipc: Migrate remaining Unix test code to Tokio 1.0 * core-client: Don't depend on unused hyper/server feature * http: Fix used feature set by hyper * WIP: Bump to the transferred parity-tokio-ipc repo * Use newly released version of parity-tokio-ipc * Remove extra newline in Cargo.toml * Refactor suspension slightly in SuspendableStream
1 parent 609d7a6 commit ac72c85

File tree

11 files changed

+44
-35
lines changed

11 files changed

+44
-35
lines changed

core-client/transports/Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ serde = { version = "1.0", features = ["derive"] }
4444
serde_json = "1.0"
4545
url = "1.7"
4646

47-
hyper = { version = "0.13", optional = true }
48-
hyper-tls = { version = "0.4", optional = true }
47+
hyper = { version = "0.14", features = ["client", "http1"], optional = true }
48+
hyper-tls = { version = "0.5", optional = true }
4949
jsonrpc-server-utils = { version = "17.1", path = "../../server-utils", optional = true }
50-
parity-tokio-ipc = { version = "0.8", optional = true }
51-
tokio = { version = "0.2", optional = true }
50+
parity-tokio-ipc = { version = "0.9", optional = true }
51+
tokio = { version = "1", optional = true }
5252
websocket = { version = "0.24", optional = true }
5353

5454
[dev-dependencies]

http/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ version = "17.1.0"
1212

1313
[dependencies]
1414
futures = "0.3"
15-
hyper = "0.13"
15+
hyper = { version = "0.14", features = ["http1", "tcp", "server", "stream"] }
1616
jsonrpc-core = { version = "17.1", path = "../core" }
1717
jsonrpc-server-utils = { version = "17.1", path = "../server-utils" }
1818
log = "0.4"

ipc/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ log = "0.4"
1515
tower-service = "0.3"
1616
jsonrpc-core = { version = "17.1", path = "../core" }
1717
jsonrpc-server-utils = { version = "17.1", path = "../server-utils", default-features = false }
18-
parity-tokio-ipc = "0.8"
18+
parity-tokio-ipc = "0.9"
1919
parking_lot = "0.11.0"
2020

2121
[dev-dependencies]
2222
env_logger = "0.7"
2323
lazy_static = "1.0"
2424

2525
[target.'cfg(not(windows))'.dev-dependencies]
26-
tokio = { version = "0.2", default-features = false, features = ["uds", "time", "rt-threaded", "io-driver"] }
26+
tokio = { version = "1", default-features = false, features = ["net", "time", "rt-multi-thread"] }
2727

2828
[badges]
2929
travis-ci = { repository = "paritytech/jsonrpc", branch = "master"}

ipc/src/server.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ mod tests {
360360
reply.expect("there should be one reply")
361361
};
362362

363-
let mut rt = tokio::runtime::Runtime::new().unwrap();
363+
let rt = tokio::runtime::Runtime::new().unwrap();
364364
rt.block_on(reply).expect("wait for reply")
365365
}
366366

@@ -609,9 +609,10 @@ mod tests {
609609
tx.send(true).expect("failed to report that the server has stopped");
610610
});
611611

612-
let mut rt = tokio::runtime::Runtime::new().unwrap();
612+
let rt = tokio::runtime::Runtime::new().unwrap();
613613
rt.block_on(async move {
614-
let timeout = tokio::time::delay_for(Duration::from_millis(500));
614+
let timeout = tokio::time::sleep(Duration::from_millis(500));
615+
futures::pin_mut!(timeout);
615616

616617
match futures::future::select(rx, timeout).await {
617618
futures::future::Either::Left((result, _)) => {

server-utils/Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ repository = "https://github.com/paritytech/jsonrpc"
1111
version = "17.1.0"
1212

1313
[dependencies]
14-
bytes = "0.5"
14+
bytes = "1.0"
1515
futures = "0.3"
1616
globset = "0.4"
1717
jsonrpc-core = { version = "17.1", path = "../core" }
1818
lazy_static = "1.1.0"
1919
log = "0.4"
20-
tokio = { version = "0.2", features = ["rt-threaded", "io-driver", "io-util", "time", "tcp"] }
21-
tokio-util = { version = "0.3", features = ["codec"] }
20+
tokio = { version = "1", features = ["rt-multi-thread", "io-util", "time", "net"] }
21+
tokio-util = { version = "0.6", features = ["codec"] }
22+
tokio-stream = { version = "0.1", features = ["net"] }
2223

2324
unicase = "2.0"
2425

server-utils/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ extern crate log;
99
extern crate lazy_static;
1010

1111
pub use tokio;
12+
pub use tokio_stream;
1213
pub use tokio_util;
1314

1415
pub mod cors;

server-utils/src/reactor.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,8 @@ impl RpcEventLoop {
9696
pub fn with_name(name: Option<String>) -> io::Result<Self> {
9797
let (stop, stopped) = futures::channel::oneshot::channel();
9898

99-
let mut tb = runtime::Builder::new();
100-
tb.core_threads(1);
101-
tb.threaded_scheduler();
99+
let mut tb = runtime::Builder::new_multi_thread();
100+
tb.worker_threads(1);
102101
tb.enable_all();
103102

104103
if let Some(name) = name {

server-utils/src/suspendable_stream.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ use std::future::Future;
22
use std::io;
33
use std::pin::Pin;
44
use std::task::Poll;
5-
use std::time::Duration;
6-
7-
use tokio::time::Delay;
5+
use std::time::{Duration, Instant};
86

97
/// `Incoming` is a stream of incoming sockets
108
/// Polling the stream may return a temporary io::Error (for instance if we can't open the connection because of "too many open files" limit)
@@ -19,7 +17,7 @@ pub struct SuspendableStream<S> {
1917
next_delay: Duration,
2018
initial_delay: Duration,
2119
max_delay: Duration,
22-
timeout: Option<Delay>,
20+
suspended_until: Option<Instant>,
2321
}
2422

2523
impl<S> SuspendableStream<S> {
@@ -31,7 +29,7 @@ impl<S> SuspendableStream<S> {
3129
next_delay: Duration::from_millis(20),
3230
initial_delay: Duration::from_millis(10),
3331
max_delay: Duration::from_secs(5),
34-
timeout: None,
32+
suspended_until: None,
3533
}
3634
}
3735
}
@@ -44,10 +42,17 @@ where
4442

4543
fn poll_next(mut self: Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> std::task::Poll<Option<Self::Item>> {
4644
loop {
47-
if let Some(timeout) = self.timeout.as_mut() {
48-
match Pin::new(timeout).poll(cx) {
45+
// If we encountered a connection error before then we suspend
46+
// polling from the underlying stream for a bit
47+
if let Some(deadline) = &mut self.suspended_until {
48+
let deadline = tokio::time::Instant::from_std(*deadline);
49+
let sleep = tokio::time::sleep_until(deadline);
50+
futures::pin_mut!(sleep);
51+
match sleep.poll(cx) {
4952
Poll::Pending => return Poll::Pending,
50-
Poll::Ready(()) => {}
53+
Poll::Ready(()) => {
54+
self.suspended_until = None;
55+
}
5156
}
5257
}
5358

@@ -78,7 +83,7 @@ where
7883
};
7984
debug!("Error accepting connection: {}", err);
8085
debug!("The server will stop accepting connections for {:?}", self.next_delay);
81-
self.timeout = Some(tokio::time::delay_for(self.next_delay));
86+
self.suspended_until = Some(Instant::now() + self.next_delay);
8287
}
8388
}
8489
}

stdio/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ version = "17.1.0"
1313
futures = "0.3"
1414
jsonrpc-core = { version = "17.1", path = "../core" }
1515
log = "0.4"
16-
tokio = { version = "0.2", features = ["io-std", "io-driver", "io-util"] }
17-
tokio-util = { version = "0.3", features = ["codec"] }
16+
tokio = { version = "1", features = ["io-std", "io-util"] }
17+
tokio-util = { version = "0.6", features = ["codec"] }
1818

1919
[dev-dependencies]
20-
tokio = { version = "0.2", features = ["rt-core", "macros"] }
20+
tokio = { version = "1", features = ["rt", "macros"] }
2121
lazy_static = "1.0"
2222
env_logger = "0.7"
2323

tcp/src/server.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use tower_service::Service as _;
77

88
use crate::futures::{self, future};
99
use crate::jsonrpc::{middleware, MetaIoHandler, Metadata, Middleware};
10+
use crate::server_utils::tokio_stream::wrappers::TcpListenerStream;
1011
use crate::server_utils::{codecs, reactor, tokio, tokio_util::codec::Framed, SuspendableStream};
1112

1213
use crate::dispatch::{Dispatcher, PeerMessageQueue, SenderChannels};
@@ -94,6 +95,7 @@ where
9495
executor.executor().spawn(async move {
9596
let start = async {
9697
let listener = tokio::net::TcpListener::bind(&address).await?;
98+
let listener = TcpListenerStream::new(listener);
9799
let connections = SuspendableStream::new(listener);
98100

99101
let server = connections.map(|socket| {

tcp/src/tests.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::net::{Shutdown, SocketAddr};
1+
use std::net::SocketAddr;
22
use std::str::FromStr;
33
use std::sync::Arc;
44
use std::time::Duration;
@@ -23,7 +23,7 @@ fn casual_server() -> ServerBuilder {
2323
}
2424

2525
fn run_future<O>(fut: impl std::future::Future<Output = O> + Send) -> O {
26-
let mut rt = tokio::runtime::Runtime::new().unwrap();
26+
let rt = tokio::runtime::Runtime::new().unwrap();
2727
rt.block_on(fut)
2828
}
2929

@@ -60,9 +60,9 @@ fn disconnect() {
6060
let _server = server.start(&addr).expect("Server must run with no issues");
6161

6262
run_future(async move {
63-
let stream = TcpStream::connect(&addr).await.unwrap();
63+
let mut stream = TcpStream::connect(&addr).await.unwrap();
6464
assert_eq!(stream.peer_addr().unwrap(), addr);
65-
stream.shutdown(::std::net::Shutdown::Both).unwrap();
65+
stream.shutdown().await.unwrap();
6666
});
6767

6868
::std::thread::sleep(::std::time::Duration::from_millis(50));
@@ -76,7 +76,7 @@ fn dummy_request(addr: &SocketAddr, data: Vec<u8>) -> Vec<u8> {
7676
let stream = async move {
7777
let mut stream = TcpStream::connect(addr).await?;
7878
stream.write_all(&data).await?;
79-
stream.shutdown(Shutdown::Write)?;
79+
stream.shutdown().await?;
8080
let mut read_buf = vec![];
8181
let _ = stream.read_to_end(&mut read_buf).await;
8282

@@ -243,7 +243,7 @@ fn message() {
243243

244244
let client = async move {
245245
let stream = TcpStream::connect(&addr);
246-
let delay = tokio::time::delay_for(Duration::from_millis(500));
246+
let delay = tokio::time::sleep(Duration::from_millis(500));
247247
let (stream, _) = futures::join!(stream, delay);
248248
let mut stream = stream?;
249249

@@ -272,7 +272,7 @@ fn message() {
272272
let data = b"{\"jsonrpc\": \"2.0\", \"method\": \"say_hello\", \"params\": [42, 23], \"id\": 1}\n";
273273
stream.write_all(&data[..]).await?;
274274

275-
stream.shutdown(Shutdown::Write).unwrap();
275+
stream.shutdown().await.unwrap();
276276
let mut read_buf = vec![];
277277
let _ = stream.read_to_end(&mut read_buf).await?;
278278

0 commit comments

Comments
 (0)