@@ -29,7 +29,9 @@ import (
2929const TxSearchPerPage = 10
3030
3131const (
32- // DB Concurrency Read Limit
32+ // MaxDBReadConcurrency is the default DB read concurrency limit.
33+ // In practice, this is aligned with WorkerPoolSize at runtime.
34+ // This constant is only used as a fallback for metrics initialization.
3335 MaxDBReadConcurrency = 16
3436
3537 // Default request limits (used as fallback values)
@@ -530,7 +532,9 @@ func (a *FilterAPI) GetFilterLogs(
530532}
531533
532534func (a * FilterAPI ) GetLogs (ctx context.Context , crit filters.FilterCriteria ) (res []* ethtypes.Log , err error ) {
533- defer recordMetrics (fmt .Sprintf ("%s_getLogs" , a .namespace ), a .connectionType , time .Now ())
535+ startTime := time .Now ()
536+ defer recordMetrics (fmt .Sprintf ("%s_getLogs" , a .namespace ), a .connectionType , startTime )
537+
534538 // Calculate block range
535539 latest := a .logFetcher .ctxProvider (LatestCtxHeight ).BlockHeight ()
536540 begin , end := latest , latest
@@ -546,16 +550,44 @@ func (a *FilterAPI) GetLogs(ctx context.Context, crit filters.FilterCriteria) (r
546550
547551 blockRange := end - begin + 1
548552
553+ // Record metrics for eth_getLogs
554+ defer func () {
555+ GetGlobalMetrics ().RecordGetLogsRequest (blockRange , time .Since (startTime ), startTime , err )
556+ }()
557+
549558 // Use config value instead of hardcoded constant
550559 if blockRange > a .filterConfig .maxBlock {
551560 return nil , fmt .Errorf ("block range too large (%d), maximum allowed is %d blocks" , blockRange , a .filterConfig .maxBlock )
552561 }
553562
563+ // Early rejection for pruned blocks - avoid wasting resources on blocks that don't exist
564+ earliest := a .logFetcher .earliestVersion ()
565+ if earliest > 0 && begin < earliest {
566+ return nil , fmt .Errorf ("requested block range [%d, %d] includes pruned blocks, earliest available block is %d" , begin , end , earliest )
567+ }
568+
554569 // Only apply rate limiting for large queries (> RPSLimitThreshold blocks)
555570 if blockRange > RPSLimitThreshold && ! a .globalRPSLimiter .Allow () {
556571 return nil , fmt .Errorf ("log query rate limit exceeded for large queries, please try again later" )
557572 }
558573
574+ // Backpressure: early rejection based on system load
575+ m := GetGlobalMetrics ()
576+
577+ // Check 1: Too many pending tasks (queue backlog)
578+ pending := m .TasksSubmitted .Load () - m .TasksCompleted .Load ()
579+ maxPending := int64 (float64 (m .QueueCapacity ) * 0.8 ) // 80% threshold
580+ if pending > maxPending {
581+ return nil , fmt .Errorf ("server too busy, rejecting new request (pending: %d, threshold: %d)" , pending , maxPending )
582+ }
583+
584+ // Check 2: I/O saturated (semaphore exhausted)
585+ semInUse := m .DBSemaphoreAcquired .Load ()
586+ semCapacity := m .DBSemaphoreCapacity
587+ if semCapacity > 0 && float64 (semInUse )/ float64 (semCapacity ) >= 0.8 {
588+ return nil , fmt .Errorf ("server I/O saturated, rejecting new request (semaphore: %d/%d in use)" , semInUse , semCapacity )
589+ }
590+
559591 logs , _ , err := a .logFetcher .GetLogsByFilters (ctx , crit , 0 )
560592 if err != nil {
561593 return nil , err
@@ -741,7 +773,7 @@ func (f *LogFetcher) GetLogsByFilters(ctx context.Context, crit filters.FilterCr
741773 batch := blockBatch
742774 wg .Add (1 )
743775
744- if err := runner .Submit (func () { processBatch (batch ) }); err != nil {
776+ if err := runner .SubmitWithMetrics (func () { processBatch (batch ) }); err != nil {
745777 wg .Done ()
746778 submitError = fmt .Errorf ("system overloaded, please reduce request frequency: %w" , err )
747779 break
@@ -757,7 +789,7 @@ func (f *LogFetcher) GetLogsByFilters(ctx context.Context, crit filters.FilterCr
757789 // Process remaining blocks
758790 if len (blockBatch ) > 0 {
759791 wg .Add (1 )
760- if err := runner .Submit (func () { processBatch (blockBatch ) }); err != nil {
792+ if err := runner .SubmitWithMetrics (func () { processBatch (blockBatch ) }); err != nil {
761793 wg .Done ()
762794 return nil , 0 , fmt .Errorf ("system overloaded, please reduce request frequency: %w" , err )
763795 }
@@ -977,7 +1009,7 @@ func (f *LogFetcher) fetchBlocksByCrit(ctx context.Context, crit filters.FilterC
9771009 }
9781010
9791011 wg .Add (1 )
980- if err := runner .Submit (func (start , endHeight int64 ) func () {
1012+ if err := runner .SubmitWithMetrics (func (start , endHeight int64 ) func () {
9811013 return func () {
9821014 defer wg .Done ()
9831015 f .processBatch (ctx , start , endHeight , crit , bloomIndexes , res , errChan )
@@ -1014,6 +1046,9 @@ func (f *LogFetcher) processBatch(ctx context.Context, start, end int64, crit fi
10141046 defer func () {
10151047 metrics .IncrementRpcRequestCounter ("num_blocks_fetched" , "blocks" , true )
10161048 }()
1049+
1050+ wpMetrics := GetGlobalMetrics ()
1051+
10171052 for height := start ; height <= end ; height ++ {
10181053 if height == 0 {
10191054 continue
@@ -1026,11 +1061,15 @@ func (f *LogFetcher) processBatch(ctx context.Context, start, end int64, crit fi
10261061 }
10271062
10281063 // Block cache miss, acquire semaphore for I/O operations
1064+ semWaitStart := time .Now ()
10291065 f .dbReadSemaphore <- struct {}{}
1066+ wpMetrics .RecordDBSemaphoreWait (time .Since (semWaitStart ))
1067+ wpMetrics .RecordDBSemaphoreAcquire ()
10301068
10311069 // Re-check cache after acquiring semaphore, in case another worker cached it.
10321070 if cachedEntry , found := f .globalBlockCache .Get (height ); found {
10331071 <- f .dbReadSemaphore
1072+ wpMetrics .RecordDBSemaphoreRelease ()
10341073 res <- cachedEntry .Block
10351074 continue
10361075 }
@@ -1046,6 +1085,7 @@ func (f *LogFetcher) processBatch(ctx context.Context, start, end int64, crit fi
10461085 // skip the bloom pre-filter instead of short-circuiting the block.
10471086 if blockBloom != (ethtypes.Bloom {}) && ! MatchFilters (blockBloom , bloomIndexes ) {
10481087 <- f .dbReadSemaphore
1088+ wpMetrics .RecordDBSemaphoreRelease ()
10491089 continue // skip the block if bloom filter does not match
10501090 }
10511091 }
@@ -1058,6 +1098,7 @@ func (f *LogFetcher) processBatch(ctx context.Context, start, end int64, crit fi
10581098 default :
10591099 }
10601100 <- f .dbReadSemaphore
1101+ wpMetrics .RecordDBSemaphoreRelease ()
10611102 continue
10621103 }
10631104
@@ -1068,6 +1109,7 @@ func (f *LogFetcher) processBatch(ctx context.Context, start, end int64, crit fi
10681109 fillMissingFields (entry , block , blockBloom )
10691110 }
10701111 <- f .dbReadSemaphore
1112+ wpMetrics .RecordDBSemaphoreRelease ()
10711113 res <- block
10721114 }
10731115}
0 commit comments