informalsystems / tendermint-rs

Client libraries for Tendermint/CometBFT in Rust!
Apache License 2.0
587 stars 213 forks source link

Allow rate limiting clients #1420

Open SuperFluffy opened 1 month ago

SuperFluffy commented 1 month ago

Description

I need to rate limit our tendermint-rpc HttpClient to not DOS our cometbft node. Unfortunately, tendermint_rpc::client::http::Builder and tendermint_rpc::client::http::HttpClient do not provide ways to set a tower middleware like its RateLimitLayer.

Prior art for how this could be achieved is for example by providing a set_middleware method as is done by jsonrpsee here: https://docs.rs/jsonrpsee-http-client/0.22.5/jsonrpsee_http_client/struct.HttpClientBuilder.html#method.set_http_middleware

Internally, they wrap their hyper client in the provided ServiceBuilder: https://docs.rs/jsonrpsee-http-client/0.22.5/src/jsonrpsee_http_client/transport.rs.html#254

I am going to try and work around it using the techniques described in this reqwest issue tracker here: https://github.com/seanmonstar/reqwest/issues/491 (specifically using tower::ServiceBuilder::service_fn), but that requires a lot of extra boilerplate at the application level.

Definition of "done"

Being able to rate-limit outgoing requests over http. The general solution would be to allow setting arbitrary tower::Services, but rate limiting would be great.

romac commented 1 month ago

Sounds good to me, would gladly accept a PR for this :)