Last active
May 25, 2021 18:54
-
-
Save valsteen/2e97cd43bb75e9fd867b272e00e156cd to your computer and use it in GitHub Desktop.
Example of concurrent processing with rate limit and cancellation with async-std
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/target |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This file is automatically @generated by Cargo. | |
# It is not intended for manual editing. | |
[[package]] | |
name = "ahash" | |
version = "0.3.8" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "e8fd72866655d1904d6b0997d0b07ba561047d070fbe29de039031c641b61217" | |
[[package]] | |
name = "async-channel" | |
version = "1.6.1" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "2114d64672151c0c5eaa5e131ec84a74f06e1e559830dabba01ca30605d66319" | |
dependencies = [ | |
"concurrent-queue", | |
"event-listener", | |
"futures-core", | |
] | |
[[package]] | |
name = "async-executor" | |
version = "1.4.1" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "871f9bb5e0a22eeb7e8cf16641feb87c9dc67032ccf8ff49e772eb9941d3a965" | |
dependencies = [ | |
"async-task", | |
"concurrent-queue", | |
"fastrand", | |
"futures-lite", | |
"once_cell", | |
"slab", | |
] | |
[[package]] | |
name = "async-global-executor" | |
version = "2.0.2" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "9586ec52317f36de58453159d48351bc244bc24ced3effc1fce22f3d48664af6" | |
dependencies = [ | |
"async-channel", | |
"async-executor", | |
"async-io", | |
"async-mutex", | |
"blocking", | |
"futures-lite", | |
"num_cpus", | |
"once_cell", | |
] | |
[[package]] | |
name = "async-io" | |
version = "1.4.1" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "4bbfd5cf2794b1e908ea8457e6c45f8f8f1f6ec5f74617bf4662623f47503c3b" | |
dependencies = [ | |
"concurrent-queue", | |
"fastrand", | |
"futures-lite", | |
"libc", | |
"log", | |
"once_cell", | |
"parking", | |
"polling", | |
"slab", | |
"socket2", | |
"waker-fn", | |
"winapi", | |
] | |
[[package]] | |
name = "async-lock" | |
version = "2.4.0" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "e6a8ea61bf9947a1007c5cada31e647dbc77b103c679858150003ba697ea798b" | |
dependencies = [ | |
"event-listener", | |
] | |
[[package]] | |
name = "async-mutex" | |
version = "1.4.0" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "479db852db25d9dbf6204e6cb6253698f175c15726470f78af0d918e99d6156e" | |
dependencies = [ | |
"event-listener", | |
] | |
[[package]] | |
name = "async-std" | |
version = "1.9.0" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "d9f06685bad74e0570f5213741bea82158279a4103d988e57bfada11ad230341" | |
dependencies = [ | |
"async-channel", | |
"async-global-executor", | |
"async-io", | |
"async-lock", | |
"crossbeam-utils", | |
"futures-channel", | |
"futures-core", | |
"futures-io", | |
"futures-lite", | |
"gloo-timers", | |
"kv-log-macro", | |
"log", | |
"memchr", | |
"num_cpus", | |
"once_cell", | |
"pin-project-lite", | |
"pin-utils", | |
"slab", | |
"wasm-bindgen-futures", | |
] | |
[[package]] | |
name = "async-task" | |
version = "4.0.3" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "e91831deabf0d6d7ec49552e489aed63b7456a7a3c46cff62adad428110b0af0" | |
[[package]] | |
name = "atomic-waker" | |
version = "1.0.0" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "065374052e7df7ee4047b1160cca5e1467a12351a40b3da123c870ba0b8eda2a" | |
[[package]] | |
name = "autocfg" | |
version = "1.0.1" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" | |
[[package]] | |
name = "bitflags" | |
version = "1.2.1" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" | |
[[package]] | |
name = "blocking" | |
version = "1.0.2" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "c5e170dbede1f740736619b776d7251cb1b9095c435c34d8ca9f57fcd2f335e9" | |
dependencies = [ | |
"async-channel", | |
"async-task", | |
"atomic-waker", | |
"fastrand", | |
"futures-lite", | |
"once_cell", | |
] | |
[[package]] | |
name = "bumpalo" | |
version = "3.6.1" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "63396b8a4b9de3f4fdfb320ab6080762242f66a8ef174c49d8e19b674db4cdbe" | |
[[package]] | |
name = "cache-padded" | |
version = "1.1.1" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "631ae5198c9be5e753e5cc215e1bd73c2b466a3565173db433f52bb9d3e66dba" | |
[[package]] | |
name = "cc" | |
version = "1.0.67" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "e3c69b077ad434294d3ce9f1f6143a2a4b89a8a2d54ef813d85003a4fd1137fd" | |
[[package]] | |
name = "cfg-if" | |
version = "1.0.0" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" | |
[[package]] | |
name = "concurrent-queue" | |
version = "1.2.2" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "30ed07550be01594c6026cff2a1d7fe9c8f683caa798e12b68694ac9e88286a3" | |
dependencies = [ | |
"cache-padded", | |
] | |
[[package]] | |
name = "crossbeam-utils" | |
version = "0.8.4" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "4feb231f0d4d6af81aed15928e58ecf5816aa62a2393e2c82f46973e92a9a278" | |
dependencies = [ | |
"autocfg", | |
"cfg-if", | |
"lazy_static", | |
] | |
[[package]] | |
name = "ctor" | |
version = "0.1.20" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "5e98e2ad1a782e33928b96fc3948e7c355e5af34ba4de7670fe8bac2a3b2006d" | |
dependencies = [ | |
"quote", | |
"syn", | |
] | |
[[package]] | |
name = "dashmap" | |
version = "4.0.2" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "e77a43b28d0668df09411cb0bc9a8c2adc40f9a048afe863e05fd43251e8e39c" | |
dependencies = [ | |
"cfg-if", | |
"num_cpus", | |
] | |
[[package]] | |
name = "event-listener" | |
version = "2.5.1" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "f7531096570974c3a9dcf9e4b8e1cede1ec26cf5046219fb3b9d897503b9be59" | |
[[package]] | |
name = "fastrand" | |
version = "1.4.1" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "77b705829d1e87f762c2df6da140b26af5839e1033aa84aa5f56bb688e4e1bdb" | |
dependencies = [ | |
"instant", | |
] | |
[[package]] | |
name = "futures" | |
version = "0.3.15" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "0e7e43a803dae2fa37c1f6a8fe121e1f7bf9548b4dfc0522a42f34145dadfc27" | |
dependencies = [ | |
"futures-channel", | |
"futures-core", | |
"futures-executor", | |
"futures-io", | |
"futures-sink", | |
"futures-task", | |
"futures-util", | |
] | |
[[package]] | |
name = "futures-channel" | |
version = "0.3.15" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "e682a68b29a882df0545c143dc3646daefe80ba479bcdede94d5a703de2871e2" | |
dependencies = [ | |
"futures-core", | |
"futures-sink", | |
] | |
[[package]] | |
name = "futures-core" | |
version = "0.3.15" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "0402f765d8a89a26043b889b26ce3c4679d268fa6bb22cd7c6aad98340e179d1" | |
[[package]] | |
name = "futures-executor" | |
version = "0.3.15" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "badaa6a909fac9e7236d0620a2f57f7664640c56575b71a7552fbd68deafab79" | |
dependencies = [ | |
"futures-core", | |
"futures-task", | |
"futures-util", | |
] | |
[[package]] | |
name = "futures-io" | |
version = "0.3.15" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "acc499defb3b348f8d8f3f66415835a9131856ff7714bf10dadfc4ec4bdb29a1" | |
[[package]] | |
name = "futures-lite" | |
version = "1.11.3" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "b4481d0cd0de1d204a4fa55e7d45f07b1d958abcb06714b3446438e2eff695fb" | |
dependencies = [ | |
"fastrand", | |
"futures-core", | |
"futures-io", | |
"memchr", | |
"parking", | |
"pin-project-lite", | |
"waker-fn", | |
] | |
[[package]] | |
name = "futures-macro" | |
version = "0.3.15" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "a4c40298486cdf52cc00cd6d6987892ba502c7656a16a4192a9992b1ccedd121" | |
dependencies = [ | |
"autocfg", | |
"proc-macro-hack", | |
"proc-macro2", | |
"quote", | |
"syn", | |
] | |
[[package]] | |
name = "futures-sink" | |
version = "0.3.15" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "a57bead0ceff0d6dde8f465ecd96c9338121bb7717d3e7b108059531870c4282" | |
[[package]] | |
name = "futures-task" | |
version = "0.3.15" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "8a16bef9fc1a4dddb5bee51c989e3fbba26569cbb0e31f5b303c184e3dd33dae" | |
[[package]] | |
name = "futures-timer" | |
version = "3.0.2" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "e64b03909df88034c26dc1547e8970b91f98bdb65165d6a4e9110d94263dbb2c" | |
[[package]] | |
name = "futures-util" | |
version = "0.3.15" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "feb5c238d27e2bf94ffdfd27b2c29e3df4a68c4193bb6427384259e2bf191967" | |
dependencies = [ | |
"autocfg", | |
"futures-channel", | |
"futures-core", | |
"futures-io", | |
"futures-macro", | |
"futures-sink", | |
"futures-task", | |
"memchr", | |
"pin-project-lite", | |
"pin-utils", | |
"proc-macro-hack", | |
"proc-macro-nested", | |
"slab", | |
] | |
[[package]] | |
name = "getrandom" | |
version = "0.2.3" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753" | |
dependencies = [ | |
"cfg-if", | |
"libc", | |
"wasi", | |
] | |
[[package]] | |
name = "gloo-timers" | |
version = "0.2.1" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "47204a46aaff920a1ea58b11d03dec6f704287d27561724a4631e450654a891f" | |
dependencies = [ | |
"futures-channel", | |
"futures-core", | |
"js-sys", | |
"wasm-bindgen", | |
"web-sys", | |
] | |
[[package]] | |
name = "governor" | |
version = "0.3.2" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "06c5d2f987ee8f6dff3fa1a352058dc59b990e447e4c7846aa7d804971314f7b" | |
dependencies = [ | |
"dashmap", | |
"futures", | |
"futures-timer", | |
"no-std-compat", | |
"nonzero_ext", | |
"parking_lot", | |
"quanta", | |
"rand", | |
"smallvec", | |
] | |
[[package]] | |
name = "hashbrown" | |
version = "0.8.2" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "e91b62f79061a0bc2e046024cb7ba44b08419ed238ecbd9adbd787434b9e8c25" | |
dependencies = [ | |
"ahash", | |
"autocfg", | |
] | |
[[package]] | |
name = "hermit-abi" | |
version = "0.1.18" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "322f4de77956e22ed0e5032c359a0f1273f1f7f0d79bfa3b8ffbc730d7fbcc5c" | |
dependencies = [ | |
"libc", | |
] | |
[[package]] | |
name = "instant" | |
version = "0.1.9" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "61124eeebbd69b8190558df225adf7e4caafce0d743919e5d6b19652314ec5ec" | |
dependencies = [ | |
"cfg-if", | |
] | |
[[package]] | |
name = "js-sys" | |
version = "0.3.51" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "83bdfbace3a0e81a4253f73b49e960b053e396a11012cbd49b9b74d6a2b67062" | |
dependencies = [ | |
"wasm-bindgen", | |
] | |
[[package]] | |
name = "kv-log-macro" | |
version = "1.0.7" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "0de8b303297635ad57c9f5059fd9cee7a47f8e8daa09df0fcd07dd39fb22977f" | |
dependencies = [ | |
"log", | |
] | |
[[package]] | |
name = "lazy_static" | |
version = "1.4.0" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" | |
[[package]] | |
name = "libc" | |
version = "0.2.94" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "18794a8ad5b29321f790b55d93dfba91e125cb1a9edbd4f8e3150acc771c1a5e" | |
[[package]] | |
name = "lock_api" | |
version = "0.4.4" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "0382880606dff6d15c9476c416d18690b72742aa7b605bb6dd6ec9030fbf07eb" | |
dependencies = [ | |
"scopeguard", | |
] | |
[[package]] | |
name = "log" | |
version = "0.4.14" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" | |
dependencies = [ | |
"cfg-if", | |
"value-bag", | |
] | |
[[package]] | |
name = "memchr" | |
version = "2.4.0" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "b16bd47d9e329435e309c58469fe0791c2d0d1ba96ec0954152a5ae2b04387dc" | |
[[package]] | |
name = "no-std-compat" | |
version = "0.4.1" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "b93853da6d84c2e3c7d730d6473e8817692dd89be387eb01b94d7f108ecb5b8c" | |
dependencies = [ | |
"hashbrown", | |
] | |
[[package]] | |
name = "nonzero_ext" | |
version = "0.2.0" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "44a1290799eababa63ea60af0cbc3f03363e328e58f32fb0294798ed3e85f444" | |
[[package]] | |
name = "num_cpus" | |
version = "1.13.0" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3" | |
dependencies = [ | |
"hermit-abi", | |
"libc", | |
] | |
[[package]] | |
name = "once_cell" | |
version = "1.7.2" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "af8b08b04175473088b46763e51ee54da5f9a164bc162f615b91bc179dbf15a3" | |
[[package]] | |
name = "parking" | |
version = "2.0.0" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "427c3892f9e783d91cc128285287e70a59e206ca452770ece88a76f7a3eddd72" | |
[[package]] | |
name = "parking_lot" | |
version = "0.11.1" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "6d7744ac029df22dca6284efe4e898991d28e3085c706c972bcd7da4a27a15eb" | |
dependencies = [ | |
"instant", | |
"lock_api", | |
"parking_lot_core", | |
] | |
[[package]] | |
name = "parking_lot_core" | |
version = "0.8.3" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "fa7a782938e745763fe6907fc6ba86946d72f49fe7e21de074e08128a99fb018" | |
dependencies = [ | |
"cfg-if", | |
"instant", | |
"libc", | |
"redox_syscall", | |
"smallvec", | |
"winapi", | |
] | |
[[package]] | |
name = "pin-project-lite" | |
version = "0.2.6" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "dc0e1f259c92177c30a4c9d177246edd0a3568b25756a977d0632cf8fa37e905" | |
[[package]] | |
name = "pin-utils" | |
version = "0.1.0" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" | |
[[package]] | |
name = "polling" | |
version = "2.0.3" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "4fc12d774e799ee9ebae13f4076ca003b40d18a11ac0f3641e6f899618580b7b" | |
dependencies = [ | |
"cfg-if", | |
"libc", | |
"log", | |
"wepoll-sys", | |
"winapi", | |
] | |
[[package]] | |
name = "ppv-lite86" | |
version = "0.2.10" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" | |
[[package]] | |
name = "proc-macro-hack" | |
version = "0.5.19" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5" | |
[[package]] | |
name = "proc-macro-nested" | |
version = "0.1.7" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "bc881b2c22681370c6a780e47af9840ef841837bc98118431d4e1868bd0c1086" | |
[[package]] | |
name = "proc-macro2" | |
version = "1.0.27" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "f0d8caf72986c1a598726adc988bb5984792ef84f5ee5aa50209145ee8077038" | |
dependencies = [ | |
"unicode-xid", | |
] | |
[[package]] | |
name = "quanta" | |
version = "0.4.1" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "d98dc777a7a39b76b1a26ae9d3f691f4c1bc0455090aa0b64dfa8cb7fc34c135" | |
dependencies = [ | |
"libc", | |
"winapi", | |
] | |
[[package]] | |
name = "quote" | |
version = "1.0.9" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" | |
dependencies = [ | |
"proc-macro2", | |
] | |
[[package]] | |
name = "rand" | |
version = "0.8.3" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" | |
dependencies = [ | |
"libc", | |
"rand_chacha", | |
"rand_core", | |
"rand_hc", | |
] | |
[[package]] | |
name = "rand_chacha" | |
version = "0.3.0" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" | |
dependencies = [ | |
"ppv-lite86", | |
"rand_core", | |
] | |
[[package]] | |
name = "rand_core" | |
version = "0.6.2" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7" | |
dependencies = [ | |
"getrandom", | |
] | |
[[package]] | |
name = "rand_hc" | |
version = "0.3.0" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" | |
dependencies = [ | |
"rand_core", | |
] | |
[[package]] | |
name = "rate_limit_example" | |
version = "0.1.0" | |
dependencies = [ | |
"async-std", | |
"futures", | |
"futures-util", | |
"governor", | |
"nonzero_ext", | |
"rand", | |
] | |
[[package]] | |
name = "redox_syscall" | |
version = "0.2.8" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "742739e41cd49414de871ea5e549afb7e2a3ac77b589bcbebe8c82fab37147fc" | |
dependencies = [ | |
"bitflags", | |
] | |
[[package]] | |
name = "scopeguard" | |
version = "1.1.0" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" | |
[[package]] | |
name = "slab" | |
version = "0.4.3" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "f173ac3d1a7e3b28003f40de0b5ce7fe2710f9b9dc3fc38664cebee46b3b6527" | |
[[package]] | |
name = "smallvec" | |
version = "1.6.1" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e" | |
[[package]] | |
name = "socket2" | |
version = "0.4.0" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "9e3dfc207c526015c632472a77be09cf1b6e46866581aecae5cc38fb4235dea2" | |
dependencies = [ | |
"libc", | |
"winapi", | |
] | |
[[package]] | |
name = "syn" | |
version = "1.0.72" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "a1e8cdbefb79a9a5a65e0db8b47b723ee907b7c7f8496c76a1770b5c310bab82" | |
dependencies = [ | |
"proc-macro2", | |
"quote", | |
"unicode-xid", | |
] | |
[[package]] | |
name = "unicode-xid" | |
version = "0.2.2" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" | |
[[package]] | |
name = "value-bag" | |
version = "1.0.0-alpha.7" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "dd320e1520f94261153e96f7534476ad869c14022aee1e59af7c778075d840ae" | |
dependencies = [ | |
"ctor", | |
"version_check", | |
] | |
[[package]] | |
name = "version_check" | |
version = "0.9.3" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe" | |
[[package]] | |
name = "waker-fn" | |
version = "1.1.0" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "9d5b2c62b4012a3e1eca5a7e077d13b3bf498c4073e33ccd58626607748ceeca" | |
[[package]] | |
name = "wasi" | |
version = "0.10.2+wasi-snapshot-preview1" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" | |
[[package]] | |
name = "wasm-bindgen" | |
version = "0.2.74" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "d54ee1d4ed486f78874278e63e4069fc1ab9f6a18ca492076ffb90c5eb2997fd" | |
dependencies = [ | |
"cfg-if", | |
"wasm-bindgen-macro", | |
] | |
[[package]] | |
name = "wasm-bindgen-backend" | |
version = "0.2.74" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "3b33f6a0694ccfea53d94db8b2ed1c3a8a4c86dd936b13b9f0a15ec4a451b900" | |
dependencies = [ | |
"bumpalo", | |
"lazy_static", | |
"log", | |
"proc-macro2", | |
"quote", | |
"syn", | |
"wasm-bindgen-shared", | |
] | |
[[package]] | |
name = "wasm-bindgen-futures" | |
version = "0.4.24" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "5fba7978c679d53ce2d0ac80c8c175840feb849a161664365d1287b41f2e67f1" | |
dependencies = [ | |
"cfg-if", | |
"js-sys", | |
"wasm-bindgen", | |
"web-sys", | |
] | |
[[package]] | |
name = "wasm-bindgen-macro" | |
version = "0.2.74" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "088169ca61430fe1e58b8096c24975251700e7b1f6fd91cc9d59b04fb9b18bd4" | |
dependencies = [ | |
"quote", | |
"wasm-bindgen-macro-support", | |
] | |
[[package]] | |
name = "wasm-bindgen-macro-support" | |
version = "0.2.74" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "be2241542ff3d9f241f5e2cb6dd09b37efe786df8851c54957683a49f0987a97" | |
dependencies = [ | |
"proc-macro2", | |
"quote", | |
"syn", | |
"wasm-bindgen-backend", | |
"wasm-bindgen-shared", | |
] | |
[[package]] | |
name = "wasm-bindgen-shared" | |
version = "0.2.74" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "d7cff876b8f18eed75a66cf49b65e7f967cb354a7aa16003fb55dbfd25b44b4f" | |
[[package]] | |
name = "web-sys" | |
version = "0.3.51" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "e828417b379f3df7111d3a2a9e5753706cae29c41f7c4029ee9fd77f3e09e582" | |
dependencies = [ | |
"js-sys", | |
"wasm-bindgen", | |
] | |
[[package]] | |
name = "wepoll-sys" | |
version = "3.0.1" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "0fcb14dea929042224824779fbc82d9fab8d2e6d3cbc0ac404de8edf489e77ff" | |
dependencies = [ | |
"cc", | |
] | |
[[package]] | |
name = "winapi" | |
version = "0.3.9" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" | |
dependencies = [ | |
"winapi-i686-pc-windows-gnu", | |
"winapi-x86_64-pc-windows-gnu", | |
] | |
[[package]] | |
name = "winapi-i686-pc-windows-gnu" | |
version = "0.4.0" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" | |
[[package]] | |
name = "winapi-x86_64-pc-windows-gnu" | |
version = "0.4.0" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "rate_limit_example" | |
version = "0.1.0" | |
authors = [] | |
edition = "2018" | |
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | |
[dependencies] | |
async-std = "1.9.0" | |
governor = "0.3.2" | |
futures = "0.3.15" | |
rand = "0.8.3" | |
nonzero_ext = "0.2.0" | |
futures-util = "0.3.15" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use async_std::stream; | |
use async_std::task; | |
use futures::StreamExt; | |
use governor::state::direct::StreamRateLimitExt; | |
use governor::{Jitter, Quota, RateLimiter}; | |
use nonzero_ext::*; | |
use rand::random; | |
use std::time::Duration; | |
use async_std::channel::bounded; | |
enum Cancellable<T> { | |
Cancelled(T), | |
Processed(T), | |
} | |
async fn main_async() { | |
// Produce 100 ints | |
let mut input = stream::from_iter(0..100); | |
// a channel of 1 item is used to implement cancellation with fallback for ignored items | |
let (process_sender, process_stream) = bounded(1); | |
let (result_sender, mut result_stream) = bounded(1); | |
let result_task_handle = task::spawn(async move { | |
while let Some(item) = result_stream.next().await { | |
match item { | |
Cancellable::Cancelled(i) => { | |
println!("cancelled item: {}", i) | |
} | |
Cancellable::Processed(i) => { | |
println!("processed item: {}", i) | |
} | |
} | |
} | |
}); | |
{ | |
let process_receiver = process_stream.clone(); | |
task::spawn(async move { | |
task::sleep(Duration::from_secs(10)).await; | |
println!("boom"); | |
// closing the channel effectively cancels the stream | |
process_receiver.close(); | |
}); | |
} | |
let process_until_cancellation = { | |
let result_sender = result_sender.clone(); | |
task::spawn(async move { | |
while let Some(item) = input.next().await { | |
// will wait until the next item is pulled, which acts as backpressure | |
if process_sender.send(item).await.is_err() { | |
result_sender.send(Cancellable::Cancelled(item)).await.expect("error while sending the cancelled item"); | |
break; // short circuit from there to not test on the error again | |
} | |
} | |
while let Some(item) = input.next().await { | |
result_sender.send(Cancellable::Cancelled(item)).await.expect("error while sending the cancelled item"); | |
} | |
}) | |
}; | |
// rate limit | |
let limiter = | |
RateLimiter::direct(Quota::per_minute(nonzero!(150u32)).allow_burst(nonzero!(1u32))); | |
let process_stream = process_stream.ratelimit_stream_with_jitter(&limiter, Jitter::up_to(Duration::from_secs(1))); | |
// do the work. the rate at which requests is limited, but we could even limit the amount of | |
// parallel requests | |
process_stream | |
.for_each_concurrent(None, |item| { | |
let result_sender = result_sender.clone(); | |
async move { | |
println!("doing stuff with {}", item); | |
task::sleep(Duration::from_millis((random::<f64>() * 500.0) as u64)).await; | |
println!("done stuff with {}", item); | |
result_sender.send(Cancellable::Processed(item)).await.expect("error while sending the processed item"); | |
} | |
}) | |
.await; // for_each_concurrent returns a future that resolves when all futures are resolved | |
// channel closes as soon as the last clone is dropped ; they will be once | |
// process_until_cancellation is done. at that point result_task loop ends after the last item | |
// is consumed. | |
drop(result_sender); | |
process_until_cancellation.await; | |
result_task_handle.await; | |
} | |
fn main() { | |
task::block_on(main_async()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment