diff --git a/sessionspaces/Cargo.lock b/sessionspaces/Cargo.lock index 3a267f2c..6d257280 100644 --- a/sessionspaces/Cargo.lock +++ b/sessionspaces/Cargo.lock @@ -252,6 +252,12 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" +[[package]] +name = "bytes" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e4cec68f03f32e44924783795810fa50a7035d8c8ebe78580ad7e6c703fba38" + [[package]] name = "bytes" version = "1.5.0" @@ -673,7 +679,7 @@ dependencies = [ "futures-sink", "futures-task", "memchr", - "pin-project-lite", + "pin-project-lite 0.2.13", "pin-utils", "slab", ] @@ -711,7 +717,7 @@ version = "0.3.26" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "81fe527a889e1532da5c525686d96d4c2e74cdd345badf8dfef9f6b39dd5f5e8" dependencies = [ - "bytes", + "bytes 1.5.0", "fnv", "futures-core", "futures-sink", @@ -719,7 +725,7 @@ dependencies = [ "http", "indexmap", "slab", - "tokio", + "tokio 1.38.0", "tokio-util", "tracing", ] @@ -797,7 +803,7 @@ version = "0.2.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "601cbb57e577e2f5ef5be8e7b83f0f63994f25aa94d673e54a92d5c516d101f1" dependencies = [ - "bytes", + "bytes 1.5.0", "fnv", "itoa", ] @@ -808,9 +814,9 @@ version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7ceab25649e9960c0311ea418d17bee82c0dcec1bd053b5f9a66e265a693bed2" dependencies = [ - "bytes", + "bytes 1.5.0", "http", - "pin-project-lite", + "pin-project-lite 0.2.13", ] [[package]] @@ -843,7 +849,7 @@ version = "0.14.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bf96e135eb83a2a8ddf766e426a841d8ddd7449d5f00d34ea02b41d2f19eef80" dependencies = [ - "bytes", + "bytes 1.5.0", "futures-channel", "futures-core", "futures-util", @@ -853,9 +859,9 @@ dependencies = [ "httparse", "httpdate", "itoa", - "pin-project-lite", + "pin-project-lite 0.2.13", "socket2", - "tokio", + "tokio 1.38.0", "tower-service", "tracing", "want", @@ -873,7 +879,7 @@ dependencies = [ "log", "rustls", "rustls-native-certs", - "tokio", + "tokio 1.38.0", "tokio-rustls", ] @@ -884,8 +890,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bbb958482e8c7be4bc3cf272a766a2b0bf1a6755e7a6ae777f017a31d11b13b1" dependencies = [ "hyper", - "pin-project-lite", - "tokio", + "pin-project-lite 0.2.13", + "tokio 1.38.0", "tokio-io-timeout", ] @@ -1009,7 +1015,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7fe0d65dd6f3adba29cfb84f19dfe55449c7f6c35425f9d8294bec40313e0b64" dependencies = [ "base64", - "bytes", + "bytes 1.5.0", "chrono", "either", "futures", @@ -1023,7 +1029,7 @@ dependencies = [ "k8s-openapi", "kube-core", "pem", - "pin-project", + "pin-project 1.1.4", "rustls", "rustls-pemfile", "secrecy", @@ -1031,7 +1037,7 @@ dependencies = [ "serde_json", "serde_yaml", "thiserror", - "tokio", + "tokio 1.38.0", "tokio-util", "tower", "tower-http", @@ -1069,7 +1075,7 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2df7f9fd9f64cf8f59e1a4a0753fe7d575a5b38d3d7ac5758dcee9357d83ef0a" dependencies = [ - "bytes", + "bytes 1.5.0", "nom", ] @@ -1080,7 +1086,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "166199a8207874a275144c8a94ff6eed5fcbf5c52303e4d9b4d53a0c7ac76554" dependencies = [ "async-trait", - "bytes", + "bytes 1.5.0", "futures", "futures-util", "lazy_static", @@ -1092,7 +1098,7 @@ dependencies = [ "rustls", "rustls-native-certs", "thiserror", - "tokio", + "tokio 1.38.0", "tokio-rustls", "tokio-stream", "tokio-util", @@ -1209,7 +1215,7 @@ dependencies = [ "serde_json", "serde_urlencoded", "similar", - "tokio", + "tokio 1.38.0", ] [[package]] @@ -1439,13 +1445,33 @@ dependencies = [ "sha2", ] +[[package]] +name = "pin-project" +version = "0.4.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ef0f924a5ee7ea9cbcea77529dba45f8a9ba9f622419fe3386ca581a3ae9d5a" +dependencies = [ + "pin-project-internal 0.4.30", +] + [[package]] name = "pin-project" version = "1.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0302c4a0442c456bd56f841aee5c3bfd17967563f6fadc9ceb9f9c23cf3807e0" dependencies = [ - "pin-project-internal", + "pin-project-internal 1.1.4", +] + +[[package]] +name = "pin-project-internal" +version = "0.4.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "851c8d0ce9bebe43790dedfc86614c23494ac9f423dd618d3a61fc693eafe61e" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", ] [[package]] @@ -1459,6 +1485,12 @@ dependencies = [ "syn 2.0.52", ] +[[package]] +name = "pin-project-lite" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "257b64915a082f7811703966789728173279bdebb956b143dbcd23f6f970a777" + [[package]] name = "pin-project-lite" version = "0.2.13" @@ -1895,7 +1927,8 @@ dependencies = [ "sqlx", "strum", "time", - "tokio", + "tokio 1.38.0", + "tower-limit", "tracing", "tracing-subscriber", "url", @@ -2040,7 +2073,7 @@ dependencies = [ "ahash", "atoi", "byteorder", - "bytes", + "bytes 1.5.0", "crc", "crossbeam-queue", "either", @@ -2067,7 +2100,7 @@ dependencies = [ "sqlformat", "thiserror", "time", - "tokio", + "tokio 1.38.0", "tokio-stream", "tracing", "url", @@ -2109,7 +2142,7 @@ dependencies = [ "sqlx-sqlite", "syn 1.0.109", "tempfile", - "tokio", + "tokio 1.38.0", "url", ] @@ -2123,7 +2156,7 @@ dependencies = [ "base64", "bitflags 2.4.2", "byteorder", - "bytes", + "bytes 1.5.0", "crc", "digest", "dotenvy", @@ -2386,6 +2419,17 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" +[[package]] +name = "tokio" +version = "0.2.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6703a273949a90131b290be1fe7b039d0fc884aa1935860dfcbe056f28cd8092" +dependencies = [ + "bytes 0.5.6", + "pin-project-lite 0.1.12", + "slab", +] + [[package]] name = "tokio" version = "1.38.0" @@ -2393,11 +2437,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ba4f4a02a7a80d6f274636f0aa95c7e383b912d41fe721a31f29e29698585a4a" dependencies = [ "backtrace", - "bytes", + "bytes 1.5.0", "libc", "mio", "parking_lot", - "pin-project-lite", + "pin-project-lite 0.2.13", "signal-hook-registry", "socket2", "tokio-macros", @@ -2410,8 +2454,8 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "30b74022ada614a1b4834de765f9bb43877f910cc8ce4be40e89042c9223a8bf" dependencies = [ - "pin-project-lite", - "tokio", + "pin-project-lite 0.2.13", + "tokio 1.38.0", ] [[package]] @@ -2432,7 +2476,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c28327cf380ac148141087fbfb9de9d7bd4e84ab5d2c28fbc911d753de8a7081" dependencies = [ "rustls", - "tokio", + "tokio 1.38.0", ] [[package]] @@ -2442,8 +2486,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "267ac89e0bec6e691e5813911606935d77c476ff49024f98abcea3e7b15e37af" dependencies = [ "futures-core", - "pin-project-lite", - "tokio", + "pin-project-lite 0.2.13", + "tokio 1.38.0", ] [[package]] @@ -2452,11 +2496,11 @@ version = "0.7.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5419f34732d9eb6ee4c3578b7989078579b7f039cbbb9ca2c4da015749371e15" dependencies = [ - "bytes", + "bytes 1.5.0", "futures-core", "futures-sink", - "pin-project-lite", - "tokio", + "pin-project-lite 0.2.13", + "tokio 1.38.0", "tracing", ] @@ -2468,15 +2512,26 @@ checksum = "b8fa9be0de6cf49e536ce1851f987bd21a43b771b09473c3549a6c853db37c1c" dependencies = [ "futures-core", "futures-util", - "pin-project", - "pin-project-lite", - "tokio", + "pin-project 1.1.4", + "pin-project-lite 0.2.13", + "tokio 1.38.0", "tokio-util", "tower-layer", "tower-service", "tracing", ] +[[package]] +name = "tower-discover" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f6b5000c3c54d269cc695dff28136bb33d08cbf1df2c48129e143ab65bf3c2a" +dependencies = [ + "futures-core", + "pin-project 0.4.30", + "tower-service", +] + [[package]] name = "tower-http" version = "0.4.4" @@ -2485,14 +2540,14 @@ checksum = "61c5bb1d698276a2443e5ecfabc1008bf15a36c12e6a7176e7bf089ea9131140" dependencies = [ "base64", "bitflags 2.4.2", - "bytes", + "bytes 1.5.0", "futures-core", "futures-util", "http", "http-body", "http-range-header", "mime", - "pin-project-lite", + "pin-project-lite 0.2.13", "tower-layer", "tower-service", "tracing", @@ -2504,6 +2559,34 @@ version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c20c8dbed6283a09604c3e69b4b7eeb54e298b8a600d4d5ecb5ad39de609f1d0" +[[package]] +name = "tower-limit" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92c3040c5dbed68abffaa0d4517ac1a454cd741044f33ab0eefab6b8d1361404" +dependencies = [ + "futures-core", + "pin-project 0.4.30", + "tokio 0.2.25", + "tower-layer", + "tower-load", + "tower-service", +] + +[[package]] +name = "tower-load" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8cc79fc3afd07492b7966d7efa7c6c50f8ed58d768a6075dd7ae6591c5d2017b" +dependencies = [ + "futures-core", + "log", + "pin-project 0.4.30", + "tokio 0.2.25", + "tower-discover", + "tower-service", +] + [[package]] name = "tower-service" version = "0.3.2" @@ -2517,7 +2600,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" dependencies = [ "log", - "pin-project-lite", + "pin-project-lite 0.2.13", "tracing-attributes", "tracing-core", ] diff --git a/sessionspaces/Cargo.toml b/sessionspaces/Cargo.toml index 445052c4..603e9f1b 100644 --- a/sessionspaces/Cargo.toml +++ b/sessionspaces/Cargo.toml @@ -17,15 +17,16 @@ ldap3 = { version = "0.11.5", default-features = false, features = [ ] } mockito = { version = "1.4.0" } serde_json = { version = "1.0.120" } -strum = { version = "0.26.3", features = ["derive"] } sqlx = { version = "0.7.3", features = [ "runtime-tokio", "tls-rustls", "mysql", "time", ] } +strum = { version = "0.26.3", features = ["derive"] } time = { version = "0.3.36" } tokio = { version = "1.38.0", features = ["macros", "time"] } +tower-limit = { version = "0.3.1" } tracing = { version = "0.1.40" } tracing-subscriber = { version = "0.3.18" } url = { version = "2.5.2" } diff --git a/sessionspaces/src/main.rs b/sessionspaces/src/main.rs index bc2fbca1..5d5e1218 100644 --- a/sessionspaces/src/main.rs +++ b/sessionspaces/src/main.rs @@ -15,8 +15,8 @@ use clap::Parser; use ldap3::LdapConnAsync; use resources::{create_configmap, create_namespace, delete_namespace}; use sqlx::mysql::MySqlPoolOptions; -use std::collections::BTreeSet; -use tokio::time::{sleep_until, Instant}; +use std::{collections::BTreeSet, time::Duration}; +use tokio::time::interval; use tracing::{info, warn}; use url::Url; @@ -32,6 +32,9 @@ struct Cli { /// The period to wait after a succesful bundle server request #[clap(long, env, default_value = "60s")] update_interval: humantime::Duration, + /// The maximum allowable k8s API requests per second + #[clap(long, env, default_value = "10")] + request_rate: u64, /// The [`tracing::Level`] to log at #[arg(long, env="LOG_LEVEL", default_value_t=tracing::Level::INFO)] log_level: tracing::Level, @@ -54,15 +57,20 @@ async fn main() { let (conn, mut ldap_connection) = LdapConnAsync::new(args.ldap_url.as_str()).await.unwrap(); ldap3::drive!(conn); - let k8s_client = kube::Client::try_default().await.unwrap(); + let k8s_client = kube::client::ClientBuilder::try_from(kube::Config::infer().await.unwrap()) + .unwrap() + .with_layer(&tower_limit::RateLimitLayer::new( + args.request_rate, + Duration::from_secs(1), + )) + .build(); let mut current_sessions = Sessions::default(); - let mut request_at = Instant::now(); + let mut update_interval = interval(args.update_interval.into()); loop { - sleep_until(request_at).await; + update_interval.tick().await; match Sessions::fetch(&ispyb_pool, &mut ldap_connection).await { Ok(mut new_sessions) => { update_sessionspaces(&mut current_sessions, &mut new_sessions, &k8s_client).await; - request_at = request_at.checked_add(*args.update_interval).unwrap(); } Err(err) => warn!("Encountered error when fetching sessions: {err}"), }