Rate Limiting with HTTP #1415
Replies: 2 comments 4 replies
-
Hey, It really depends how you instantiate the rate middleware and you could decide whether to use enable it globally on the server or per connection. let global_cnt = Arc::new(AtomicUsize::new(0));
let rpc_middleware = RpcServiceBuilder::new()
// This state is created per connection.
.layer_fn(|service| CallsPerConn { service, count: Default::default() })
// This state is shared by all connections.
.layer_fn(move |service| GlobalCalls { service, count: global_cnt.clone() });
// start server.... So you could enable two rate limiting middlewares:
However, if you want to rate-limit/blacklist certain peer/ip addr then you need to something more complicated than you can use low-level API and utilize jsonrpsee as a service. |
Beta Was this translation helpful? Give feedback.
-
Sorry, the jsonrpsee rpc middleware is not "tower compatible" it's a separate trait and currently we have no way to use tower layers https://docs.rs/jsonrpsee-server/latest/src/jsonrpsee_server/middleware/rpc/mod.rs.html#40-l#50. If you really just want to do http rate limiting you shouldn't need the RpcMiddleware at all and just the http middleware, which version of jsonrpsee and tower-governor are you using? Just double check that both are using the same version of tower and http.... |
Beta Was this translation helpful? Give feedback.
-
I have followed the jsonrpsee guide for adding rate limiting middleware to my server. When I make an API call to it from postman they are all succeeding despite allowing only 1 call every 15 seconds. I can see from my logs that it is not storing the
RateLimit
state between calls. But when I try the example server they are using a websocket cliet (WsClientBuilder
). This successfully returns the rate limit error. I assume this is because jsonrpsee is maintaining theRateLimit
state under the hood because the WS connection is still alive between individual calls vs HTTP requests are individual connections.So my question is how is one supposed to rate limit a specific IP for JSON-RPC requests over HTTP?
Beta Was this translation helpful? Give feedback.
All reactions