diff --git a/.github/workflows/websocket-proxy-ci.yaml b/.github/workflows/websocket-proxy-ci.yaml new file mode 100644 index 0000000..91c1f7d --- /dev/null +++ b/.github/workflows/websocket-proxy-ci.yaml @@ -0,0 +1,67 @@ +name: Websocket Proxy CI + +on: + push: + branches: [main] + pull_request: + +env: + CARGO_TERM_COLOR: always + +jobs: + check: + name: Check + runs-on: ubuntu-latest + defaults: + run: + working-directory: "./websocket-proxy" + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Install Redis for tests + run: | + sudo apt-get update + sudo apt-get install -y redis + + - name: Set up Rust + uses: actions-rs/toolchain@v1 + with: + toolchain: stable + override: true + components: rustfmt + + - name: Cache dependencies + uses: Swatinem/rust-cache@v2 + + - name: Run build + run: cargo build + + - name: Check formatting + run: cargo fmt --all -- --check + + - name: Run clippy + run: cargo clippy -- -D warnings + + - name: Run tests + run: cargo test --all-features + + - name: Check for common mistakes + run: cargo check + + docker: + name: Docker Build + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Build Docker image + uses: docker/build-push-action@v5 + with: + context: . + push: false + cache-from: type=gha + cache-to: type=gha,mode=max diff --git a/.gitignore b/.gitignore index ac4798a..861c887 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,8 @@ /target +/.idea/ +/.vscode/ .env +websocket-proxy/.env +websocket-proxy/target integration_logs -.vscode \ No newline at end of file +.vscode diff --git a/websocket-proxy/.dockerignore b/websocket-proxy/.dockerignore new file mode 100644 index 0000000..7bf30c2 --- /dev/null +++ b/websocket-proxy/.dockerignore @@ -0,0 +1,5 @@ +target/ +.git/ +.github/ +.gitignore +README.md \ No newline at end of file diff --git a/websocket-proxy/.env.example b/websocket-proxy/.env.example new file mode 100644 index 0000000..b6f660e --- /dev/null +++ b/websocket-proxy/.env.example @@ -0,0 +1,3 @@ +UPSTREAM_WS=ws://upstreamurl.com/rs +MAXIMUM_CONCURRENT_CONNECTIONS=2 +LOG_LEVEL=debug diff --git a/websocket-proxy/Cargo.lock b/websocket-proxy/Cargo.lock new file mode 100644 index 0000000..78e1ba8 --- /dev/null +++ b/websocket-proxy/Cargo.lock @@ -0,0 +1,2829 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "addr2line" +version = "0.24.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfbe277e56a376000877090da837660b4427aad530e3028d44e0bffe4f89a1c1" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler2" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" + +[[package]] +name = "ahash" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011" +dependencies = [ + "cfg-if", + "once_cell", + "version_check", + "zerocopy 0.7.35", +] + +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + +[[package]] +name = "anstream" +version = "0.6.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8acc5369981196006228e28809f761875c0327210a891e941f4c683b3a99529b" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9" + +[[package]] +name = "anstyle-parse" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b2d16507662817a6a20a9ea92df6652ee4f94f914589377d69f3b21bc5798a9" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c" +dependencies = [ + "windows-sys 0.59.0", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3534e77181a9cc07539ad51f2141fe32f6c3ffd4df76db8ad92346b003ae4e" +dependencies = [ + "anstyle", + "once_cell", + "windows-sys 0.59.0", +] + +[[package]] +name = "arc-swap" +version = "1.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69f7f8c3906b62b754cd5326047894316021dcfe5a194c8ea52bdd94934a3457" + +[[package]] +name = "atomic-waker" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" + +[[package]] +name = "autocfg" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" + +[[package]] +name = "aws-lc-rs" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19b756939cb2f8dc900aa6dcd505e6e2428e9cae7ff7b028c49e3946efa70878" +dependencies = [ + "aws-lc-sys", + "zeroize", +] + +[[package]] +name = "aws-lc-sys" +version = "0.28.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa9b6986f250236c27e5a204062434a773a13243d2ffc2955f37bdba4c5c6a1" +dependencies = [ + "bindgen", + "cc", + "cmake", + "dunce", + "fs_extra", +] + +[[package]] +name = "axum" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de45108900e1f9b9242f7f2e254aa3e2c029c921c258fe9e6b4217eeebd54288" +dependencies = [ + "axum-core", + "base64", + "bytes", + "form_urlencoded", + "futures-util", + "http", + "http-body", + "http-body-util", + "hyper", + "hyper-util", + "itoa", + "matchit", + "memchr", + "mime", + "percent-encoding", + "pin-project-lite", + "rustversion", + "serde", + "serde_json", + "serde_path_to_error", + "serde_urlencoded", + "sha1", + "sync_wrapper", + "tokio", + "tokio-tungstenite", + "tower", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "axum-core" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68464cd0412f486726fb3373129ef5d2993f90c34bc2bc1c1e9943b2f4fc7ca6" +dependencies = [ + "bytes", + "futures-core", + "http", + "http-body", + "http-body-util", + "mime", + "pin-project-lite", + "rustversion", + "sync_wrapper", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "backoff" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b62ddb9cb1ec0a098ad4bbf9344d0713fa193ae1a80af55febcff2627b6a00c1" +dependencies = [ + "getrandom 0.2.16", + "instant", + "rand 0.8.5", +] + +[[package]] +name = "backtrace" +version = "0.3.74" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d82cb332cdfaed17ae235a638438ac4d4839913cc2af585c3c6746e8f8bee1a" +dependencies = [ + "addr2line", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", + "windows-targets 0.52.6", +] + +[[package]] +name = "base64" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" + +[[package]] +name = "bindgen" +version = "0.69.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "271383c67ccabffb7381723dea0672a673f292304fcb45c01cc648c7a8d58088" +dependencies = [ + "bitflags", + "cexpr", + "clang-sys", + "itertools", + "lazy_static", + "lazycell", + "log", + "prettyplease", + "proc-macro2", + "quote", + "regex", + "rustc-hash", + "shlex", + "syn", + "which", +] + +[[package]] +name = "bitflags" +version = "2.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd" + +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + +[[package]] +name = "bumpalo" +version = "3.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1628fb46dfa0b37568d12e5edd512553eccf6a22a78e8bde00bb4aed84d5bdbf" + +[[package]] +name = "bytes" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d71b6127be86fdcfddb610f7182ac57211d4b18a3e9c82eb2d17662f2227ad6a" + +[[package]] +name = "cc" +version = "1.2.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04da6a0d40b948dfc4fa8f5bbf402b0fc1a64a28dbf7d12ffd683550f2c1b63a" +dependencies = [ + "jobserver", + "libc", + "shlex", +] + +[[package]] +name = "cexpr" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766" +dependencies = [ + "nom", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "clang-sys" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b023947811758c97c59bf9d1c188fd619ad4718dcaa767947df1cadb14f39f4" +dependencies = [ + "glob", + "libc", + "libloading", +] + +[[package]] +name = "clap" +version = "4.5.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eccb054f56cbd38340b380d4a8e69ef1f02f1af43db2f0cc817a4774d80ae071" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "efd9466fac8543255d3b1fcad4762c5e116ffe808c8a3043d4263cd4fd4862a2" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09176aae279615badda0765c0c0b3f6ed53f4709118af73cf4655d85d1530cd7" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6" + +[[package]] +name = "cmake" +version = "0.1.54" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7caa3f9de89ddbe2c607f4101924c5abec803763ae9534e4f4d7d8f84aa81f0" +dependencies = [ + "cc", +] + +[[package]] +name = "colorchoice" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" + +[[package]] +name = "combine" +version = "4.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba5a308b75df32fe02788e748662718f03fde005016435c444eea572398219fd" +dependencies = [ + "bytes", + "memchr", +] + +[[package]] +name = "core-foundation" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91e195e091a93c46f7102ec7818a2aa394e1e1771c3ab4825963fa03e45afb8f" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "core-foundation" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b55271e5c8c478ad3f38ad24ef34923091e0548492a266d19b3c0b4d82574c63" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + +[[package]] +name = "cpufeatures" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280" +dependencies = [ + "libc", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" + +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "data-encoding" +version = "2.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a2330da5de22e8a3cb63252ce2abb30116bf5265e89c0e01bc17015ce30a476" + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", +] + +[[package]] +name = "displaydoc" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "dotenvy" +version = "0.15.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b" + +[[package]] +name = "dunce" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92773504d58c093f6de2459af4af33faa518c13451eb8f2b5698ed3d36e7c813" + +[[package]] +name = "either" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" + +[[package]] +name = "equivalent" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" + +[[package]] +name = "errno" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "976dd42dc7e85965fe702eb8164f21f450704bdde31faefd6471dba214cb594e" +dependencies = [ + "libc", + "windows-sys 0.59.0", +] + +[[package]] +name = "fastrand" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" + +[[package]] +name = "flashblocks-websocket-proxy" +version = "0.1.0" +dependencies = [ + "axum", + "backoff", + "clap", + "dotenvy", + "futures", + "hostname", + "http", + "metrics", + "metrics-derive", + "metrics-exporter-prometheus", + "redis", + "redis-test", + "reqwest", + "ring", + "serde_json", + "thiserror", + "tokio", + "tokio-tungstenite", + "tokio-util", + "tracing", + "tracing-subscriber", + "uuid", +] + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "foldhash" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" + +[[package]] +name = "foreign-types" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" +dependencies = [ + "foreign-types-shared", +] + +[[package]] +name = "foreign-types-shared" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" + +[[package]] +name = "form_urlencoded" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13624c2627564efccf4934284bdd98cbaa14e79b0b5a141218e507b3a823456" +dependencies = [ + "percent-encoding", +] + +[[package]] +name = "fs_extra" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42703706b716c37f96a77aea830392ad231f44c9e9a67872fa5548707e11b11c" + +[[package]] +name = "futures" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65bc07b1a8bc7c85c5f2e110c476c7389b4554ba72af57d8445ea63a576b0876" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e" + +[[package]] +name = "futures-executor" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e28d1d997f585e54aebc3f97d39e72338912123a67330d723fdbb564d646c9f" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6" + +[[package]] +name = "futures-macro" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "futures-sink" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e575fab7d1e0dcb8d0c7bcf9a63ee213816ab51902e6d244a95819acacf1d4f7" + +[[package]] +name = "futures-task" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f90f7dce0722e95104fcb095585910c0977252f286e354b5e3bd38902cd99988" + +[[package]] +name = "futures-util" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-macro", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "pin-utils", + "slab", +] + +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "getrandom" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "335ff9f135e4384c8150d6f27c6daed433577f86b4750418338c01a1a2528592" +dependencies = [ + "cfg-if", + "libc", + "wasi 0.11.0+wasi-snapshot-preview1", +] + +[[package]] +name = "getrandom" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73fea8450eea4bac3940448fb7ae50d91f034f941199fcd9d909a5a07aa455f0" +dependencies = [ + "cfg-if", + "libc", + "r-efi", + "wasi 0.14.2+wasi-0.2.4", +] + +[[package]] +name = "gimli" +version = "0.31.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" + +[[package]] +name = "glob" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8d1add55171497b4705a648c6b583acafb01d58050a51727785f0b2c8e0a2b2" + +[[package]] +name = "h2" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75249d144030531f8dee69fe9cea04d3edf809a017ae445e2abdff6629e86633" +dependencies = [ + "atomic-waker", + "bytes", + "fnv", + "futures-core", + "futures-sink", + "http", + "indexmap", + "slab", + "tokio", + "tokio-util", + "tracing", +] + +[[package]] +name = "hashbrown" +version = "0.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" +dependencies = [ + "foldhash", +] + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "home" +version = "0.5.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589533453244b0995c858700322199b2becb13b627df2851f64a2775d024abcf" +dependencies = [ + "windows-sys 0.59.0", +] + +[[package]] +name = "hostname" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a56f203cd1c76362b69e3863fd987520ac36cf70a8c92627449b2f64a8cf7d65" +dependencies = [ + "cfg-if", + "libc", + "windows-link", +] + +[[package]] +name = "http" +version = "1.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4a85d31aea989eead29a3aaf9e1115a180df8282431156e533de47660892565" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + +[[package]] +name = "http-body" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1efedce1fb8e6913f23e0c92de8e62cd5b772a67e7b3946df930a62566c93184" +dependencies = [ + "bytes", + "http", +] + +[[package]] +name = "http-body-util" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b021d93e26becf5dc7e1b75b1bed1fd93124b374ceb73f43d4d4eafec896a64a" +dependencies = [ + "bytes", + "futures-core", + "http", + "http-body", + "pin-project-lite", +] + +[[package]] +name = "httparse" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87" + +[[package]] +name = "httpdate" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" + +[[package]] +name = "hyper" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc2b571658e38e0c01b1fdca3bbbe93c00d3d71693ff2770043f8c29bc7d6f80" +dependencies = [ + "bytes", + "futures-channel", + "futures-util", + "h2", + "http", + "http-body", + "httparse", + "httpdate", + "itoa", + "pin-project-lite", + "smallvec", + "tokio", + "want", +] + +[[package]] +name = "hyper-rustls" +version = "0.27.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d191583f3da1305256f22463b9bb0471acad48a4e534a5218b9963e9c1f59b2" +dependencies = [ + "futures-util", + "http", + "hyper", + "hyper-util", + "rustls", + "rustls-native-certs", + "rustls-pki-types", + "tokio", + "tokio-rustls", + "tower-service", +] + +[[package]] +name = "hyper-tls" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70206fc6890eaca9fde8a0bf71caa2ddfc9fe045ac9e5c70df101a7dbde866e0" +dependencies = [ + "bytes", + "http-body-util", + "hyper", + "hyper-util", + "native-tls", + "tokio", + "tokio-native-tls", + "tower-service", +] + +[[package]] +name = "hyper-util" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "497bbc33a26fdd4af9ed9c70d63f61cf56a938375fbb32df34db9b1cd6d643f2" +dependencies = [ + "bytes", + "futures-channel", + "futures-util", + "http", + "http-body", + "hyper", + "libc", + "pin-project-lite", + "socket2", + "tokio", + "tower-service", + "tracing", +] + +[[package]] +name = "icu_collections" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db2fa452206ebee18c4b5c2274dbf1de17008e874b4dc4f0aea9d01ca79e4526" +dependencies = [ + "displaydoc", + "yoke", + "zerofrom", + "zerovec", +] + +[[package]] +name = "icu_locid" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13acbb8371917fc971be86fc8057c41a64b521c184808a698c02acc242dbf637" +dependencies = [ + "displaydoc", + "litemap", + "tinystr", + "writeable", + "zerovec", +] + +[[package]] +name = "icu_locid_transform" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01d11ac35de8e40fdeda00d9e1e9d92525f3f9d887cdd7aa81d727596788b54e" +dependencies = [ + "displaydoc", + "icu_locid", + "icu_locid_transform_data", + "icu_provider", + "tinystr", + "zerovec", +] + +[[package]] +name = "icu_locid_transform_data" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7515e6d781098bf9f7205ab3fc7e9709d34554ae0b21ddbcb5febfa4bc7df11d" + +[[package]] +name = "icu_normalizer" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19ce3e0da2ec68599d193c93d088142efd7f9c5d6fc9b803774855747dc6a84f" +dependencies = [ + "displaydoc", + "icu_collections", + "icu_normalizer_data", + "icu_properties", + "icu_provider", + "smallvec", + "utf16_iter", + "utf8_iter", + "write16", + "zerovec", +] + +[[package]] +name = "icu_normalizer_data" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c5e8338228bdc8ab83303f16b797e177953730f601a96c25d10cb3ab0daa0cb7" + +[[package]] +name = "icu_properties" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93d6020766cfc6302c15dbbc9c8778c37e62c14427cb7f6e601d849e092aeef5" +dependencies = [ + "displaydoc", + "icu_collections", + "icu_locid_transform", + "icu_properties_data", + "icu_provider", + "tinystr", + "zerovec", +] + +[[package]] +name = "icu_properties_data" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85fb8799753b75aee8d2a21d7c14d9f38921b54b3dbda10f5a3c7a7b82dba5e2" + +[[package]] +name = "icu_provider" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ed421c8a8ef78d3e2dbc98a973be2f3770cb42b606e3ab18d6237c4dfde68d9" +dependencies = [ + "displaydoc", + "icu_locid", + "icu_provider_macros", + "stable_deref_trait", + "tinystr", + "writeable", + "yoke", + "zerofrom", + "zerovec", +] + +[[package]] +name = "icu_provider_macros" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ec89e9337638ecdc08744df490b221a7399bf8d164eb52a665454e60e075ad6" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "idna" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "686f825264d630750a544639377bae737628043f20d38bbc029e8f29ea968a7e" +dependencies = [ + "idna_adapter", + "smallvec", + "utf8_iter", +] + +[[package]] +name = "idna_adapter" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "daca1df1c957320b2cf139ac61e7bd64fed304c5040df000a745aa1de3b4ef71" +dependencies = [ + "icu_normalizer", + "icu_properties", +] + +[[package]] +name = "indexmap" +version = "2.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cea70ddb795996207ad57735b50c5982d8844f38ba9ee5f1aedcfb708a2aa11e" +dependencies = [ + "equivalent", + "hashbrown", +] + +[[package]] +name = "instant" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0242819d153cba4b4b05a5a8f2a7e9bbf97b6055b2a002b395c96b5ff3c0222" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "ipnet" +version = "2.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "469fb0b9cefa57e3ef31275ee7cacb78f2fdca44e4765491884a2b119d4eb130" + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + +[[package]] +name = "itertools" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba291022dbbd398a455acf126c1e341954079855bc60dfdda641363bd6922569" +dependencies = [ + "either", +] + +[[package]] +name = "itoa" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" + +[[package]] +name = "jobserver" +version = "0.1.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38f262f097c174adebe41eb73d66ae9c06b2844fb0da69969647bbddd9b0538a" +dependencies = [ + "getrandom 0.3.2", + "libc", +] + +[[package]] +name = "js-sys" +version = "0.3.77" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cfaf33c695fc6e08064efbc1f72ec937429614f25eef83af942d0e227c3a28f" +dependencies = [ + "once_cell", + "wasm-bindgen", +] + +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + +[[package]] +name = "lazycell" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" + +[[package]] +name = "libc" +version = "0.2.172" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d750af042f7ef4f724306de029d18836c26c1765a54a6a3f094cbd23a7267ffa" + +[[package]] +name = "libloading" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc2f4eb4bc735547cfed7c0a4922cbd04a4655978c09b54f1f7b228750664c34" +dependencies = [ + "cfg-if", + "windows-targets 0.52.6", +] + +[[package]] +name = "linux-raw-sys" +version = "0.4.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d26c52dbd32dccf2d10cac7725f8eae5296885fb5703b261f7d0a0739ec807ab" + +[[package]] +name = "linux-raw-sys" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd945864f07fe9f5371a27ad7b52a172b4b499999f1d97574c9fa68373937e12" + +[[package]] +name = "litemap" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23fb14cb19457329c82206317a5663005a4d404783dc74f4252769b0d5f42856" + +[[package]] +name = "lock_api" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17" +dependencies = [ + "autocfg", + "scopeguard", +] + +[[package]] +name = "log" +version = "0.4.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94" + +[[package]] +name = "matchers" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558" +dependencies = [ + "regex-automata 0.1.10", +] + +[[package]] +name = "matchit" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47e1ffaa40ddd1f3ed91f717a33c8c0ee23fff369e3aa8772b9605cc1d22f4c3" + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "metrics" +version = "0.24.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25dea7ac8057892855ec285c440160265225438c3c45072613c25a4b26e98ef5" +dependencies = [ + "ahash", + "portable-atomic", +] + +[[package]] +name = "metrics-derive" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3dbdd96ed57d565ec744cba02862d707acf373c5772d152abae6ec5c4e24f6c" +dependencies = [ + "proc-macro2", + "quote", + "regex", + "syn", +] + +[[package]] +name = "metrics-exporter-prometheus" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df88858cd28baaaf2cfc894e37789ed4184be0e1351157aec7bf3c2266c793fd" +dependencies = [ + "base64", + "http-body-util", + "hyper", + "hyper-rustls", + "hyper-util", + "indexmap", + "ipnet", + "metrics", + "metrics-util", + "quanta", + "thiserror", + "tokio", + "tracing", +] + +[[package]] +name = "metrics-util" +version = "0.19.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8496cc523d1f94c1385dd8f0f0c2c480b2b8aeccb5b7e4485ad6365523ae376" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", + "hashbrown", + "metrics", + "quanta", + "rand 0.9.1", + "rand_xoshiro", + "sketches-ddsketch", +] + +[[package]] +name = "mime" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" + +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + +[[package]] +name = "miniz_oxide" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3be647b768db090acb35d5ec5db2b0e1f1de11133ca123b9eacf5137868f892a" +dependencies = [ + "adler2", +] + +[[package]] +name = "mio" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2886843bf800fba2e3377cff24abf6379b4c4d5c6681eaf9ea5b0d15090450bd" +dependencies = [ + "libc", + "wasi 0.11.0+wasi-snapshot-preview1", + "windows-sys 0.52.0", +] + +[[package]] +name = "native-tls" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87de3442987e9dbec73158d5c715e7ad9072fda936bb03d19d7fa10e00520f0e" +dependencies = [ + "libc", + "log", + "openssl", + "openssl-probe", + "openssl-sys", + "schannel", + "security-framework 2.11.1", + "security-framework-sys", + "tempfile", +] + +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + +[[package]] +name = "nu-ansi-term" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84" +dependencies = [ + "overload", + "winapi", +] + +[[package]] +name = "num-bigint" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9" +dependencies = [ + "num-integer", + "num-traits", +] + +[[package]] +name = "num-integer" +version = "0.1.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "object" +version = "0.36.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62948e14d923ea95ea2c7c86c71013138b66525b86bdc08d2dcc262bdb497b87" +dependencies = [ + "memchr", +] + +[[package]] +name = "once_cell" +version = "1.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" + +[[package]] +name = "openssl" +version = "0.10.72" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fedfea7d58a1f73118430a55da6a286e7b044961736ce96a16a17068ea25e5da" +dependencies = [ + "bitflags", + "cfg-if", + "foreign-types", + "libc", + "once_cell", + "openssl-macros", + "openssl-sys", +] + +[[package]] +name = "openssl-macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "openssl-probe" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d05e27ee213611ffe7d6348b942e8f942b37114c00cc03cec254295a4a17852e" + +[[package]] +name = "openssl-sys" +version = "0.9.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8288979acd84749c744a9014b4382d42b8f7b2592847b5afb2ed29e5d16ede07" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "overload" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" + +[[package]] +name = "parking_lot" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-targets 0.52.6", +] + +[[package]] +name = "percent-encoding" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" + +[[package]] +name = "pin-project-lite" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + +[[package]] +name = "pkg-config" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c" + +[[package]] +name = "portable-atomic" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "350e9b48cbc6b0e028b0473b114454c6316e57336ee184ceab6e53f72c178b3e" + +[[package]] +name = "ppv-lite86" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" +dependencies = [ + "zerocopy 0.8.25", +] + +[[package]] +name = "prettyplease" +version = "0.2.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "664ec5419c51e34154eec046ebcba56312d5a2fc3b09a06da188e1ad21afadf6" +dependencies = [ + "proc-macro2", + "syn", +] + +[[package]] +name = "proc-macro2" +version = "1.0.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quanta" +version = "0.12.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3bd1fe6824cea6538803de3ff1bc0cf3949024db3d43c9643024bfb33a807c0e" +dependencies = [ + "crossbeam-utils", + "libc", + "once_cell", + "raw-cpuid", + "wasi 0.11.0+wasi-snapshot-preview1", + "web-sys", + "winapi", +] + +[[package]] +name = "quote" +version = "1.0.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "r-efi" +version = "5.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74765f6d916ee2faa39bc8e68e4f3ed8949b48cccdac59983d287a7cb71ce9c5" + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha 0.3.1", + "rand_core 0.6.4", +] + +[[package]] +name = "rand" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fbfd9d094a40bf3ae768db9361049ace4c0e04a4fd6b359518bd7b73a73dd97" +dependencies = [ + "rand_chacha 0.9.0", + "rand_core 0.9.3", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core 0.6.4", +] + +[[package]] +name = "rand_chacha" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" +dependencies = [ + "ppv-lite86", + "rand_core 0.9.3", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom 0.2.16", +] + +[[package]] +name = "rand_core" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38" +dependencies = [ + "getrandom 0.3.2", +] + +[[package]] +name = "rand_xoshiro" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f703f4665700daf5512dcca5f43afa6af89f09db47fb56be587f80636bda2d41" +dependencies = [ + "rand_core 0.9.3", +] + +[[package]] +name = "raw-cpuid" +version = "11.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6df7ab838ed27997ba19a4664507e6f82b41fe6e20be42929332156e5e85146" +dependencies = [ + "bitflags", +] + +[[package]] +name = "redis" +version = "0.30.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "438a4e5f8e9aa246d6f3666d6978441bf1b37d5f417b50c4dd220be09f5fcc17" +dependencies = [ + "arc-swap", + "combine", + "itoa", + "num-bigint", + "percent-encoding", + "ryu", + "sha1_smol", + "socket2", + "url", +] + +[[package]] +name = "redis-test" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "967d3ffa2d2ead5a95b2e8561d7453c4719c9fe9dbba521673e058e513cb1c24" +dependencies = [ + "rand 0.9.1", + "redis", + "socket2", + "tempfile", +] + +[[package]] +name = "redox_syscall" +version = "0.5.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2f103c6d277498fbceb16e84d317e2a400f160f46904d5f5410848c829511a3" +dependencies = [ + "bitflags", +] + +[[package]] +name = "regex" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata 0.4.9", + "regex-syntax 0.8.5", +] + +[[package]] +name = "regex-automata" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" +dependencies = [ + "regex-syntax 0.6.29", +] + +[[package]] +name = "regex-automata" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax 0.8.5", +] + +[[package]] +name = "regex-syntax" +version = "0.6.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" + +[[package]] +name = "regex-syntax" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" + +[[package]] +name = "reqwest" +version = "0.12.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d19c46a6fdd48bc4dab94b6103fccc55d34c67cc0ad04653aad4ea2a07cd7bbb" +dependencies = [ + "base64", + "bytes", + "futures-core", + "futures-util", + "http", + "http-body", + "http-body-util", + "hyper", + "hyper-tls", + "hyper-util", + "ipnet", + "js-sys", + "log", + "mime", + "native-tls", + "once_cell", + "percent-encoding", + "pin-project-lite", + "rustls-pemfile", + "serde", + "serde_json", + "serde_urlencoded", + "sync_wrapper", + "tokio", + "tokio-native-tls", + "tower", + "tower-service", + "url", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", + "windows-registry", +] + +[[package]] +name = "ring" +version = "0.17.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4689e6c2294d81e88dc6261c768b63bc4fcdb852be6d1352498b114f61383b7" +dependencies = [ + "cc", + "cfg-if", + "getrandom 0.2.16", + "libc", + "untrusted", + "windows-sys 0.52.0", +] + +[[package]] +name = "rustc-demangle" +version = "0.1.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" + +[[package]] +name = "rustc-hash" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" + +[[package]] +name = "rustix" +version = "0.38.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fdb5bc1ae2baa591800df16c9ca78619bf65c0488b41b96ccec5d11220d8c154" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys 0.4.15", + "windows-sys 0.59.0", +] + +[[package]] +name = "rustix" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d97817398dd4bb2e6da002002db259209759911da105da92bec29ccb12cf58bf" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys 0.9.4", + "windows-sys 0.59.0", +] + +[[package]] +name = "rustls" +version = "0.23.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df51b5869f3a441595eac5e8ff14d486ff285f7b8c0df8770e49c3b56351f0f0" +dependencies = [ + "aws-lc-rs", + "once_cell", + "rustls-pki-types", + "rustls-webpki", + "subtle", + "zeroize", +] + +[[package]] +name = "rustls-native-certs" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fcff2dd52b58a8d98a70243663a0d234c4e2b79235637849d15913394a247d3" +dependencies = [ + "openssl-probe", + "rustls-pki-types", + "schannel", + "security-framework 3.2.0", +] + +[[package]] +name = "rustls-pemfile" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dce314e5fee3f39953d46bb63bb8a46d40c2f8fb7cc5a3b6cab2bde9721d6e50" +dependencies = [ + "rustls-pki-types", +] + +[[package]] +name = "rustls-pki-types" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "917ce264624a4b4db1c364dcc35bfca9ded014d0a958cd47ad3e960e988ea51c" + +[[package]] +name = "rustls-webpki" +version = "0.103.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fef8b8769aaccf73098557a87cd1816b4f9c7c16811c9c77142aa695c16f2c03" +dependencies = [ + "aws-lc-rs", + "ring", + "rustls-pki-types", + "untrusted", +] + +[[package]] +name = "rustversion" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eded382c5f5f786b989652c49544c4877d9f015cc22e145a5ea8ea66c2921cd2" + +[[package]] +name = "ryu" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f" + +[[package]] +name = "schannel" +version = "0.1.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f29ebaa345f945cec9fbbc532eb307f0fdad8161f281b6369539c8d84876b3d" +dependencies = [ + "windows-sys 0.59.0", +] + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] +name = "security-framework" +version = "2.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "897b2245f0b511c87893af39b033e5ca9cce68824c4d7e7630b5a1d339658d02" +dependencies = [ + "bitflags", + "core-foundation 0.9.4", + "core-foundation-sys", + "libc", + "security-framework-sys", +] + +[[package]] +name = "security-framework" +version = "3.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "271720403f46ca04f7ba6f55d438f8bd878d6b8ca0a1046e8228c4145bcbb316" +dependencies = [ + "bitflags", + "core-foundation 0.10.0", + "core-foundation-sys", + "libc", + "security-framework-sys", +] + +[[package]] +name = "security-framework-sys" +version = "2.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49db231d56a190491cb4aeda9527f1ad45345af50b0851622a7adb8c03b01c32" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "serde" +version = "1.0.219" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.219" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.140" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20068b6e96dc6c9bd23e01df8827e6c7e1f2fddd43c21810382803c136b99373" +dependencies = [ + "itoa", + "memchr", + "ryu", + "serde", +] + +[[package]] +name = "serde_path_to_error" +version = "0.1.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59fab13f937fa393d08645bf3a84bdfe86e296747b506ada67bb15f10f218b2a" +dependencies = [ + "itoa", + "serde", +] + +[[package]] +name = "serde_urlencoded" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd" +dependencies = [ + "form_urlencoded", + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "sha1" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3bf829a2d51ab4a5ddf1352d8470c140cadc8301b2ae1789db023f01cedd6ba" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + +[[package]] +name = "sha1_smol" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbfa15b3dddfee50a0fff136974b3e1bde555604ba463834a7eb7deb6417705d" + +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + +[[package]] +name = "signal-hook-registry" +version = "1.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9203b8055f63a2a00e2f593bb0510367fe707d7ff1e5c872de2f537b339e5410" +dependencies = [ + "libc", +] + +[[package]] +name = "sketches-ddsketch" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1e9a774a6c28142ac54bb25d25562e6bcf957493a184f15ad4eebccb23e410a" + +[[package]] +name = "slab" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" +dependencies = [ + "autocfg", +] + +[[package]] +name = "smallvec" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8917285742e9f3e1683f0a9c4e6b57960b7314d0b08d30d1ecd426713ee2eee9" + +[[package]] +name = "socket2" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f5fd57c80058a56cf5c777ab8a126398ece8e442983605d280a44ce79d0edef" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + +[[package]] +name = "stable_deref_trait" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "subtle" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" + +[[package]] +name = "syn" +version = "2.0.101" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ce2b7fc941b3a24138a0a7cf8e858bfc6a992e7978a068a5c760deb0ed43caf" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "sync_wrapper" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263" +dependencies = [ + "futures-core", +] + +[[package]] +name = "synstructure" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8af7666ab7b6390ab78131fb5b0fce11d6b7a6951602017c35fa82800708971" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tempfile" +version = "3.19.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7437ac7763b9b123ccf33c338a5cc1bac6f69b45a136c19bdd8a65e3916435bf" +dependencies = [ + "fastrand", + "getrandom 0.3.2", + "once_cell", + "rustix 1.0.5", + "windows-sys 0.59.0", +] + +[[package]] +name = "thiserror" +version = "2.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f7cf42b4507d8ea322120659672cf1b9dbb93f8f2d4ecfd6e51350ff5b17a1d" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "thread_local" +version = "1.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c" +dependencies = [ + "cfg-if", + "once_cell", +] + +[[package]] +name = "tinystr" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9117f5d4db391c1cf6927e7bea3db74b9a1c1add8f7eda9ffd5364f40f57b82f" +dependencies = [ + "displaydoc", + "zerovec", +] + +[[package]] +name = "tokio" +version = "1.44.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6b88822cbe49de4185e3a4cbf8321dd487cf5fe0c5c65695fef6346371e9c48" +dependencies = [ + "backtrace", + "bytes", + "libc", + "mio", + "parking_lot", + "pin-project-lite", + "signal-hook-registry", + "socket2", + "tokio-macros", + "windows-sys 0.52.0", +] + +[[package]] +name = "tokio-macros" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e06d43f1345a3bcd39f6a56dbb7dcab2ba47e68e8ac134855e7e2bdbaf8cab8" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tokio-native-tls" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbae76ab933c85776efabc971569dd6119c580d8f5d448769dec1764bf796ef2" +dependencies = [ + "native-tls", + "tokio", +] + +[[package]] +name = "tokio-rustls" +version = "0.26.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e727b36a1a0e8b74c376ac2211e40c2c8af09fb4013c60d910495810f008e9b" +dependencies = [ + "rustls", + "tokio", +] + +[[package]] +name = "tokio-tungstenite" +version = "0.26.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a9daff607c6d2bf6c16fd681ccb7eecc83e4e2cdc1ca067ffaadfca5de7f084" +dependencies = [ + "futures-util", + "log", + "native-tls", + "tokio", + "tokio-native-tls", + "tungstenite", +] + +[[package]] +name = "tokio-util" +version = "0.7.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "66a539a9ad6d5d281510d5bd368c973d636c02dbf8a67300bfb6b950696ad7df" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "tower" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d039ad9159c98b70ecfd540b2573b97f7f52c3e8d9f8ad57a24b916a536975f9" +dependencies = [ + "futures-core", + "futures-util", + "pin-project-lite", + "sync_wrapper", + "tokio", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "tower-layer" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e" + +[[package]] +name = "tower-service" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3" + +[[package]] +name = "tracing" +version = "0.1.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0" +dependencies = [ + "log", + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "395ae124c09f9e6918a2310af6038fba074bcf474ac352496d5910dd59a2226d" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tracing-core" +version = "0.1.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e672c95779cf947c5311f83787af4fa8fffd12fb27e4993211a84bdfd9610f9c" +dependencies = [ + "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" +dependencies = [ + "log", + "once_cell", + "tracing-core", +] + +[[package]] +name = "tracing-serde" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "704b1aeb7be0d0a84fc9828cae51dab5970fee5088f83d1dd7ee6f6246fc6ff1" +dependencies = [ + "serde", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8189decb5ac0fa7bc8b96b7cb9b2701d60d48805aca84a238004d665fcc4008" +dependencies = [ + "matchers", + "nu-ansi-term", + "once_cell", + "regex", + "serde", + "serde_json", + "sharded-slab", + "smallvec", + "thread_local", + "tracing", + "tracing-core", + "tracing-log", + "tracing-serde", +] + +[[package]] +name = "try-lock" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" + +[[package]] +name = "tungstenite" +version = "0.26.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4793cb5e56680ecbb1d843515b23b6de9a75eb04b66643e256a396d43be33c13" +dependencies = [ + "bytes", + "data-encoding", + "http", + "httparse", + "log", + "native-tls", + "rand 0.9.1", + "sha1", + "thiserror", + "utf-8", +] + +[[package]] +name = "typenum" +version = "1.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1dccffe3ce07af9386bfd29e80c0ab1a8205a2fc34e4bcd40364df902cfa8f3f" + +[[package]] +name = "unicode-ident" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" + +[[package]] +name = "untrusted" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" + +[[package]] +name = "url" +version = "2.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32f8b686cadd1473f4bd0117a5d28d36b1ade384ea9b5069a1c40aefed7fda60" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", +] + +[[package]] +name = "utf-8" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" + +[[package]] +name = "utf16_iter" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8232dd3cdaed5356e0f716d285e4b40b932ac434100fe9b7e0e8e935b9e6246" + +[[package]] +name = "utf8_iter" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "uuid" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "458f7a779bf54acc9f347480ac654f68407d3aab21269a6e3c9f922acd9e2da9" +dependencies = [ + "getrandom 0.3.2", +] + +[[package]] +name = "valuable" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65" + +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + +[[package]] +name = "want" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa7760aed19e106de2c7c0b581b509f2f25d3dacaf737cb82ac61bc6d760b0e" +dependencies = [ + "try-lock", +] + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "wasi" +version = "0.14.2+wasi-0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9683f9a5a998d873c0d21fcbe3c083009670149a8fab228644b8bd36b2c48cb3" +dependencies = [ + "wit-bindgen-rt", +] + +[[package]] +name = "wasm-bindgen" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1edc8929d7499fc4e8f0be2262a241556cfc54a0bea223790e71446f2aab1ef5" +dependencies = [ + "cfg-if", + "once_cell", + "rustversion", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f0a0651a5c2bc21487bde11ee802ccaf4c51935d0d3d42a6101f98161700bc6" +dependencies = [ + "bumpalo", + "log", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.50" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "555d470ec0bc3bb57890405e5d4322cc9ea83cebb085523ced7be4144dac1e61" +dependencies = [ + "cfg-if", + "js-sys", + "once_cell", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fe63fc6d09ed3792bd0897b314f53de8e16568c2b3f7982f468c0bf9bd0b407" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a05d73b933a847d6cccdda8f838a22ff101ad9bf93e33684f39c1f5f0eece3d" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "web-sys" +version = "0.3.77" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33b6dd2ef9186f1f2072e409e99cd22a975331a6b3591b12c764e0e55c60d5d2" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "which" +version = "4.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87ba24419a2078cd2b0f2ede2691b6c66d8e47836da3b6db8265ebad47afbfc7" +dependencies = [ + "either", + "home", + "once_cell", + "rustix 0.38.44", +] + +[[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" + +[[package]] +name = "windows-link" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76840935b766e1b0a05c0066835fb9ec80071d4c09a16f6bd5f7e655e3c14c38" + +[[package]] +name = "windows-registry" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4286ad90ddb45071efd1a66dfa43eb02dd0dfbae1545ad6cc3c51cf34d7e8ba3" +dependencies = [ + "windows-result", + "windows-strings", + "windows-targets 0.53.0", +] + +[[package]] +name = "windows-result" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c64fd11a4fd95df68efcfee5f44a294fe71b8bc6a91993e2791938abcc712252" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-strings" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87fa48cc5d406560701792be122a10132491cff9d0aeb23583cc2dcafc847319" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", + "windows_i686_gnullvm 0.52.6", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", +] + +[[package]] +name = "windows-targets" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1e4c7e8ceaaf9cb7d7507c974735728ab453b67ef8f18febdd7c11fe59dca8b" +dependencies = [ + "windows_aarch64_gnullvm 0.53.0", + "windows_aarch64_msvc 0.53.0", + "windows_i686_gnu 0.53.0", + "windows_i686_gnullvm 0.53.0", + "windows_i686_msvc 0.53.0", + "windows_x86_64_gnu 0.53.0", + "windows_x86_64_gnullvm 0.53.0", + "windows_x86_64_msvc 0.53.0", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86b8d5f90ddd19cb4a147a5fa63ca848db3df085e25fee3cc10b39b6eebae764" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7651a1f62a11b8cbd5e0d42526e55f2c99886c77e007179efff86c2b137e66c" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnu" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1dc67659d35f387f5f6c479dc4e28f1d4bb90ddd1a5d3da2e5d97b42d6272c3" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ce6ccbdedbf6d6354471319e781c0dfef054c81fbc7cf83f338a4296c0cae11" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_i686_msvc" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "581fee95406bb13382d2f65cd4a908ca7b1e4c2f1917f143ba16efe98a589b5d" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e55b5ac9ea33f2fc1716d1742db15574fd6fc8dadc51caab1c16a3d3b4190ba" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a6e035dd0599267ce1ee132e51c27dd29437f63325753051e71dd9e42406c57" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "271414315aff87387382ec3d271b52d7ae78726f5d44ac98b4f4030c91880486" + +[[package]] +name = "wit-bindgen-rt" +version = "0.39.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1" +dependencies = [ + "bitflags", +] + +[[package]] +name = "write16" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1890f4022759daae28ed4fe62859b1236caebfc61ede2f63ed4e695f3f6d936" + +[[package]] +name = "writeable" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e9df38ee2d2c3c5948ea468a8406ff0db0b29ae1ffde1bcf20ef305bcc95c51" + +[[package]] +name = "yoke" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "120e6aef9aa629e3d4f52dc8cc43a015c7724194c97dfaf45180d2daf2b77f40" +dependencies = [ + "serde", + "stable_deref_trait", + "yoke-derive", + "zerofrom", +] + +[[package]] +name = "yoke-derive" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2380878cad4ac9aac1e2435f3eb4020e8374b5f13c296cb75b4620ff8e229154" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] + +[[package]] +name = "zerocopy" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" +dependencies = [ + "zerocopy-derive 0.7.35", +] + +[[package]] +name = "zerocopy" +version = "0.8.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1702d9583232ddb9174e01bb7c15a2ab8fb1bc6f227aa1233858c351a3ba0cb" +dependencies = [ + "zerocopy-derive 0.8.25", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28a6e20d751156648aa063f3800b706ee209a32c0b4d9f24be3d980b01be55ef" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "zerofrom" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50cc42e0333e05660c3587f3bf9d0478688e15d870fab3346451ce7f8c9fbea5" +dependencies = [ + "zerofrom-derive", +] + +[[package]] +name = "zerofrom-derive" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d71e5d6e06ab090c67b5e44993ec16b72dcbaabc526db883a360057678b48502" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] + +[[package]] +name = "zeroize" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde" + +[[package]] +name = "zerovec" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa2b893d79df23bfb12d5461018d408ea19dfafe76c2c7ef6d4eba614f8ff079" +dependencies = [ + "yoke", + "zerofrom", + "zerovec-derive", +] + +[[package]] +name = "zerovec-derive" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6eafa6dfb17584ea3e2bd6e76e0cc15ad7af12b09abdd1ca55961bed9b1063c6" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/websocket-proxy/Cargo.toml b/websocket-proxy/Cargo.toml new file mode 100644 index 0000000..180dbdd --- /dev/null +++ b/websocket-proxy/Cargo.toml @@ -0,0 +1,35 @@ +[package] +name = "flashblocks-websocket-proxy" +version = "0.1.0" +edition = "2021" +rust-version = "1.85" + +[dependencies] +tokio = { version = "1.44.2", features = ["full"] } +tokio-tungstenite = { version = "0.26.2", features = ["native-tls"] } +metrics-exporter-prometheus = { version = "0.17.0", features = ["http-listener"]} +http = "1.2.0" +axum = { version = "0.8.1", features = ["ws"] } +tracing = "0.1.41" +clap = { version = "4", features = ["derive", "env"] } +dotenvy = "0.15.7" +backoff = "0.4.0" +futures = "0.3.31" +tracing-subscriber = { version = "0.3.19", features = ["json", "env-filter"] } +tokio-util = "0.7.12" +reqwest = { version = "0.12.15", default-features = false, features = ["native-tls"] } +metrics = "0.24.1" +metrics-derive = "0.1" +thiserror = "2.0.11" +serde_json = "1.0.138" +hostname = "0.4.0" +redis = "0.30.0" +redis-test = { version = "0.10.0", optional = true } +uuid = { version = "1.16.0", features = ["v4"] } + + +[dependencies.ring] +version = "0.17.12" + +[features] +integration = ["redis-test"] diff --git a/websocket-proxy/Dockerfile b/websocket-proxy/Dockerfile new file mode 100644 index 0000000..f8fe1b6 --- /dev/null +++ b/websocket-proxy/Dockerfile @@ -0,0 +1,18 @@ +FROM rust:1.85 AS builder + +WORKDIR /app + +ARG BINARY="flashblocks-websocket-proxy" +ARG FEATURES + +COPY . . + +RUN cargo build --release --features="$FEATURES" --package=${BINARY} + +FROM gcr.io/distroless/cc-debian12 +WORKDIR /app + +ARG BINARY="flashblocks-websocket-proxy" +COPY --from=builder /app/target/release/${BINARY} /usr/local/bin/ + +ENTRYPOINT ["/usr/local/bin/flashblocks-websocket-proxy"] diff --git a/websocket-proxy/README.md b/websocket-proxy/README.md new file mode 100644 index 0000000..796ad18 --- /dev/null +++ b/websocket-proxy/README.md @@ -0,0 +1,64 @@ +# Flashblocks Websocket Proxy + +## Overview +The Flashblocks Websocket Proxy is a service that subscribes to new Flashblocks from +[rollup-boost](https://github.com/flashbots/rollup-boost) on the sequencer. Then broadcasts them out to any downstream +RPC nodes. Minimizing the number of connections to the sequencer and restricting access. + +> ⚠️ **Warning** +> +> This is currently alpha software -- deploy at your own risk! +> +> Currently, this project is a one-directional generic websocket proxy. It doesn't inspect any data or validate clients. +> This may not always be the case. + +## For Developers + +### Contributing + +### Building & Testing +You can build and test the project using [Cargo](https://doc.rust-lang.org/cargo/). Some useful commands are: +``` +# Build the project +cargo build + +# Run all the tests (requires local version of redis to be installed) +cargo test --all-features +``` + +### Deployment +Builds of the websocket proxy [are provided](https://github.com/base/flashblocks-websocket-proxy/pkgs/container/flashblocks-websocket-proxy). +The only configuration required is the rollup-boost URL to proxy. You can set this via an env var `UPSTREAM_WS` or a flag `--upstream-ws`. + + +You can see a full list of parameters by running: + +`docker run ghcr.io/base/flashblocks-websocket-proxy:master --help` + +### Redis Integration + +The proxy supports distributed rate limiting with Redis. This is useful when running multiple instances of the proxy behind a load balancer, as it allows rate limits to be enforced across all instances. + +To enable Redis integration, use the following parameters: + +- `--redis-url` - Redis connection URL (e.g., `redis://localhost:6379`) +- `--redis-key-prefix` - Prefix for Redis keys (default: `flashblocks`) + +Example: + +```bash +docker run ghcr.io/base/flashblocks-websocket-proxy:master \ + --upstream-ws wss://your-sequencer-endpoint \ + --redis-url redis://redis:6379 \ + --global-connections-limit 1000 \ + --per-ip-connections-limit 10 +``` + +When Redis is enabled, the following features are available: + +- Distributed rate limiting across multiple proxy instances +- Connection tracking persists even if the proxy instance restarts +- More accurate global connection limiting in multi-instance deployments + +If the Redis connection fails, the proxy will automatically fall back to in-memory rate limiting. + diff --git a/websocket-proxy/src/client.rs b/websocket-proxy/src/client.rs new file mode 100644 index 0000000..8c04a85 --- /dev/null +++ b/websocket-proxy/src/client.rs @@ -0,0 +1,28 @@ +use crate::rate_limit::Ticket; +use axum::extract::ws::WebSocket; +use axum::Error; +use std::net::IpAddr; + +pub struct ClientConnection { + client_addr: IpAddr, + _ticket: Ticket, + pub(crate) websocket: WebSocket, +} + +impl ClientConnection { + pub fn new(client_addr: IpAddr, ticket: Ticket, websocket: WebSocket) -> Self { + Self { + client_addr, + _ticket: ticket, + websocket, + } + } + + pub async fn send(&mut self, data: String) -> Result<(), Error> { + self.websocket.send(data.into_bytes().into()).await + } + + pub fn id(&self) -> String { + self.client_addr.to_string() + } +} diff --git a/websocket-proxy/src/integration.rs b/websocket-proxy/src/integration.rs new file mode 100644 index 0000000..5bb2f5f --- /dev/null +++ b/websocket-proxy/src/integration.rs @@ -0,0 +1,308 @@ +mod test { + use crate::metrics::Metrics; + use crate::rate_limit::InMemoryRateLimit; + use crate::registry::Registry; + use crate::server::Server; + use futures::StreamExt; + use std::collections::hash_map::Entry; + use std::collections::HashMap; + use std::error::Error; + use std::net::SocketAddr; + use std::sync::{Arc, Mutex}; + use std::time::Duration; + use tokio::net::TcpListener; + use tokio::sync::broadcast; + use tokio::sync::broadcast::Sender; + use tokio::task::JoinHandle; + use tokio_tungstenite::connect_async; + use tokio_util::sync::CancellationToken; + use tracing::error; + + struct TestHarness { + received_messages: Arc>>>, + clients_failed_to_connect: Arc>>, + current_client_id: usize, + cancel_token: CancellationToken, + server: Server, + server_addr: SocketAddr, + client_id_to_handle: HashMap>, + sender: Sender, + } + + impl TestHarness { + async fn alloc_port() -> SocketAddr { + let address = SocketAddr::from(([127, 0, 0, 1], 0)); + let listener = TcpListener::bind(&address).await.unwrap(); + listener.local_addr().unwrap() + } + fn new(addr: SocketAddr) -> TestHarness { + let (sender, _) = broadcast::channel(5); + let metrics = Arc::new(Metrics::default()); + let registry = Registry::new(sender.clone(), metrics.clone()); + let rate_limited = Arc::new(InMemoryRateLimit::new(3, 10)); + + Self { + received_messages: Arc::new(Mutex::new(HashMap::new())), + clients_failed_to_connect: Arc::new(Mutex::new(HashMap::new())), + current_client_id: 0, + cancel_token: CancellationToken::new(), + server: Server::new( + addr.into(), + registry, + metrics, + rate_limited, + "header".to_string(), + ), + server_addr: addr, + client_id_to_handle: HashMap::new(), + sender, + } + } + + async fn healthcheck(&self) -> Result<(), Box> { + let url = format!("http://{}/healthz", self.server_addr); + let response = reqwest::get(url).await?; + match response.error_for_status() { + Ok(_) => Ok(()), + Err(e) => Err(e.into()), + } + } + + async fn start_server(&mut self) { + let cancel_token = self.cancel_token.clone(); + let server = self.server.clone(); + + // todo! + let _server_handle = tokio::spawn(async move { + _ = server.listen(cancel_token).await; + }); + + let mut healthy = true; + for _ in 0..5 { + let resp = self.healthcheck().await; + match resp { + Ok(_) => { + healthy = true; + break; + } + Err(_) => { + tokio::time::sleep(Duration::from_millis(25)).await; + } + } + } + + assert!(healthy); + } + + fn connect_client(&mut self) -> usize { + let uri = format!("ws://{}/ws", self.server_addr); + + let client_id = self.current_client_id; + self.current_client_id += 1; + + let results = self.received_messages.clone(); + let failed_conns = self.clients_failed_to_connect.clone(); + + let handle = tokio::spawn(async move { + let (ws_stream, _) = match connect_async(uri).await { + Ok(results) => results, + Err(_) => { + failed_conns.lock().unwrap().insert(client_id, true); + return; + } + }; + + let (_, mut read) = ws_stream.split(); + + loop { + match read.next().await { + Some(Ok(msg)) => { + match results.lock().unwrap().entry(client_id.clone()) { + Entry::Occupied(o) => { + o.into_mut().push(msg.to_string()); + } + Entry::Vacant(v) => { + v.insert(vec![msg.to_string()]); + } + }; + } + Some(Err(e)) => { + error!(message = "error receiving message", error = e.to_string()); + } + None => {} + } + } + }); + + self.client_id_to_handle.insert(client_id, handle); + client_id + } + + fn send_messages(&mut self, messages: Vec<&str>) { + let messages: Vec = messages.into_iter().map(String::from).collect(); + + for message in messages.iter() { + match self.sender.send(message.clone()) { + Ok(_) => {} + Err(_) => { + assert!(false) + } + } + } + } + + async fn wait_for_messages_to_drain(&mut self) { + let mut drained = false; + for _ in 0..5 { + let len = self.sender.len(); + if len > 0 { + tokio::time::sleep(Duration::from_millis(5)).await; + continue; + } else { + drained = true; + break; + } + } + assert!(drained); + } + + fn messages_for_client(&mut self, client_id: usize) -> Vec { + match self.received_messages.lock().unwrap().get(&client_id) { + Some(messages) => messages.clone(), + None => vec![], + } + } + + async fn stop_client(&mut self, client_id: usize) { + if let Some(handle) = self.client_id_to_handle.remove(&client_id) { + handle.abort(); + _ = handle.await; + } else { + assert!(false) + } + } + } + + #[tokio::test] + async fn test_healthcheck() { + let addr = TestHarness::alloc_port().await; + let mut harness = TestHarness::new(addr); + assert!(harness.healthcheck().await.is_err()); + harness.start_server().await; + assert!(harness.healthcheck().await.is_ok()); + } + + #[tokio::test] + async fn test_clients_receive_messages() { + let addr = TestHarness::alloc_port().await; + + let mut harness = TestHarness::new(addr); + harness.start_server().await; + + let client_one = harness.connect_client(); + let client_two = harness.connect_client(); + + tokio::time::sleep(Duration::from_millis(100)).await; + + harness.send_messages(vec!["one", "two"]); + harness.wait_for_messages_to_drain().await; + + assert_eq!(vec!["one", "two"], harness.messages_for_client(client_one)); + assert_eq!(vec!["one", "two"], harness.messages_for_client(client_two)); + } + + #[tokio::test] + async fn test_server_limits_connections() { + let addr = TestHarness::alloc_port().await; + + let mut harness = TestHarness::new(addr); + harness.start_server().await; + + let client_one = harness.connect_client(); + let client_two = harness.connect_client(); + let client_three = harness.connect_client(); + let client_four = harness.connect_client(); + + tokio::time::sleep(Duration::from_millis(100)).await; + + harness.send_messages(vec!["one", "two"]); + harness.wait_for_messages_to_drain().await; + + assert_eq!(vec!["one", "two"], harness.messages_for_client(client_one)); + assert_eq!(vec!["one", "two"], harness.messages_for_client(client_two)); + assert_eq!( + vec!["one", "two"], + harness.messages_for_client(client_three) + ); + + // Client four was not able to be setup as the test has a limit of three + assert!(harness.messages_for_client(client_four).is_empty()); + assert!(harness.clients_failed_to_connect.lock().unwrap()[&client_four]); + } + + #[tokio::test] + async fn test_deregister() { + let addr = TestHarness::alloc_port().await; + + let mut harness = TestHarness::new(addr); + harness.start_server().await; + + assert_eq!(harness.sender.receiver_count(), 0); + + let client_one = harness.connect_client(); + let client_two = harness.connect_client(); + let client_three = harness.connect_client(); + + tokio::time::sleep(Duration::from_millis(100)).await; + + assert_eq!(harness.sender.receiver_count(), 3); + + harness.send_messages(vec!["one", "two"]); + harness.wait_for_messages_to_drain().await; + + assert_eq!(vec!["one", "two"], harness.messages_for_client(client_one)); + assert_eq!(vec!["one", "two"], harness.messages_for_client(client_two)); + assert_eq!( + vec!["one", "two"], + harness.messages_for_client(client_three) + ); + + harness.stop_client(client_three).await; + tokio::time::sleep(Duration::from_millis(100)).await; + + // It takes a couple of messages for dead clients to disconnect. + harness.send_messages(vec!["three"]); + harness.wait_for_messages_to_drain().await; + harness.send_messages(vec!["four"]); + harness.wait_for_messages_to_drain().await; + + // Client three is disconnected + assert_eq!(harness.sender.receiver_count(), 2); + + let client_four = harness.connect_client(); + tokio::time::sleep(Duration::from_millis(100)).await; + assert_eq!(harness.sender.receiver_count(), 3); + + harness.send_messages(vec!["five"]); + harness.wait_for_messages_to_drain().await; + harness.send_messages(vec!["six"]); + harness.wait_for_messages_to_drain().await; + + assert_eq!( + vec!["one", "two", "three", "four", "five", "six"], + harness.messages_for_client(client_one) + ); + assert_eq!( + vec!["one", "two", "three", "four", "five", "six"], + harness.messages_for_client(client_two) + ); + assert_eq!( + vec!["one", "two"], + harness.messages_for_client(client_three) + ); + assert_eq!( + vec!["five", "six"], + harness.messages_for_client(client_four) + ); + } +} diff --git a/websocket-proxy/src/main.rs b/websocket-proxy/src/main.rs new file mode 100644 index 0000000..03c5679 --- /dev/null +++ b/websocket-proxy/src/main.rs @@ -0,0 +1,363 @@ +mod client; +#[cfg(all(feature = "integration", test))] +mod integration; +mod metrics; +mod rate_limit; +mod registry; +mod server; +mod subscriber; + +use crate::metrics::Metrics; +use crate::rate_limit::{InMemoryRateLimit, RateLimit}; +use crate::registry::Registry; +use crate::server::Server; +use crate::subscriber::WebsocketSubscriber; +use axum::http::Uri; +use clap::Parser; +use dotenvy::dotenv; +use metrics_exporter_prometheus::PrometheusBuilder; +use rate_limit::RedisRateLimit; +use std::net::SocketAddr; +use std::sync::Arc; +use tokio::signal::unix::{signal, SignalKind}; +use tokio::sync::broadcast; +use tokio_util::sync::CancellationToken; +use tracing::{error, info, trace, warn, Level}; +use tracing_subscriber::EnvFilter; + +#[derive(Parser, Debug)] +#[command(author, version, about)] +struct Args { + #[arg( + long, + env, + default_value = "0.0.0.0:8545", + help = "The address and port to listen on for incoming connections" + )] + listen_addr: SocketAddr, + + #[arg( + long, + env, + value_delimiter = ',', + help = "WebSocket URI of the upstream server to connect to" + )] + upstream_ws: Vec, + + #[arg( + long, + env, + default_value = "20", + help = "Number of messages to buffer for lagging clients" + )] + message_buffer_size: usize, + + #[arg( + long, + env, + default_value = "100", + help = "Maximum number of concurrently connected clients" + )] + global_connections_limit: usize, + + #[arg( + long, + env, + default_value = "10", + help = "Maximum number of concurrently connected clients" + )] + per_ip_connections_limit: usize, + + #[arg( + long, + env, + default_value = "X-Forwarded-For", + help = "Header to use to determine the clients origin IP" + )] + ip_addr_http_header: String, + + #[arg(long, env, default_value = "info")] + log_level: Level, + + /// Format for logs, can be json or text + #[arg(long, env, default_value = "text")] + log_format: String, + + // Enable Prometheus metrics + #[arg(long, env, default_value = "true")] + metrics: bool, + + /// Address to run the metrics server on + #[arg(long, env, default_value = "0.0.0.0:9000")] + metrics_addr: SocketAddr, + + /// Tags to add to every metrics emitted, should be in the format --metrics-global-labels label1=value1,label2=value2 + #[arg(long, env, default_value = "")] + metrics_global_labels: String, + + /// Add the hostname as a label to all Prometheus metrics + #[arg(long, env, default_value = "false")] + metrics_host_label: bool, + + /// Maximum backoff allowed for upstream connections + #[arg(long, env, default_value = "20")] + subscriber_max_interval: u64, + + #[arg( + long, + env, + help = "Redis URL for distributed rate limiting (e.g., redis://localhost:6379). If not provided, in-memory rate limiting will be used." + )] + redis_url: Option, + + #[arg( + long, + env, + default_value = "flashblocks", + help = "Prefix for Redis keys" + )] + redis_key_prefix: String, +} + +#[tokio::main] +async fn main() { + dotenv().ok(); + let args = Args::parse(); + + let log_format = args.log_format.to_lowercase(); + let log_level = args.log_level.to_string(); + + if log_format == "json" { + tracing_subscriber::fmt() + .json() + .with_env_filter(EnvFilter::new(log_level)) + .with_ansi(false) + .init(); + } else { + tracing_subscriber::fmt() + .with_env_filter(EnvFilter::new(log_level)) + .with_ansi(false) + .init(); + } + + if args.metrics { + info!( + message = "starting metrics server", + address = args.metrics_addr.to_string() + ); + + let mut builder = PrometheusBuilder::new().with_http_listener(args.metrics_addr); + + if args.metrics_host_label { + let hostname = hostname::get() + .expect("could not find hostname") + .into_string() + .expect("could not convert hostname to string"); + builder = builder.add_global_label("hostname", hostname); + } + + for (key, value) in parse_global_metrics(args.metrics_global_labels) { + builder = builder.add_global_label(key, value); + } + + builder + .install() + .expect("failed to setup Prometheus endpoint") + } + + // Validate that we have at least one upstream URI + if args.upstream_ws.is_empty() { + error!(message = "no upstream URIs provided"); + panic!("No upstream URIs provided"); + } + + info!(message = "using upstream URIs", uris = ?args.upstream_ws); + + let metrics = Arc::new(Metrics::default()); + let metrics_clone = metrics.clone(); + + let (send, _rec) = broadcast::channel(args.message_buffer_size); + let sender = send.clone(); + + let listener = move |data: String| { + trace!(message = "received data", data = data); + // Subtract one from receiver count, as we have to keep one receiver open at all times (see _rec) + // to avoid the channel being closed. However this is not an active client connection. + metrics_clone + .active_connections + .set((send.receiver_count() - 1) as f64); + + match send.send(data) { + Ok(_) => (), + Err(e) => error!(message = "failed to send data", error = e.to_string()), + } + }; + + let token = CancellationToken::new(); + let mut subscriber_tasks = Vec::new(); + + // Start a subscriber for each upstream URI + for (index, uri) in args.upstream_ws.iter().enumerate() { + let uri_clone = uri.clone(); + let listener_clone = listener.clone(); + let token_clone = token.clone(); + let metrics_clone = metrics.clone(); + + let mut subscriber = WebsocketSubscriber::new( + uri_clone.clone(), + listener_clone, + args.subscriber_max_interval, + metrics_clone, + ); + + let task = tokio::spawn(async move { + info!( + message = "starting subscriber", + index = index, + uri = uri_clone.to_string() + ); + subscriber.run(token_clone).await; + }); + + subscriber_tasks.push(task); + } + + let registry = Registry::new(sender, metrics.clone()); + + let rate_limiter = match &args.redis_url { + Some(redis_url) => { + info!(message = "Using Redis rate limiter", redis_url = redis_url); + match RedisRateLimit::new( + redis_url, + args.global_connections_limit, + args.per_ip_connections_limit, + &args.redis_key_prefix, + ) { + Ok(limiter) => { + info!(message = "Connected to Redis successfully"); + Arc::new(limiter) as Arc + } + Err(e) => { + error!( + message = + "Failed to connect to Redis, falling back to in-memory rate limiting", + error = e.to_string() + ); + Arc::new(InMemoryRateLimit::new( + args.global_connections_limit, + args.per_ip_connections_limit, + )) as Arc + } + } + } + None => { + info!(message = "Using in-memory rate limiter"); + Arc::new(InMemoryRateLimit::new( + args.global_connections_limit, + args.per_ip_connections_limit, + )) as Arc + } + }; + + let server = Server::new( + args.listen_addr, + registry.clone(), + metrics, + rate_limiter, + args.ip_addr_http_header, + ); + let server_task = server.listen(token.clone()); + + let mut interrupt = signal(SignalKind::interrupt()).unwrap(); + let mut terminate = signal(SignalKind::terminate()).unwrap(); + + tokio::select! { + _ = futures::future::join_all(subscriber_tasks) => { + info!("all subscriber tasks terminated"); + token.cancel(); + }, + _ = server_task => { + info!("server task terminated"); + token.cancel(); + } + _ = interrupt.recv() => { + info!("process interrupted, shutting down"); + token.cancel(); + } + _ = terminate.recv() => { + info!("process terminated, shutting down"); + token.cancel(); + } + } +} + +fn parse_global_metrics(metrics: String) -> Vec<(String, String)> { + let mut result = Vec::new(); + + for metric in metrics.split(',') { + if metric.is_empty() { + continue; + } + + let parts = metric + .splitn(2, '=') + .map(|s| s.to_string()) + .collect::>(); + + if parts.len() != 2 { + warn!( + message = "malformed global metric: invalid count", + metric = metric + ); + continue; + } + + let label = parts[0].to_string(); + let value = parts[1].to_string(); + + if label.is_empty() || value.is_empty() { + warn!( + message = "malformed global metric: empty value", + metric = metric + ); + continue; + } + + result.push((label, value)); + } + + result +} + +#[cfg(test)] +mod test { + use crate::parse_global_metrics; + + #[test] + fn test_parse_global_metrics() { + assert_eq!( + parse_global_metrics("".into()), + Vec::<(String, String)>::new(), + ); + + assert_eq!( + parse_global_metrics("key=value".into()), + vec![("key".into(), "value".into())] + ); + + assert_eq!( + parse_global_metrics("key=value,key2=value2".into()), + vec![ + ("key".into(), "value".into()), + ("key2".into(), "value2".into()) + ], + ); + + assert_eq!(parse_global_metrics("gibberish".into()), Vec::new()); + + assert_eq!( + parse_global_metrics("key=value,key2=,".into()), + vec![("key".into(), "value".into())], + ); + } +} diff --git a/websocket-proxy/src/metrics.rs b/websocket-proxy/src/metrics.rs new file mode 100644 index 0000000..94dfc6d --- /dev/null +++ b/websocket-proxy/src/metrics.rs @@ -0,0 +1,45 @@ +use metrics::{Counter, Gauge}; +use metrics_derive::Metrics; +#[derive(Metrics)] +#[metrics(scope = "websocket_proxy")] +pub struct Metrics { + #[metric(describe = "Messages sent to clients")] + pub sent_messages: Counter, + + #[metric(describe = "Count of messages that were unable to be sent")] + pub failed_messages: Counter, + + #[metric(describe = "Count of new connections opened")] + pub new_connections: Counter, + + #[metric(describe = "Count of number of connections closed")] + pub closed_connections: Counter, + + #[metric(describe = "Number of client connections currently open")] + pub active_connections: Gauge, + + #[metric(describe = "Count of rate limited request")] + pub rate_limited_requests: Counter, + + #[metric(describe = "Count of times that a client lagged")] + pub lag_events: Counter, + + #[metric(describe = "Count of times upstream receiver was closed/errored")] + pub upstream_errors: Counter, + + #[metric(describe = "Count of messages received from the upstream source")] + pub upstream_messages: Gauge, + + // New metrics for multiple upstream connections + #[metric(describe = "Number of active upstream connections")] + pub upstream_connections: Gauge, + + #[metric(describe = "Number of upstream connection attempts")] + pub upstream_connection_attempts: Counter, + + #[metric(describe = "Number of successful upstream connections")] + pub upstream_connection_successes: Counter, + + #[metric(describe = "Number of failed upstream connection attempts")] + pub upstream_connection_failures: Counter, +} diff --git a/websocket-proxy/src/rate_limit.rs b/websocket-proxy/src/rate_limit.rs new file mode 100644 index 0000000..dc7a14a --- /dev/null +++ b/websocket-proxy/src/rate_limit.rs @@ -0,0 +1,849 @@ +use std::collections::HashMap; +use std::net::IpAddr; +use std::sync::{Arc, Mutex}; +use tracing::{debug, error, warn}; + +use thiserror::Error; +use tokio::sync::{OwnedSemaphorePermit, Semaphore}; + +use redis::{Client, Commands, RedisError}; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::time::{Duration, SystemTime}; +use uuid::Uuid; + +#[derive(Error, Debug)] +pub enum RateLimitError { + #[error("Rate Limit Reached: {reason}")] + Limit { reason: String }, +} + +#[clippy::has_significant_drop] +pub struct Ticket { + addr: IpAddr, + _permit: OwnedSemaphorePermit, + rate_limiter: Arc, +} + +impl Drop for Ticket { + fn drop(&mut self) { + self.rate_limiter.release(self.addr) + } +} + +pub trait RateLimit: Send + Sync { + fn try_acquire(self: Arc, addr: IpAddr) -> Result; + + fn release(&self, ticket: IpAddr); +} + +struct Inner { + active_connections: HashMap, + semaphore: Arc, +} + +pub struct InMemoryRateLimit { + per_ip_limit: usize, + inner: Mutex, +} + +impl InMemoryRateLimit { + pub fn new(global_limit: usize, per_ip_limit: usize) -> Self { + Self { + per_ip_limit, + inner: Mutex::new(Inner { + active_connections: HashMap::new(), + semaphore: Arc::new(Semaphore::new(global_limit)), + }), + } + } +} + +impl RateLimit for InMemoryRateLimit { + fn try_acquire(self: Arc, addr: IpAddr) -> Result { + let mut inner = self.inner.lock().unwrap(); + + let permit = + inner + .semaphore + .clone() + .try_acquire_owned() + .map_err(|_| RateLimitError::Limit { + reason: "Global limit".to_owned(), + })?; + + let current_count = match inner.active_connections.get(&addr) { + Some(count) => *count, + None => 0, + }; + + if current_count + 1 > self.per_ip_limit { + debug!( + message = "Rate limit exceeded, trying to acquire", + client = addr.to_string() + ); + return Err(RateLimitError::Limit { + reason: String::from("IP limit exceeded"), + }); + } + + let new_count = current_count + 1; + + inner.active_connections.insert(addr, new_count); + + Ok(Ticket { + addr, + _permit: permit, + rate_limiter: self.clone(), + }) + } + + fn release(&self, addr: IpAddr) { + let mut inner = self.inner.lock().unwrap(); + + let current_count = match inner.active_connections.get(&addr) { + Some(count) => *count, + None => 0, + }; + + let new_count = if current_count == 0 { + warn!( + message = "ip counting is not accurate -- unexpected underflow", + client = addr.to_string() + ); + 0 + } else { + current_count - 1 + }; + + if new_count == 0 { + inner.active_connections.remove(&addr); + } else { + inner.active_connections.insert(addr, new_count); + } + } +} + +pub struct RedisRateLimit { + redis_client: Client, + global_limit: usize, + per_ip_limit: usize, + semaphore: Arc, + key_prefix: String, + instance_id: String, + heartbeat_interval: Duration, + heartbeat_ttl: Duration, + background_tasks_started: AtomicBool, +} + +impl RedisRateLimit { + pub fn new( + redis_url: &str, + global_limit: usize, + per_ip_limit: usize, + key_prefix: &str, + ) -> Result { + let client = Client::open(redis_url)?; + let instance_id = Uuid::new_v4().to_string(); + + let heartbeat_interval = Duration::from_secs(10); + let heartbeat_ttl = Duration::from_secs(30); + + let rate_limiter = Self { + redis_client: client, + global_limit, + per_ip_limit, + semaphore: Arc::new(Semaphore::new(global_limit)), + key_prefix: key_prefix.to_string(), + instance_id, + heartbeat_interval, + heartbeat_ttl, + background_tasks_started: AtomicBool::new(false), + }; + + if let Err(e) = rate_limiter.register_instance() { + error!( + message = "Failed to register instance in Redis", + error = e.to_string() + ); + } + + Ok(rate_limiter) + } + + pub fn start_background_tasks(self: Arc) { + if self.background_tasks_started.swap(true, Ordering::SeqCst) { + return; + } + + debug!( + message = "Starting background heartbeat and cleanup tasks", + instance_id = self.instance_id + ); + + let self_clone = self.clone(); + tokio::spawn(async move { + loop { + if let Err(e) = self_clone.update_heartbeat() { + error!( + message = "Failed to update heartbeat in background task", + error = e.to_string() + ); + } + + if let Err(e) = self_clone.cleanup_stale_instances() { + error!( + message = "Failed to cleanup stale instances in background task", + error = e.to_string() + ); + } + + tokio::time::sleep(self_clone.heartbeat_interval / 2).await; + } + }); + } + + fn register_instance(&self) -> Result<(), RedisError> { + self.update_heartbeat()?; + debug!( + message = "Registered instance in Redis", + instance_id = self.instance_id + ); + + Ok(()) + } + + fn update_heartbeat(&self) -> Result<(), RedisError> { + let now = SystemTime::now(); + let mut conn = self.redis_client.get_connection()?; + + let ttl = self.heartbeat_ttl.as_secs(); + conn.set_ex::<_, _, ()>( + self.instance_heartbeat_key(), + now.duration_since(SystemTime::UNIX_EPOCH) + .unwrap() + .as_secs(), + ttl, + )?; + + debug!( + message = "Updated instance heartbeat", + instance_id = self.instance_id + ); + + Ok(()) + } + + fn cleanup_stale_instances(&self) -> Result<(), RedisError> { + let mut conn = self.redis_client.get_connection()?; + + let instance_heartbeat_pattern = format!("{}:instance:*:heartbeat", self.key_prefix); + let instance_heartbeats: Vec = conn.keys(instance_heartbeat_pattern)?; + + let active_instance_ids: Vec = instance_heartbeats + .iter() + .filter_map(|key| key.split(':').nth(2).map(String::from)) + .collect(); + + debug!( + message = "Active instances with heartbeats", + instance_count = active_instance_ids.len(), + current_instance = self.instance_id + ); + + let ip_instance_pattern = format!("{}:ip:*:instance:*:connections", self.key_prefix); + let ip_instance_keys: Vec = conn.keys(ip_instance_pattern)?; + + let mut instance_ids_with_connections = std::collections::HashSet::new(); + for key in &ip_instance_keys { + if let Some(instance_id) = key.split(':').nth(4) { + instance_ids_with_connections.insert(instance_id.to_string()); + } + } + + debug!( + message = "Checking for stale instances", + instances_with_connections = instance_ids_with_connections.len(), + current_instance = self.instance_id + ); + + for instance_id in instance_ids_with_connections { + if instance_id == self.instance_id { + debug!( + message = "Skipping current instance", + instance_id = instance_id + ); + continue; + } + + if !active_instance_ids.contains(&instance_id) { + debug!( + message = "Found stale instance", + instance_id = instance_id, + reason = "Heartbeat key not found" + ); + self.cleanup_instance(&mut conn, &instance_id)?; + } + } + + debug!(message = "Completed stale instance cleanup"); + + Ok(()) + } + + fn cleanup_instance( + &self, + conn: &mut redis::Connection, + instance_id: &str, + ) -> Result<(), RedisError> { + let ip_instance_pattern = format!( + "{}:ip:*:instance:{}:connections", + self.key_prefix, instance_id + ); + let ip_instance_keys: Vec = conn.keys(ip_instance_pattern)?; + + debug!( + message = "Cleaning up instance", + instance_id = instance_id, + ip_key_count = ip_instance_keys.len() + ); + + for key in ip_instance_keys { + conn.del::<_, ()>(&key)?; + debug!(message = "Deleted IP instance key", key = key); + } + + Ok(()) + } + + fn ip_instance_key(&self, addr: &IpAddr) -> String { + format!( + "{}:ip:{}:instance:{}:connections", + self.key_prefix, addr, self.instance_id + ) + } + + fn instance_heartbeat_key(&self) -> String { + format!( + "{}:instance:{}:heartbeat", + self.key_prefix, self.instance_id + ) + } +} + +impl RateLimit for RedisRateLimit { + fn try_acquire(self: Arc, addr: IpAddr) -> Result { + self.clone().start_background_tasks(); + + let permit = match self.semaphore.clone().try_acquire_owned() { + Ok(permit) => permit, + Err(_) => { + return Err(RateLimitError::Limit { + reason: "Maximum connection limit reached for this server instance".to_string(), + }); + } + }; + + let mut conn = match self.redis_client.get_connection() { + Ok(conn) => conn, + Err(e) => { + error!( + message = "Failed to connect to Redis", + error = e.to_string() + ); + return Err(RateLimitError::Limit { + reason: "Redis connection failed".to_string(), + }); + } + }; + + let ip_instance_pattern = format!("{}:ip:*:instance:*:connections", self.key_prefix); + let ip_instance_keys: Vec = match conn.keys(ip_instance_pattern) { + Ok(keys) => keys, + Err(e) => { + error!( + message = "Failed to get IP instance keys from Redis", + error = e.to_string() + ); + return Err(RateLimitError::Limit { + reason: "Redis operation failed".to_string(), + }); + } + }; + + let mut total_global_connections: usize = 0; + for key in &ip_instance_keys { + let count: usize = conn.get(key).unwrap_or(0); + total_global_connections += count; + } + + if total_global_connections >= self.global_limit { + debug!( + message = "Global limit reached", + global_connections = total_global_connections, + global_limit = self.global_limit + ); + return Err(RateLimitError::Limit { + reason: "Global connection limit reached".to_string(), + }); + } + + let ip_keys_pattern = format!("{}:ip:{}:instance:*:connections", self.key_prefix, addr); + let ip_keys: Vec = match conn.keys(ip_keys_pattern) { + Ok(keys) => keys, + Err(e) => { + error!( + message = "Failed to get IP instance keys from Redis", + error = e.to_string() + ); + return Err(RateLimitError::Limit { + reason: "Redis operation failed".to_string(), + }); + } + }; + + let mut total_ip_connections: usize = 0; + for key in &ip_keys { + let count: usize = conn.get(key).unwrap_or(0); + total_ip_connections += count; + } + + if total_ip_connections >= self.per_ip_limit { + return Err(RateLimitError::Limit { + reason: format!("Per-IP connection limit reached for {}", addr), + }); + } + + let ip_instance_connections: usize = match conn.incr(self.ip_instance_key(&addr), 1) { + Ok(count) => count, + Err(e) => { + error!( + message = "Failed to increment per-instance IP counter in Redis", + error = e.to_string() + ); + return Err(RateLimitError::Limit { + reason: "Redis operation failed".to_string(), + }); + } + }; + + debug!( + message = "Connection established", + ip = addr.to_string(), + ip_instance_connections = ip_instance_connections, + total_ip_connections = total_ip_connections + 1, + total_global_connections = total_global_connections + 1, + instance_id = self.instance_id + ); + + Ok(Ticket { + addr, + _permit: permit, + rate_limiter: self, + }) + } + + fn release(&self, addr: IpAddr) { + match self.redis_client.get_connection() { + Ok(mut conn) => { + let ip_instance_connections: Result = + conn.decr(self.ip_instance_key(&addr), 1); + + if let Err(ref e) = ip_instance_connections { + error!( + message = "Failed to decrement per-instance IP counter in Redis", + error = e.to_string() + ); + } + + debug!( + message = "Connection released", + ip = addr.to_string(), + ip_instance_connections = ip_instance_connections.unwrap_or(0), + instance_id = self.instance_id + ); + } + Err(e) => { + error!( + message = "Failed to connect to Redis for release", + error = e.to_string() + ); + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::str::FromStr; + + const GLOBAL_LIMIT: usize = 3; + const PER_IP_LIMIT: usize = 2; + + #[tokio::test] + async fn test_tickets_are_released() { + let user_1 = IpAddr::from_str("127.0.0.1").unwrap(); + + let rate_limiter = Arc::new(InMemoryRateLimit::new(GLOBAL_LIMIT, PER_IP_LIMIT)); + + assert_eq!( + rate_limiter + .inner + .lock() + .unwrap() + .semaphore + .available_permits(), + GLOBAL_LIMIT + ); + assert_eq!( + rate_limiter.inner.lock().unwrap().active_connections.len(), + 0 + ); + + let c1 = rate_limiter.clone().try_acquire(user_1).unwrap(); + + assert_eq!( + rate_limiter + .inner + .lock() + .unwrap() + .semaphore + .available_permits(), + GLOBAL_LIMIT - 1 + ); + assert_eq!( + rate_limiter.inner.lock().unwrap().active_connections.len(), + 1 + ); + assert_eq!( + rate_limiter.inner.lock().unwrap().active_connections[&user_1], + 1 + ); + + drop(c1); + + assert_eq!( + rate_limiter + .inner + .lock() + .unwrap() + .semaphore + .available_permits(), + GLOBAL_LIMIT + ); + assert_eq!( + rate_limiter.inner.lock().unwrap().active_connections.len(), + 0 + ); + } + + #[tokio::test] + async fn test_global_rate_limits() { + let user_1 = IpAddr::from_str("127.0.0.1").unwrap(); + let user_2 = IpAddr::from_str("128.0.0.1").unwrap(); + + let rate_limiter = Arc::new(InMemoryRateLimit::new(GLOBAL_LIMIT, PER_IP_LIMIT)); + + let _c1 = rate_limiter.clone().try_acquire(user_1).unwrap(); + + let _c2 = rate_limiter.clone().try_acquire(user_2).unwrap(); + + let _c3 = rate_limiter.clone().try_acquire(user_1).unwrap(); + + assert_eq!( + rate_limiter + .inner + .lock() + .unwrap() + .semaphore + .available_permits(), + 0 + ); + + let c4 = rate_limiter.clone().try_acquire(user_2); + assert!(c4.is_err()); + assert_eq!( + c4.err().unwrap().to_string(), + "Rate Limit Reached: Global limit" + ); + + drop(_c3); + + let c4 = rate_limiter.clone().try_acquire(user_2); + assert!(c4.is_ok()); + } + + #[tokio::test] + async fn test_per_ip_limits() { + let user_1 = IpAddr::from_str("127.0.0.1").unwrap(); + let user_2 = IpAddr::from_str("127.0.0.2").unwrap(); + + let rate_limiter = Arc::new(InMemoryRateLimit::new(GLOBAL_LIMIT, PER_IP_LIMIT)); + + let _c1 = rate_limiter.clone().try_acquire(user_1).unwrap(); + let _c2 = rate_limiter.clone().try_acquire(user_1).unwrap(); + + assert_eq!( + rate_limiter.inner.lock().unwrap().active_connections[&user_1], + 2 + ); + + let c3 = rate_limiter.clone().try_acquire(user_1); + assert!(c3.is_err()); + assert_eq!( + c3.err().unwrap().to_string(), + "Rate Limit Reached: IP limit exceeded" + ); + + let c4 = rate_limiter.clone().try_acquire(user_2); + assert!(c4.is_ok()); + } + + #[tokio::test] + async fn test_global_limits_with_multiple_ips() { + let user_1 = IpAddr::from_str("127.0.0.1").unwrap(); + let user_2 = IpAddr::from_str("127.0.0.2").unwrap(); + let user_3 = IpAddr::from_str("127.0.0.3").unwrap(); + + let rate_limiter = Arc::new(InMemoryRateLimit::new(4, 3)); + + let ticket_1_1 = rate_limiter.clone().try_acquire(user_1).unwrap(); + let ticket_1_2 = rate_limiter.clone().try_acquire(user_1).unwrap(); + + let ticket_2_1 = rate_limiter.clone().try_acquire(user_2).unwrap(); + let ticket_2_2 = rate_limiter.clone().try_acquire(user_2).unwrap(); + + assert_eq!( + rate_limiter + .inner + .lock() + .unwrap() + .semaphore + .available_permits(), + 0 + ); + + // Try user_3 - should fail due to global limit + let result = rate_limiter.clone().try_acquire(user_3); + assert!(result.is_err()); + assert_eq!( + result.err().unwrap().to_string(), + "Rate Limit Reached: Global limit" + ); + + drop(ticket_1_1); + + let ticket_3_1 = rate_limiter.clone().try_acquire(user_3).unwrap(); + + drop(ticket_1_2); + drop(ticket_2_1); + drop(ticket_2_2); + drop(ticket_3_1); + + assert_eq!( + rate_limiter + .inner + .lock() + .unwrap() + .semaphore + .available_permits(), + 4 + ); + assert_eq!( + rate_limiter.inner.lock().unwrap().active_connections.len(), + 0 + ); + } + + #[tokio::test] + async fn test_per_ip_limits_remain_enforced() { + let user_1 = IpAddr::from_str("127.0.0.1").unwrap(); + let user_2 = IpAddr::from_str("127.0.0.2").unwrap(); + + let rate_limiter = Arc::new(InMemoryRateLimit::new(5, 2)); + + let ticket_1_1 = rate_limiter.clone().try_acquire(user_1).unwrap(); + let ticket_1_2 = rate_limiter.clone().try_acquire(user_1).unwrap(); + + let result = rate_limiter.clone().try_acquire(user_1); + assert!(result.is_err()); + assert_eq!( + result.err().unwrap().to_string(), + "Rate Limit Reached: IP limit exceeded" + ); + + let ticket_2_1 = rate_limiter.clone().try_acquire(user_2).unwrap(); + drop(ticket_1_1); + + let ticket_1_3 = rate_limiter.clone().try_acquire(user_1).unwrap(); + + let result = rate_limiter.clone().try_acquire(user_1); + assert!(result.is_err()); + assert_eq!( + result.err().unwrap().to_string(), + "Rate Limit Reached: IP limit exceeded" + ); + + drop(ticket_1_2); + drop(ticket_1_3); + drop(ticket_2_1); + + assert_eq!( + rate_limiter + .inner + .lock() + .unwrap() + .semaphore + .available_permits(), + 5 + ); + assert_eq!( + rate_limiter.inner.lock().unwrap().active_connections.len(), + 0 + ); + } + + #[tokio::test] + #[cfg(all(feature = "integration", test))] + async fn test_instance_tracking_and_cleanup() { + use redis_test::server::RedisServer; + use std::time::Duration; + + let server = RedisServer::new(); + let client_addr = format!("redis://{}", server.client_addr()); + + tokio::time::sleep(Duration::from_millis(100)).await; + + let user_1 = IpAddr::from_str("127.0.0.1").unwrap(); + let user_2 = IpAddr::from_str("127.0.0.2").unwrap(); + + let redis_client = Client::open(client_addr.as_str()).unwrap(); + + { + let rate_limiter1 = Arc::new(RedisRateLimit { + redis_client: Client::open(client_addr.as_str()).unwrap(), + global_limit: 10, + per_ip_limit: 5, + semaphore: Arc::new(Semaphore::new(10)), + key_prefix: "test".to_string(), + instance_id: "instance1".to_string(), + heartbeat_interval: Duration::from_millis(200), + heartbeat_ttl: Duration::from_secs(1), + background_tasks_started: AtomicBool::new(true), + }); + + rate_limiter1.register_instance().unwrap(); + let _ticket1 = rate_limiter1.clone().try_acquire(user_1).unwrap(); + let _ticket2 = rate_limiter1.clone().try_acquire(user_2).unwrap(); + // no drop on release (exit of block) + std::mem::forget(_ticket1); + std::mem::forget(_ticket2); + + { + let mut conn = redis_client.get_connection().unwrap(); + + let exists: bool = redis::cmd("EXISTS") + .arg(format!("test:instance:instance1:heartbeat")) + .query(&mut conn) + .unwrap(); + assert!(exists, "Instance1 heartbeat should exist initially"); + + let ip1_instance1_count: usize = redis::cmd("GET") + .arg("test:ip:127.0.0.1:instance:instance1:connections") + .query(&mut conn) + .unwrap(); + let ip2_instance1_count: usize = redis::cmd("GET") + .arg("test:ip:127.0.0.2:instance:instance1:connections") + .query(&mut conn) + .unwrap(); + + assert_eq!(ip1_instance1_count, 1, "IP1 count should be 1 initially"); + assert_eq!(ip2_instance1_count, 1, "IP2 count should be 1 initially"); + } + }; + + tokio::time::sleep(Duration::from_secs(1)).await; + + { + let mut conn = redis_client.get_connection().unwrap(); + + let exists: bool = redis::cmd("EXISTS") + .arg(format!("test:instance:instance1:heartbeat")) + .query(&mut conn) + .unwrap(); + assert!( + !exists, + "Instance1 heartbeat should be gone after TTL expiration" + ); + + let ip1_instance1_count: usize = redis::cmd("GET") + .arg("test:ip:127.0.0.1:instance:instance1:connections") + .query(&mut conn) + .unwrap(); + let ip2_instance1_count: usize = redis::cmd("GET") + .arg("test:ip:127.0.0.2:instance:instance1:connections") + .query(&mut conn) + .unwrap(); + + assert_eq!( + ip1_instance1_count, 1, + "IP1 instance1 count should still be 1 after instance1 crash" + ); + assert_eq!( + ip2_instance1_count, 1, + "IP2 instance1 count should still be 1 after crash" + ); + } + + let rate_limiter2 = Arc::new(RedisRateLimit { + redis_client: Client::open(client_addr.as_str()).unwrap(), + global_limit: 10, + per_ip_limit: 5, + semaphore: Arc::new(Semaphore::new(10)), + key_prefix: "test".to_string(), + instance_id: "instance2".to_string(), + heartbeat_interval: Duration::from_millis(200), + heartbeat_ttl: Duration::from_secs(2), + background_tasks_started: AtomicBool::new(false), + }); + + rate_limiter2.register_instance().unwrap(); + rate_limiter2.cleanup_stale_instances().unwrap(); + + tokio::time::sleep(Duration::from_secs(1)).await; + + { + let mut conn = redis_client.get_connection().unwrap(); + + let ip1_instance1_exists: bool = redis::cmd("EXISTS") + .arg("test:ip:127.0.0.1:instance:instance1:connections") + .query(&mut conn) + .unwrap(); + let ip2_instance1_exists: bool = redis::cmd("EXISTS") + .arg("test:ip:127.0.0.2:instance:instance1:connections") + .query(&mut conn) + .unwrap(); + + assert!( + !ip1_instance1_exists, + "IP1 instance1 counter should be gone after cleanup" + ); + assert!( + !ip2_instance1_exists, + "IP2 instance1 counter should be gone after cleanup" + ); + } + + let _ticket3 = rate_limiter2.clone().try_acquire(user_1).unwrap(); + + { + let mut conn = redis_client.get_connection().unwrap(); + let ip1_instance2_count: usize = redis::cmd("GET") + .arg("test:ip:127.0.0.1:instance:instance2:connections") + .query(&mut conn) + .unwrap(); + + assert_eq!(ip1_instance2_count, 1, "IP1 instance2 count should be 1"); + } + } +} diff --git a/websocket-proxy/src/registry.rs b/websocket-proxy/src/registry.rs new file mode 100644 index 0000000..4dfa06b --- /dev/null +++ b/websocket-proxy/src/registry.rs @@ -0,0 +1,60 @@ +use crate::client::ClientConnection; +use crate::metrics::Metrics; +use std::sync::Arc; +use tokio::sync::broadcast::error::RecvError; +use tokio::sync::broadcast::Sender; +use tracing::{info, trace, warn}; + +#[derive(Clone)] +pub struct Registry { + sender: Sender, + metrics: Arc, +} + +impl Registry { + pub fn new(sender: Sender, metrics: Arc) -> Self { + Self { sender, metrics } + } + + pub async fn subscribe(&self, mut client: ClientConnection) { + info!(message = "subscribing client", client = client.id()); + + let mut receiver = self.sender.subscribe(); + let metrics = self.metrics.clone(); + metrics.new_connections.increment(1); + + tokio::spawn(async move { + loop { + match receiver.recv().await { + Ok(msg) => match client.send(msg.clone()).await { + Ok(_) => { + trace!(message = "message sent to client", client = client.id()); + metrics.sent_messages.increment(1); + } + Err(e) => { + warn!( + message = "failed to send data to client", + client = client.id(), + error = e.to_string() + ); + metrics.failed_messages.increment(1); + break; + } + }, + Err(RecvError::Closed) => { + info!(message = "upstream connection closed", client = client.id()); + break; + } + Err(RecvError::Lagged(_)) => { + info!(message = "client is lagging", client = client.id()); + metrics.lag_events.increment(1); + receiver = receiver.resubscribe(); + } + } + } + + metrics.closed_connections.increment(1); + info!(message = "client disconnected", client = client.id()); + }); + } +} diff --git a/websocket-proxy/src/server.rs b/websocket-proxy/src/server.rs new file mode 100644 index 0000000..be7c16d --- /dev/null +++ b/websocket-proxy/src/server.rs @@ -0,0 +1,174 @@ +use crate::client::ClientConnection; +use crate::metrics::Metrics; +use crate::rate_limit::{RateLimit, RateLimitError}; +use crate::registry::Registry; +use axum::body::Body; +use axum::extract::{ConnectInfo, State, WebSocketUpgrade}; +use axum::http::StatusCode; +use axum::response::{IntoResponse, Response}; +use axum::routing::{any, get}; +use axum::{Error, Router}; +use http::{HeaderMap, HeaderValue}; +use serde_json::json; +use std::net::{IpAddr, SocketAddr}; +use std::sync::Arc; +use tokio_util::sync::CancellationToken; +use tracing::{info, warn}; + +#[derive(Clone)] +struct ServerState { + registry: Registry, + rate_limiter: Arc, + metrics: Arc, + ip_addr_http_header: String, +} + +#[derive(Clone)] +pub struct Server { + listen_addr: SocketAddr, + registry: Registry, + rate_limiter: Arc, + metrics: Arc, + ip_addr_http_header: String, +} + +impl Server { + pub fn new( + listen_addr: SocketAddr, + registry: Registry, + metrics: Arc, + rate_limiter: Arc, + ip_addr_http_header: String, + ) -> Self { + Self { + listen_addr, + registry, + rate_limiter, + metrics, + ip_addr_http_header, + } + } + + pub async fn listen(&self, cancellation_token: CancellationToken) { + let router = Router::new() + .route("/healthz", get(healthz_handler)) + .route("/ws", any(websocket_handler)) + .with_state(ServerState { + registry: self.registry.clone(), + rate_limiter: self.rate_limiter.clone(), + metrics: self.metrics.clone(), + ip_addr_http_header: self.ip_addr_http_header.clone(), + }); + + let listener = tokio::net::TcpListener::bind(self.listen_addr) + .await + .unwrap(); + + info!( + message = "starting server", + address = listener.local_addr().unwrap().to_string() + ); + + axum::serve( + listener, + router.into_make_service_with_connect_info::(), + ) + .with_graceful_shutdown(cancellation_token.cancelled_owned()) + .await + .unwrap() + } +} + +async fn healthz_handler() -> impl IntoResponse { + StatusCode::OK +} + +async fn websocket_handler( + State(state): State, + ws: WebSocketUpgrade, + ConnectInfo(addr): ConnectInfo, + headers: HeaderMap, +) -> impl IntoResponse { + let connect_addr = addr.ip(); + + let client_addr = match headers.get(state.ip_addr_http_header) { + None => connect_addr, + Some(value) => extract_addr(value, connect_addr), + }; + + let ticket = match state.rate_limiter.try_acquire(client_addr) { + Ok(ticket) => ticket, + Err(RateLimitError::Limit { reason }) => { + state.metrics.rate_limited_requests.increment(1); + + return Response::builder() + .status(StatusCode::TOO_MANY_REQUESTS) + .body(Body::from(json!({"message": reason}).to_string())) + .unwrap(); + } + }; + + ws.on_failed_upgrade(move |e: Error| { + info!( + message = "failed to upgrade connection", + error = e.to_string(), + client = addr.to_string() + ) + }) + .on_upgrade(async move |socket| { + let client = ClientConnection::new(client_addr, ticket, socket); + state.registry.subscribe(client).await; + }) +} + +fn extract_addr(header: &HeaderValue, fallback: IpAddr) -> IpAddr { + if header.is_empty() { + return fallback; + } + + match header.to_str() { + Ok(header_value) => { + let raw_value = header_value + .split(',') + .map(|ip| ip.trim().to_string()) + .next_back(); + + if let Some(raw_value) = raw_value { + return raw_value.parse::().unwrap_or(fallback); + } + + fallback + } + Err(e) => { + warn!( + message = "could not get header value", + error = e.to_string() + ); + fallback + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::net::Ipv4Addr; + + #[tokio::test] + async fn test_header_addr() { + let fb = Ipv4Addr::new(127, 0, 0, 1); + + let test = |header: &str, expected: Ipv4Addr| { + let hv = HeaderValue::from_str(header).unwrap(); + let result = extract_addr(&hv, IpAddr::V4(fb)); + assert_eq!(result, expected); + }; + + test("129.1.1.1", Ipv4Addr::new(129, 1, 1, 1)); + test("129.1.1.1,130.1.1.1", Ipv4Addr::new(130, 1, 1, 1)); + test("129.1.1.1 , 130.1.1.1 ", Ipv4Addr::new(130, 1, 1, 1)); + test("nonsense", fb); + test("400.0.0.1", fb); + test("120.0.0.1.0", fb); + } +} diff --git a/websocket-proxy/src/subscriber.rs b/websocket-proxy/src/subscriber.rs new file mode 100644 index 0000000..1f7ca50 --- /dev/null +++ b/websocket-proxy/src/subscriber.rs @@ -0,0 +1,371 @@ +use crate::metrics::Metrics; +use axum::http::Uri; +use backoff::{backoff::Backoff, ExponentialBackoff}; +use futures::StreamExt; +use std::sync::Arc; +use std::time::Duration; +use tokio::select; +use tokio_tungstenite::{connect_async, tungstenite::Error}; +use tokio_util::sync::CancellationToken; +use tracing::{error, info, trace, warn}; + +pub struct WebsocketSubscriber +where + F: Fn(String) + Send + Sync + 'static, +{ + uri: Uri, + handler: F, + backoff: ExponentialBackoff, + metrics: Arc, +} + +impl WebsocketSubscriber +where + F: Fn(String) + Send + Sync + 'static, +{ + pub fn new(uri: Uri, handler: F, max_interval: u64, metrics: Arc) -> Self { + let backoff = ExponentialBackoff { + initial_interval: Duration::from_secs(1), + max_interval: Duration::from_secs(max_interval), + max_elapsed_time: None, // Will retry indefinitely + ..Default::default() + }; + + Self { + uri, + handler, + backoff, + metrics, + } + } + + pub async fn run(&mut self, token: CancellationToken) { + info!( + message = "starting upstream subscription", + uri = self.uri.to_string() + ); + loop { + select! { + _ = token.cancelled() => { + info!( + message = "cancelled upstream subscription", + uri = self.uri.to_string() + ); + return; + } + result = self.connect_and_listen() => { + match result { + Ok(()) => { + info!( + message = "upstream connection closed", + uri = self.uri.to_string() + ); + } + Err(e) => { + error!( + message = "upstream websocket error", + uri = self.uri.to_string(), + error = e.to_string() + ); + self.metrics.upstream_errors.increment(1); + // Decrement the active connections count when connection fails + self.metrics.upstream_connections.decrement(1); + + if let Some(duration) = self.backoff.next_backoff() { + warn!( + message = "reconnecting", + uri = self.uri.to_string(), + seconds = duration.as_secs() + ); + select! { + _ = token.cancelled() => { + info!( + message = "cancelled subscriber during backoff", + uri = self.uri.to_string() + ); + return + } + _ = tokio::time::sleep(duration) => {} + } + } + } + } + } + } + } + } + + async fn connect_and_listen(&mut self) -> Result<(), Error> { + info!( + message = "connecting to websocket", + uri = self.uri.to_string() + ); + + // Increment connection attempts counter for metrics + self.metrics.upstream_connection_attempts.increment(1); + + // Modified connection with success/failure metrics tracking + let (ws_stream, _) = match connect_async(&self.uri).await { + Ok(connection) => { + // Track successful connections + self.metrics.upstream_connection_successes.increment(1); + connection + } + Err(e) => { + // Track failed connections + self.metrics.upstream_connection_failures.increment(1); + return Err(e); + } + }; + + info!( + message = "websocket connection established", + uri = self.uri.to_string() + ); + + // Increment active connections counter + self.metrics.upstream_connections.increment(1); + // Reset backoff timer on successful connection + self.backoff.reset(); + + let (_, mut read) = ws_stream.split(); + + while let Some(message) = read.next().await { + match message { + Ok(msg) => { + let text = msg.to_text()?; + trace!( + message = "received message", + uri = self.uri.to_string(), + payload = text + ); + self.metrics.upstream_messages.increment(1); + (self.handler)(text.into()); + } + Err(e) => { + error!( + message = "error receiving message", + uri = self.uri.to_string(), + error = e.to_string() + ); + return Err(e); + } + } + } + + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::metrics::Metrics; + use axum::http::Uri; + use futures::SinkExt; + use std::net::SocketAddr; + use std::sync::{Arc, Mutex}; + use tokio::net::{TcpListener, TcpStream}; + use tokio::sync::broadcast; + use tokio::time::{sleep, timeout, Duration}; + use tokio_tungstenite::{accept_async, tungstenite::Message}; + + struct MockServer { + addr: SocketAddr, + message_sender: broadcast::Sender, + shutdown: CancellationToken, + } + + impl MockServer { + async fn new() -> Self { + let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); + let addr = listener.local_addr().unwrap(); + let (tx, _) = broadcast::channel::(100); + let shutdown = CancellationToken::new(); + let shutdown_clone = shutdown.clone(); + let tx_clone = tx.clone(); + + tokio::spawn(async move { + loop { + select! { + _ = shutdown_clone.cancelled() => { + break; + } + accept_result = listener.accept() => { + match accept_result { + Ok((stream, _)) => { + let tx = tx_clone.clone(); + let shutdown = shutdown_clone.clone(); + tokio::spawn(async move { + Self::handle_connection(stream, tx, shutdown).await; + }); + } + Err(e) => { + eprintln!("Failed to accept: {}", e); + break; + } + } + } + } + } + }); + + Self { + addr, + message_sender: tx, + shutdown, + } + } + + async fn handle_connection( + stream: TcpStream, + tx: broadcast::Sender, + shutdown: CancellationToken, + ) { + let ws_stream = match accept_async(stream).await { + Ok(ws_stream) => ws_stream, + Err(e) => { + eprintln!("Failed to accept websocket: {}", e); + return; + } + }; + + let (mut ws_sender, _) = ws_stream.split(); + + let mut rx = tx.subscribe(); + + loop { + select! { + _ = shutdown.cancelled() => { + break; + } + msg = rx.recv() => { + match msg { + Ok(text) => { + if let Err(e) = ws_sender.send(Message::Text(text.into())).await { + eprintln!("Error sending message: {}", e); + break; + } + } + Err(_) => { + break; + } + } + } + } + } + } + + async fn send_message( + &self, + msg: &str, + ) -> Result> { + self.message_sender.send(msg.to_string()) + } + + async fn shutdown(self) { + self.shutdown.cancel(); + } + + fn uri(&self) -> Uri { + format!("ws://{}", self.addr) + .parse() + .expect("Failed to parse URI") + } + } + + #[tokio::test] + async fn test_multiple_subscribers_single_listener() { + // Create two mock servers + let server1 = MockServer::new().await; + let server2 = MockServer::new().await; + + // Create a receiver for the messages + let received_messages = Arc::new(Mutex::new(Vec::new())); + let received_clone = received_messages.clone(); + + // Create a listener function that will be shared by both subscribers + let listener = move |data: String| { + if let Ok(mut messages) = received_clone.lock() { + messages.push(data); + } + }; + + // Create metrics + let metrics = Arc::new(Metrics::default()); + + // Create cancellation token + let token = CancellationToken::new(); + let token_clone1 = token.clone(); + let token_clone2 = token.clone(); + + // Create and run the first subscriber + let uri1 = server1.uri(); + let listener_clone1 = listener.clone(); + let metrics_clone1 = metrics.clone(); + + let mut subscriber1 = + WebsocketSubscriber::new(uri1.clone(), listener_clone1, 5, metrics_clone1); + + // Create and run the second subscriber + let uri2 = server2.uri(); + let listener_clone2 = listener.clone(); + let metrics_clone2 = metrics.clone(); + + let mut subscriber2 = + WebsocketSubscriber::new(uri2.clone(), listener_clone2, 5, metrics_clone2); + + // Spawn tasks for subscribers + let task1 = tokio::spawn(async move { + subscriber1.run(token_clone1).await; + }); + + let task2 = tokio::spawn(async move { + subscriber2.run(token_clone2).await; + }); + + // Wait for connections to establish + sleep(Duration::from_millis(500)).await; + + // Send different messages from each server + let _ = server1.send_message("Message from server 1").await; + let _ = server2.send_message("Message from server 2").await; + + // Wait for messages to be processed + sleep(Duration::from_millis(500)).await; + + // Send more messages to ensure continuous operation + let _ = server1.send_message("Another message from server 1").await; + let _ = server2.send_message("Another message from server 2").await; + + // Wait for messages to be processed + sleep(Duration::from_millis(500)).await; + + // Cancel the token to shut down subscribers + token.cancel(); + + // Wait for tasks to complete + let _ = timeout(Duration::from_secs(1), task1).await; + let _ = timeout(Duration::from_secs(1), task2).await; + + // Shutdown the mock servers + server1.shutdown().await; + server2.shutdown().await; + + // Verify that messages were received + let messages = match received_messages.lock() { + Ok(guard) => guard, + Err(poisoned) => poisoned.into_inner(), + }; + + assert_eq!(messages.len(), 4); + + // Check that we received messages from both servers + assert!(messages.contains(&"Message from server 1".to_string())); + assert!(messages.contains(&"Message from server 2".to_string())); + assert!(messages.contains(&"Another message from server 1".to_string())); + assert!(messages.contains(&"Another message from server 2".to_string())); + + assert!(messages.len() > 0); + } +}