diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2019-12-15 11:47:26 +0100 |
---|---|---|
committer | Ry Dahl <ry@tinyclouds.org> | 2019-12-15 18:47:26 +0800 |
commit | e1eb458cad8caf5a0f08ad1d44caba7d557daf92 (patch) | |
tree | 27d26c56c8518e31d3b4e3a3dc311c1c21e0c5d0 | |
parent | 33d2e3d53601c4e56e4ac56b75e336cf1152ad08 (diff) |
upgrade: tokio 0.2 in deno_core_http_bench, take2 (#3435)
-rw-r--r-- | Cargo.lock | 85 | ||||
-rw-r--r-- | core/Cargo.toml | 3 | ||||
-rw-r--r-- | core/examples/http_bench.rs | 232 |
3 files changed, 213 insertions, 107 deletions
diff --git a/Cargo.lock b/Cargo.lock index d8ccb8b90..ee14d8d71 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -132,6 +132,11 @@ dependencies = [ ] [[package]] +name = "bytes" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] name = "c2-chacha" version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -276,8 +281,9 @@ dependencies = [ "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", + "num_cpus 1.11.1 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.41 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio 0.1.22 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", "url 1.7.2 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -505,7 +511,7 @@ version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "futures 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)", - "num_cpus 1.11.0 (registry+https://github.com/rust-lang/crates.io-index)", + "num_cpus 1.11.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -516,7 +522,7 @@ dependencies = [ "futures-core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "futures-task 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "futures-util 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", - "num_cpus 1.11.0 (registry+https://github.com/rust-lang/crates.io-index)", + "num_cpus 1.11.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -827,9 +833,10 @@ dependencies = [ [[package]] name = "mio" -version = "0.6.19" +version = "0.6.21" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ + "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", "fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "iovec 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", @@ -848,7 +855,7 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", - "mio 0.6.19 (registry+https://github.com/rust-lang/crates.io-index)", + "mio 0.6.21 (registry+https://github.com/rust-lang/crates.io-index)", "miow 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -860,7 +867,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "iovec 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", - "mio 0.6.19 (registry+https://github.com/rust-lang/crates.io-index)", + "mio 0.6.21 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -916,7 +923,7 @@ dependencies = [ [[package]] name = "num_cpus" -version = "1.11.0" +version = "1.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "hermit-abi 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", @@ -967,6 +974,11 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] +name = "pin-project-lite" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] name = "pin-utils" version = "0.1.0-alpha.4" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1577,8 +1589,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)", - "mio 0.6.19 (registry+https://github.com/rust-lang/crates.io-index)", - "num_cpus 1.11.0 (registry+https://github.com/rust-lang/crates.io-index)", + "mio 0.6.21 (registry+https://github.com/rust-lang/crates.io-index)", + "num_cpus 1.11.1 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-codec 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-current-thread 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-executor 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1594,6 +1606,28 @@ dependencies = [ ] [[package]] +name = "tokio" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bytes 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", + "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "mio 0.6.21 (registry+https://github.com/rust-lang/crates.io-index)", + "mio-named-pipes 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "mio-uds 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)", + "num_cpus 1.11.1 (registry+https://github.com/rust-lang/crates.io-index)", + "pin-project-lite 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "signal-hook-registry 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-macros 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "tokio-buf" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1652,6 +1686,15 @@ dependencies = [ ] [[package]] +name = "tokio-macros" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "tokio-process" version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1661,7 +1704,7 @@ dependencies = [ "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", - "mio 0.6.19 (registry+https://github.com/rust-lang/crates.io-index)", + "mio 0.6.21 (registry+https://github.com/rust-lang/crates.io-index)", "mio-named-pipes 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-reactor 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1678,8 +1721,8 @@ dependencies = [ "futures 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", - "mio 0.6.19 (registry+https://github.com/rust-lang/crates.io-index)", - "num_cpus 1.11.0 (registry+https://github.com/rust-lang/crates.io-index)", + "mio 0.6.21 (registry+https://github.com/rust-lang/crates.io-index)", + "num_cpus 1.11.1 (registry+https://github.com/rust-lang/crates.io-index)", "parking_lot 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-executor 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1707,7 +1750,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "futures 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", - "mio 0.6.19 (registry+https://github.com/rust-lang/crates.io-index)", + "mio 0.6.21 (registry+https://github.com/rust-lang/crates.io-index)", "mio-uds 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)", "signal-hook 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-executor 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1733,7 +1776,7 @@ dependencies = [ "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)", "iovec 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", - "mio 0.6.19 (registry+https://github.com/rust-lang/crates.io-index)", + "mio 0.6.21 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-reactor 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1749,7 +1792,7 @@ dependencies = [ "futures 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", - "num_cpus 1.11.0 (registry+https://github.com/rust-lang/crates.io-index)", + "num_cpus 1.11.1 (registry+https://github.com/rust-lang/crates.io-index)", "slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-executor 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1773,7 +1816,7 @@ dependencies = [ "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", - "mio 0.6.19 (registry+https://github.com/rust-lang/crates.io-index)", + "mio 0.6.21 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-codec 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-reactor 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1789,7 +1832,7 @@ dependencies = [ "iovec 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", - "mio 0.6.19 (registry+https://github.com/rust-lang/crates.io-index)", + "mio 0.6.21 (registry+https://github.com/rust-lang/crates.io-index)", "mio-uds 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-codec 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2120,6 +2163,7 @@ dependencies = [ "checksum bumpalo 2.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ad807f2fc2bf185eeb98ff3a901bd46dc5ad58163d0fa4577ba0d25674d71708" "checksum byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a7c3dd8985a7111efc5c80b44e23ecdd8c007de8ade3b96595387e812b957cf5" "checksum bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)" = "206fdffcfa2df7cbe15601ef46c813fce0965eb3286db6b56c583b814b51c81c" +"checksum bytes 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1c85319f157e4e26c703678e68e26ab71a46c0199286fa670b21cc9fec13d895" "checksum c2-chacha 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "214238caa1bf3a496ec3392968969cab8549f96ff30652c9e56885329315f6bb" "checksum cc 1.0.47 (registry+https://github.com/rust-lang/crates.io-index)" = "aa87058dce70a3ff5621797f1506cb837edd02ac4c0ae642b4542dce802908b8" "checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" @@ -2190,7 +2234,7 @@ dependencies = [ "checksum mime 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)" = "dd1d63acd1b78403cc0c325605908475dd9b9a3acbf65ed8bcab97e27014afcf" "checksum mime_guess 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1a0ed03949aef72dbdf3116a383d7b38b4768e6f960528cd6a6044aa9ed68599" "checksum miniz_oxide 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "6f3f74f726ae935c3f514300cc6773a0c9492abc5e972d42ba0c0ebb88757625" -"checksum mio 0.6.19 (registry+https://github.com/rust-lang/crates.io-index)" = "83f51996a3ed004ef184e16818edc51fadffe8e7ca68be67f9dee67d84d0ff23" +"checksum mio 0.6.21 (registry+https://github.com/rust-lang/crates.io-index)" = "302dec22bcf6bae6dfb69c647187f4b4d0fb6f535521f7bc022430ce8e12008f" "checksum mio-named-pipes 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "f5e374eff525ce1c5b7687c4cef63943e7686524a387933ad27ca7ec43779cb3" "checksum mio-uds 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)" = "966257a94e196b11bb43aca423754d87429960a768de9414f3691d6957abf125" "checksum miow 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f2f3b1cf331de6896aabf6e9d55dca90356cc9960cca7eaaf408a355ae919" @@ -2198,12 +2242,13 @@ dependencies = [ "checksum net2 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)" = "42550d9fb7b6684a6d404d9fa7250c2eb2646df731d1c06afc06dcee9e1bcf88" "checksum nix 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)" = "6c722bee1037d430d0f8e687bbdbf222f27cc6e4e68d5caf630857bb2b6dbdce" "checksum nom 4.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2ad2a91a8e869eeb30b9cb3119ae87773a8f4ae617f41b1eb9c154b2905f7bd6" -"checksum num_cpus 1.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "155394f924cdddf08149da25bfb932d226b4a593ca7468b08191ff6335941af5" +"checksum num_cpus 1.11.1 (registry+https://github.com/rust-lang/crates.io-index)" = "76dac5ed2a876980778b8b85f75a71b6cbf0db0b1232ee12f826bccb00d09d72" "checksum os_pipe 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "db4d06355a7090ce852965b2d08e11426c315438462638c6d721448d0b47aa22" "checksum parking_lot 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f842b1982eb6c2fe34036a4fbfb06dd185a3f5c8edfaacdf7d1ea10b07de6252" "checksum parking_lot_core 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b876b1b9e7ac6e1a74a6da34d25c42e17e8862aa409cbbbdcfc8d86c6f3bc62b" "checksum percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "31010dd2e1ac33d5b46a5b413495239882813e0369f8ed8a5e266f173602f831" "checksum percent-encoding 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" +"checksum pin-project-lite 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f0af6cbca0e6e3ce8692ee19fb8d734b641899e07b68eb73e9bbbd32f1703991" "checksum pin-utils 0.1.0-alpha.4 (registry+https://github.com/rust-lang/crates.io-index)" = "5894c618ce612a3fa23881b152b608bafb8c56cfc22f434a3ba3120b40f7b587" "checksum ppv-lite86 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "74490b50b9fbe561ac330df47c08f3f33073d2d00c150f719147d7c54522fa1b" "checksum proc-macro-hack 0.5.11 (registry+https://github.com/rust-lang/crates.io-index)" = "ecd45702f76d6d3c75a80564378ae228a85f0b59d2f3ed43c91b4a69eb2ebfc5" @@ -2270,12 +2315,14 @@ dependencies = [ "checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" "checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f" "checksum tokio 0.1.22 (registry+https://github.com/rust-lang/crates.io-index)" = "5a09c0b5bb588872ab2f09afa13ee6e9dac11e10a0ec9e8e3ba39a5a5d530af6" +"checksum tokio 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "bcced6bb623d4bff3739c176c415f13c418f426395c169c9c3cd9a492c715b16" "checksum tokio-buf 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8fb220f46c53859a4b7ec083e41dec9778ff0b1851c0942b211edb89e0ccdc46" "checksum tokio-codec 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "5c501eceaf96f0e1793cf26beb63da3d11c738c4a943fdf3746d81d64684c39f" "checksum tokio-current-thread 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "d16217cad7f1b840c5a97dfb3c43b0c871fef423a6e8d2118c604e843662a443" "checksum tokio-executor 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "0f27ee0e6db01c5f0b2973824547ce7e637b2ed79b891a9677b0de9bd532b6ac" "checksum tokio-fs 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "3fe6dc22b08d6993916647d108a1a7d15b9cd29c4f4496c62b92c45b5041b7af" "checksum tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "5090db468dad16e1a7a54c8c67280c5e4b544f3d3e018f0b913b400261f85926" +"checksum tokio-macros 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d5795a71419535c6dcecc9b6ca95bdd3c2d6142f7e8343d7beb9923f129aa87e" "checksum tokio-process 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "afbd6ef1b8cc2bd2c2b580d882774d443ebb1c6ceefe35ba9ea4ab586c89dbe8" "checksum tokio-reactor 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "c56391be9805bc80163151c0b9e5164ee64f4b0200962c346fea12773158f22d" "checksum tokio-rustls 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1df2fa53ac211c136832f530ccb081af9af891af22d685a9493e232c7a359bc2" diff --git a/core/Cargo.toml b/core/Cargo.toml index 72f6a4e6e..e39c5e8a2 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -28,4 +28,5 @@ path = "examples/http_bench.rs" # tokio is only used for deno_core_http_bench [dev_dependencies] -tokio = "0.1.18" +tokio = { version = "0.2", features = ["full"] } +num_cpus = "1.11.1" diff --git a/core/examples/http_bench.rs b/core/examples/http_bench.rs index a7b26f4b1..c08c5b6e1 100644 --- a/core/examples/http_bench.rs +++ b/core/examples/http_bench.rs @@ -5,6 +5,7 @@ extern crate deno; extern crate futures; extern crate libc; +extern crate num_cpus; extern crate tokio; #[macro_use] @@ -13,25 +14,23 @@ extern crate log; extern crate lazy_static; use deno::*; -use futures::compat::AsyncRead01CompatExt; -use futures::compat::AsyncWrite01CompatExt; +use futures::future::Future; use futures::future::FutureExt; -use futures::future::TryFutureExt; -use futures::io::{AsyncRead, AsyncWrite}; -use futures::stream::StreamExt; +use futures::task::{Context, Poll}; use std::env; -use std::future::Future; use std::io::Error; use std::io::ErrorKind; use std::net::SocketAddr; use std::pin::Pin; use std::sync::Mutex; use std::sync::MutexGuard; -use std::task::Poll; -use tokio::net::tcp::Incoming; +use tokio::io::AsyncRead; +use tokio::io::AsyncWrite; static LOGGER: Logger = Logger; + struct Logger; + impl log::Log for Logger { fn enabled(&self, metadata: &log::Metadata) -> bool { metadata.level() <= log::max_level() @@ -118,25 +117,18 @@ fn http_op( let record = Record::from(control); let is_sync = record.promise_id == 0; let op = handler(record.clone(), zero_copy_buf); - let mut record_a = record.clone(); - let mut record_b = record.clone(); - - let fut = Box::new( - op.and_then(move |result| { - record_a.result = result; - futures::future::ok(record_a) - }) - .or_else(|err| { - eprintln!("unexpected err {}", err); - record_b.result = -1; - futures::future::ok(record_b) - }) - .then(|result: Result<Record, ()>| { - let record = result.unwrap(); - futures::future::ok(record.into()) - }), - ); + + let fut = async move { + match op.await { + Ok(result) => record_a.result = result, + Err(err) => { + eprintln!("unexpected err {}", err); + record_a.result = -1; + } + }; + Ok(record_a.into()) + }; if is_sync { Op::Sync(futures::executor::block_on(fut).unwrap()) @@ -172,27 +164,35 @@ fn main() { isolate.register_op("write", http_op(op_write)); isolate.register_op("close", http_op(op_close)); - let main_future = isolate - .then(|r| { - js_check(r); - futures::future::ok(()) - }) - .boxed(); + let multi_thread = args.iter().any(|a| a == "--multi-thread"); - if args.iter().any(|a| a == "--multi-thread") { + println!( + "num cpus; logical: {}; physical: {}", + num_cpus::get(), + num_cpus::get_physical() + ); + let mut builder = tokio::runtime::Builder::new(); + let builder = if multi_thread { println!("multi-thread"); - tokio::run(main_future.compat()); + builder.threaded_scheduler() } else { println!("single-thread"); - tokio::runtime::current_thread::run(main_future.compat()); - } + builder.basic_scheduler() + }; + + let mut runtime = builder + .enable_io() + .build() + .expect("Unable to create tokio runtime"); + let result = runtime.block_on(isolate.boxed()); + js_check(result); } pub fn bad_resource() -> Error { Error::new(ErrorKind::NotFound, "bad resource id") } -struct TcpListener(Incoming); +struct TcpListener(tokio::net::TcpListener); impl Resource for TcpListener {} @@ -209,33 +209,42 @@ fn lock_resource_table<'a>() -> MutexGuard<'a, ResourceTable> { RESOURCE_TABLE.lock().unwrap() } +struct Accept { + rid: ResourceId, +} + +impl Future for Accept { + type Output = Result<(tokio::net::TcpStream, SocketAddr), std::io::Error>; + + fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> { + let inner = self.get_mut(); + + let mut table = lock_resource_table(); + match table.get_mut::<TcpListener>(inner.rid) { + None => Poll::Ready(Err(bad_resource())), + Some(listener) => { + let listener = &mut listener.0; + listener.poll_accept(cx) + } + } + } +} + fn op_accept( record: Record, _zero_copy_buf: Option<PinnedBuf>, ) -> Pin<Box<HttpOp>> { let rid = record.arg as u32; debug!("accept {}", rid); - let fut = futures::future::poll_fn(move |cx| { - let mut table = lock_resource_table(); - let listener = - table.get_mut::<TcpListener>(rid).ok_or_else(bad_resource)?; - let mut listener = futures::compat::Compat01As03::new(&mut listener.0); - match listener.poll_next_unpin(cx) { - Poll::Ready(Some(Err(e))) => Poll::Ready(Err(e)), - Poll::Ready(Some(Ok(stream))) => { - let addr = stream.peer_addr().unwrap(); - Poll::Ready(Ok((stream, addr))) - } - Poll::Pending => Poll::Pending, - _ => unreachable!(), - } - }) - .and_then(move |(stream, addr)| { + + let fut = async move { + let (stream, addr) = Accept { rid }.await?; debug!("accept success {}", addr); let mut table = lock_resource_table(); let rid = table.add("tcpStream", Box::new(TcpStream(stream))); - futures::future::ok(rid as i32) - }); + Ok(rid as i32) + }; + fut.boxed() } @@ -244,12 +253,15 @@ fn op_listen( _zero_copy_buf: Option<PinnedBuf>, ) -> Pin<Box<HttpOp>> { debug!("listen"); - let addr = "127.0.0.1:4544".parse::<SocketAddr>().unwrap(); - let listener = tokio::net::TcpListener::bind(&addr).unwrap(); - let mut table = lock_resource_table(); - let rid = - table.add("tcpListener", Box::new(TcpListener(listener.incoming()))); - futures::future::ok(rid as i32).boxed() + let fut = async { + let addr = "127.0.0.1:4544".parse::<SocketAddr>().unwrap(); + let listener = tokio::net::TcpListener::bind(&addr).await?; + let mut table = lock_resource_table(); + let rid = table.add("tcpListener", Box::new(TcpListener(listener))); + Ok(rid as i32) + }; + + fut.boxed() } fn op_close( @@ -257,36 +269,82 @@ fn op_close( _zero_copy_buf: Option<PinnedBuf>, ) -> Pin<Box<HttpOp>> { debug!("close"); - let rid = record.arg as u32; - let mut table = lock_resource_table(); - let fut = match table.close(rid) { - Some(_) => futures::future::ok(0), - None => futures::future::err(bad_resource()), + let fut = async move { + let rid = record.arg as u32; + let mut table = lock_resource_table(); + match table.close(rid) { + Some(_) => Ok(0), + None => Err(bad_resource()), + } }; fut.boxed() } +struct Read { + rid: ResourceId, + buf: PinnedBuf, +} + +impl Future for Read { + type Output = Result<usize, std::io::Error>; + + fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> { + let inner = self.get_mut(); + let mut table = lock_resource_table(); + + match table.get_mut::<TcpStream>(inner.rid) { + None => Poll::Ready(Err(bad_resource())), + Some(stream) => { + let pinned_stream = Pin::new(&mut stream.0); + pinned_stream.poll_read(cx, &mut inner.buf) + } + } + } +} + fn op_read( record: Record, zero_copy_buf: Option<PinnedBuf>, ) -> Pin<Box<HttpOp>> { let rid = record.arg as u32; debug!("read rid={}", rid); - let mut zero_copy_buf = zero_copy_buf.unwrap(); - let fut = futures::future::poll_fn(move |cx| { - let mut table = lock_resource_table(); - let stream = table.get_mut::<TcpStream>(rid).ok_or_else(bad_resource)?; - let mut f: Box<dyn AsyncRead + Unpin> = - Box::new(AsyncRead01CompatExt::compat(&stream.0)); - AsyncRead::poll_read(Pin::new(&mut f), cx, &mut zero_copy_buf) - }) - .and_then(move |nread| { + let zero_copy_buf = zero_copy_buf.unwrap(); + + let fut = async move { + let nread = Read { + rid, + buf: zero_copy_buf, + } + .await?; debug!("read success {}", nread); - futures::future::ok(nread as i32) - }); + Ok(nread as i32) + }; + fut.boxed() } +struct Write { + rid: ResourceId, + buf: PinnedBuf, +} + +impl Future for Write { + type Output = Result<usize, std::io::Error>; + + fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> { + let inner = self.get_mut(); + let mut table = lock_resource_table(); + + match table.get_mut::<TcpStream>(inner.rid) { + None => Poll::Ready(Err(bad_resource())), + Some(stream) => { + let pinned_stream = Pin::new(&mut stream.0); + pinned_stream.poll_write(cx, &inner.buf) + } + } + } +} + fn op_write( record: Record, zero_copy_buf: Option<PinnedBuf>, @@ -294,17 +352,17 @@ fn op_write( let rid = record.arg as u32; debug!("write rid={}", rid); let zero_copy_buf = zero_copy_buf.unwrap(); - let fut = futures::future::poll_fn(move |cx| { - let mut table = lock_resource_table(); - let stream = table.get_mut::<TcpStream>(rid).ok_or_else(bad_resource)?; - let mut f: Box<dyn AsyncWrite + Unpin> = - Box::new(AsyncWrite01CompatExt::compat(&stream.0)); - AsyncWrite::poll_write(Pin::new(&mut f), cx, &zero_copy_buf) - }) - .and_then(move |nwritten| { + + let fut = async move { + let nwritten = Write { + rid, + buf: zero_copy_buf, + } + .await?; debug!("write success {}", nwritten); - futures::future::ok(nwritten as i32) - }); + Ok(nwritten as i32) + }; + fut.boxed() } |