@@ -32,6 +32,7 @@ pub use methods::{
3232} ;
3333pub ( crate ) use outbound:: OutboundRequest ;
3434pub use protocol:: { max_rpc_size, Protocol , RPCError } ;
35+ use crate :: rpc:: rate_limiter:: RequestSizeLimiter ;
3536
3637use self :: config:: { InboundRateLimiterConfig , OutboundRateLimiterConfig } ;
3738use self :: protocol:: RPCProtocol ;
@@ -122,6 +123,8 @@ pub struct RPC<Id: ReqId, E: EthSpec> {
122123 limiter : Option < RateLimiter > ,
123124 /// Rate limiter for our responses. This is shared with RPCHandlers.
124125 response_limiter : Option < Arc < Mutex < RateLimiter > > > ,
126+ ///
127+ request_size_limiter : Option < RequestSizeLimiter > ,
125128 /// Rate limiter for our own requests.
126129 self_limiter : Option < SelfRateLimiter < Id , E > > ,
127130 /// Queue of events to be processed.
@@ -152,21 +155,27 @@ impl<Id: ReqId, E: EthSpec> RPC<Id, E> {
152155 // });
153156 let inbound_limiter = None ; // TODO
154157
155- let response_limiter = inbound_rate_limiter_config. map ( |config| {
158+ let response_limiter = inbound_rate_limiter_config. clone ( ) . map ( |config| {
156159 debug ! ( log, "Using response rate limiting params" ; "config" => ?config) ;
157160 Arc :: new ( Mutex :: new (
158161 RateLimiter :: new_with_config ( config. 0 )
159162 . expect ( "Inbound limiter configuration parameters are valid" ) ,
160163 ) )
161164 } ) ;
162165
166+ let request_size_limiter = inbound_rate_limiter_config. map ( |config| {
167+ RequestSizeLimiter :: new_with_config ( config. 0 )
168+ . expect ( "Inbound limiter configuration parameters are valid" )
169+ } ) ;
170+
163171 let self_limiter = outbound_rate_limiter_config. map ( |config| {
164172 SelfRateLimiter :: new ( config, log. clone ( ) ) . expect ( "Configuration parameters are valid" )
165173 } ) ;
166174
167175 RPC {
168176 limiter : inbound_limiter,
169177 response_limiter,
178+ request_size_limiter,
170179 self_limiter,
171180 events : Vec :: new ( ) ,
172181 fork_context,
@@ -315,58 +324,95 @@ where
315324 ) {
316325 match event {
317326 HandlerEvent :: Ok ( RPCReceived :: Request ( ref id, ref req) ) => {
318- if let Some ( limiter) = self . limiter . as_mut ( ) {
319- // check if the request is conformant to the quota
320- match limiter. allows ( & peer_id, req) {
321- Ok ( ( ) ) => {
322- // send the event to the user
323- self . events . push ( ToSwarm :: GenerateEvent ( RPCMessage {
324- peer_id,
325- conn_id,
326- event,
327- } ) )
328- }
329- Err ( RateLimitedErr :: TooLarge ) => {
330- // we set the batch sizes, so this is a coding/config err for most protocols
331- let protocol = req. versioned_protocol ( ) . protocol ( ) ;
332- if matches ! (
327+ if let Some ( limiter) = self . request_size_limiter . as_ref ( ) {
328+ println ! ( "req limiter" ) ;
329+ // if let Some(limiter) = self.limiter.as_mut() {
330+ // Check if the request is conformant to the quota
331+ if limiter. allows ( req) {
332+ println ! ( "req limiter allowed" ) ;
333+ // Send the event to the user
334+ self . events . push ( ToSwarm :: GenerateEvent ( RPCMessage {
335+ peer_id,
336+ conn_id,
337+ event,
338+ } ) )
339+ } else {
340+ println ! ( "req limiter NOT allowed" ) ;
341+ // We set the batch sizes, so this is a coding/config err for most protocols
342+ let protocol = req. versioned_protocol ( ) . protocol ( ) ;
343+ if matches ! (
333344 protocol,
334345 Protocol :: BlocksByRange
335346 | Protocol :: BlobsByRange
336347 | Protocol :: BlocksByRoot
337348 | Protocol :: BlobsByRoot
338349 ) {
339- debug ! ( self . log, "Request too large to process" ; "request" => %req, "protocol" => %protocol) ;
340- } else {
341- // Other protocols shouldn't be sending large messages, we should flag the peer kind
342- crit ! ( self . log, "Request size too large to ever be processed" ; "protocol" => %protocol) ;
343- }
344- // send an error code to the peer.
345- // the handler upon receiving the error code will send it back to the behaviour
346- self . send_response (
347- peer_id,
348- ( conn_id, * id) ,
349- RPCCodedResponse :: Error (
350- RPCResponseErrorCode :: RateLimited ,
351- "Rate limited. Request too large" . into ( ) ,
352- ) ,
353- ) ;
354- }
355- Err ( RateLimitedErr :: TooSoon ( wait_time) ) => {
356- debug ! ( self . log, "Request exceeds the rate limit" ;
357- "request" => %req, "peer_id" => %peer_id, "wait_time_ms" => wait_time. as_millis( ) ) ;
358- // send an error code to the peer.
359- // the handler upon receiving the error code will send it back to the behaviour
360- self . send_response (
361- peer_id,
362- ( conn_id, * id) ,
363- RPCCodedResponse :: Error (
364- RPCResponseErrorCode :: RateLimited ,
365- format ! ( "Wait {:?}" , wait_time) . into ( ) ,
366- ) ,
367- ) ;
350+ debug ! ( self . log, "Request too large to process" ; "request" => %req, "protocol" => %protocol) ;
351+ } else {
352+ // Other protocols shouldn't be sending large messages, we should flag the peer kind
353+ crit ! ( self . log, "Request size too large to ever be processed" ; "protocol" => %protocol) ;
368354 }
355+ // Send an error code to the peer.
356+ // The handler upon receiving the error code will send it back to the behaviour
357+ self . send_response (
358+ peer_id,
359+ ( conn_id, * id) ,
360+ RPCCodedResponse :: Error (
361+ RPCResponseErrorCode :: RateLimited ,
362+ "Rate limited. Request too large" . into ( ) ,
363+ ) ,
364+ ) ;
369365 }
366+ // match limiter.allows(&peer_id, req) {
367+ // Ok(()) => {
368+ // // send the event to the user
369+ // self.events.push(ToSwarm::GenerateEvent(RPCMessage {
370+ // peer_id,
371+ // conn_id,
372+ // event,
373+ // }))
374+ // }
375+ // Err(RateLimitedErr::TooLarge) => {
376+ // // we set the batch sizes, so this is a coding/config err for most protocols
377+ // let protocol = req.versioned_protocol().protocol();
378+ // if matches!(
379+ // protocol,
380+ // Protocol::BlocksByRange
381+ // | Protocol::BlobsByRange
382+ // | Protocol::BlocksByRoot
383+ // | Protocol::BlobsByRoot
384+ // ) {
385+ // debug!(self.log, "Request too large to process"; "request" => %req, "protocol" => %protocol);
386+ // } else {
387+ // // Other protocols shouldn't be sending large messages, we should flag the peer kind
388+ // crit!(self.log, "Request size too large to ever be processed"; "protocol" => %protocol);
389+ // }
390+ // // send an error code to the peer.
391+ // // the handler upon receiving the error code will send it back to the behaviour
392+ // self.send_response(
393+ // peer_id,
394+ // (conn_id, *id),
395+ // RPCCodedResponse::Error(
396+ // RPCResponseErrorCode::RateLimited,
397+ // "Rate limited. Request too large".into(),
398+ // ),
399+ // );
400+ // }
401+ // Err(RateLimitedErr::TooSoon(wait_time)) => {
402+ // debug!(self.log, "Request exceeds the rate limit";
403+ // "request" => %req, "peer_id" => %peer_id, "wait_time_ms" => wait_time.as_millis());
404+ // // send an error code to the peer.
405+ // // the handler upon receiving the error code will send it back to the behaviour
406+ // self.send_response(
407+ // peer_id,
408+ // (conn_id, *id),
409+ // RPCCodedResponse::Error(
410+ // RPCResponseErrorCode::RateLimited,
411+ // format!("Wait {:?}", wait_time).into(),
412+ // ),
413+ // );
414+ // }
415+ // }
370416 } else {
371417 // No rate limiting, send the event to the user
372418 self . events . push ( ToSwarm :: GenerateEvent ( RPCMessage {
0 commit comments