docs, code simplification, fix potential divide-by-zero

This commit is contained in:
Jared Wasinger 2025-04-15 18:16:48 +08:00
parent 14908c56be
commit b777a5c8d6

View file

@ -49,9 +49,13 @@ type resultStore struct {
// keep track of the total non-blob gas used in the headers we are scheduling for retrieval. // keep track of the total non-blob gas used in the headers we are scheduling for retrieval.
// when we exceed a threshold, we will throttle preventing additional requests until // when we exceed a threshold, we will throttle preventing additional requests until
// some current ones have completed. // some current ones have completed.
// cumulative non-blob gas used of all scheduled blocks. used to project worst-case
// block size of what we expect to be delivered, and becomes a signal to throttle
// if the estimated total size of all pending blocks exceeds a threshold.
itemsGasUsed uint64 itemsGasUsed uint64
// count of all scheduled items (including completes that haven't yet been purged) // count of all scheduled blocks (including completes that haven't yet been purged)
itemsCount int itemsCount int
lock sync.RWMutex lock sync.RWMutex
@ -83,7 +87,11 @@ func (r *resultStore) AddFetch(header *types.Header, fastSync bool) (stale, thro
return stale, throttled, item, err return stale, throttled, item, err
} }
if item == nil { if item == nil {
if (r.itemsGasUsed+header.GasUsed)/10 > uint64(blockCacheMemory) { var curEstSize uint64
if r.itemsCount > 0 {
curEstSize = estWorstCaseBlockSize(r.itemsGasUsed/uint64(r.itemsCount)) * uint64(r.itemsCount)
}
if curEstSize+estWorstCaseBlockSize(header.GasUsed) > uint64(blockCacheMemory) {
return false, true, nil, nil return false, true, nil, nil
} }
r.itemsGasUsed += header.GasUsed r.itemsGasUsed += header.GasUsed
@ -130,8 +138,10 @@ func (r *resultStore) throttleThreshold() int {
avgGasUsed = r.itemsGasUsed / uint64(r.itemsCount) avgGasUsed = r.itemsGasUsed / uint64(r.itemsCount)
} }
blockSize := max(estWorstCaseBlockSize(avgGasUsed), headerSize) blockSize := max(estWorstCaseBlockSize(avgGasUsed), headerSize)
throttleThreshold := min(uint64(len(r.items)), uint64(blockCacheMemory)/blockSize+1)
return int(throttleThreshold) // cap the throttle threshold to len(items)
throttleThreshold := min(len(r.items), blockCacheMemory/int(blockSize)+1)
return throttleThreshold
} }
// getFetchResult returns the fetchResult corresponding to the given item, and // getFetchResult returns the fetchResult corresponding to the given item, and