Skip to content

Commit 35d2260

Browse files
authored
Merge pull request #763 from onflow/mpeter/rate-limited-requests-metric
Add metric to count the rate-limited requests
2 parents 5b8d549 + 8b90e95 commit 35d2260

File tree

8 files changed

+156
-111
lines changed

8 files changed

+156
-111
lines changed

api/api.go

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/onflow/go-ethereum/rlp"
1414
"github.com/onflow/go-ethereum/rpc"
1515
"github.com/rs/zerolog"
16-
"github.com/sethvargo/go-limiter"
1716

1817
evmTypes "github.com/onflow/flow-go/fvm/evm/types"
1918

@@ -87,7 +86,7 @@ type BlockChainAPI struct {
8786
transactions storage.TransactionIndexer
8887
receipts storage.ReceiptIndexer
8988
indexingResumedHeight uint64
90-
limiter limiter.Store
89+
rateLimiter RateLimiter
9190
collector metrics.Collector
9291
}
9392

@@ -98,7 +97,7 @@ func NewBlockChainAPI(
9897
blocks storage.BlockIndexer,
9998
transactions storage.TransactionIndexer,
10099
receipts storage.ReceiptIndexer,
101-
ratelimiter limiter.Store,
100+
rateLimiter RateLimiter,
102101
collector metrics.Collector,
103102
indexingResumedHeight uint64,
104103
) *BlockChainAPI {
@@ -110,14 +109,14 @@ func NewBlockChainAPI(
110109
transactions: transactions,
111110
receipts: receipts,
112111
indexingResumedHeight: indexingResumedHeight,
113-
limiter: ratelimiter,
112+
rateLimiter: rateLimiter,
114113
collector: collector,
115114
}
116115
}
117116

118117
// BlockNumber returns the block number of the chain head.
119118
func (b *BlockChainAPI) BlockNumber(ctx context.Context) (hexutil.Uint64, error) {
120-
if err := rateLimit(ctx, b.limiter, b.logger); err != nil {
119+
if err := b.rateLimiter.Apply(ctx, "BlockNumber"); err != nil {
121120
return 0, err
122121
}
123122

@@ -136,7 +135,7 @@ func (b *BlockChainAPI) BlockNumber(ctx context.Context) (hexutil.Uint64, error)
136135
// - currentBlock: block number this node is currently importing
137136
// - highestBlock: block number of the highest block header this node has received from peers
138137
func (b *BlockChainAPI) Syncing(ctx context.Context) (interface{}, error) {
139-
if err := rateLimit(ctx, b.limiter, b.logger); err != nil {
138+
if err := b.rateLimiter.Apply(ctx, "Syncing"); err != nil {
140139
return nil, err
141140
}
142141

@@ -176,7 +175,7 @@ func (b *BlockChainAPI) SendRawTransaction(
176175
Str("input", input.String()).
177176
Logger()
178177

179-
if err := rateLimit(ctx, b.limiter, l); err != nil {
178+
if err := b.rateLimiter.Apply(ctx, "SendRawTransaction"); err != nil {
180179
return common.Hash{}, err
181180
}
182181

@@ -201,7 +200,7 @@ func (b *BlockChainAPI) GetBalance(
201200
Str("address", address.String()).
202201
Logger()
203202

204-
if err := rateLimit(ctx, b.limiter, l); err != nil {
203+
if err := b.rateLimiter.Apply(ctx, "GetBalance"); err != nil {
205204
return nil, err
206205
}
207206

@@ -228,7 +227,7 @@ func (b *BlockChainAPI) GetTransactionByHash(
228227
Str("hash", hash.String()).
229228
Logger()
230229

231-
if err := rateLimit(ctx, b.limiter, l); err != nil {
230+
if err := b.rateLimiter.Apply(ctx, "GetTransactionByHash"); err != nil {
232231
return nil, err
233232
}
234233

@@ -257,7 +256,7 @@ func (b *BlockChainAPI) GetTransactionByBlockHashAndIndex(
257256
Str("index", index.String()).
258257
Logger()
259258

260-
if err := rateLimit(ctx, b.limiter, l); err != nil {
259+
if err := b.rateLimiter.Apply(ctx, "GetTransactionByBlockHashAndIndex"); err != nil {
261260
return nil, err
262261
}
263262

@@ -292,7 +291,7 @@ func (b *BlockChainAPI) GetTransactionByBlockNumberAndIndex(
292291
Str("index", index.String()).
293292
Logger()
294293

295-
if err := rateLimit(ctx, b.limiter, l); err != nil {
294+
if err := b.rateLimiter.Apply(ctx, "GetTransactionByBlockNumberAndIndex"); err != nil {
296295
return nil, err
297296
}
298297

@@ -332,7 +331,7 @@ func (b *BlockChainAPI) GetTransactionReceipt(
332331
Str("hash", hash.String()).
333332
Logger()
334333

335-
if err := rateLimit(ctx, b.limiter, l); err != nil {
334+
if err := b.rateLimiter.Apply(ctx, "GetTransactionReceipt"); err != nil {
336335
return nil, err
337336
}
338337

@@ -366,7 +365,7 @@ func (b *BlockChainAPI) GetBlockByHash(
366365
Str("hash", hash.String()).
367366
Logger()
368367

369-
if err := rateLimit(ctx, b.limiter, l); err != nil {
368+
if err := b.rateLimiter.Apply(ctx, "GetBlockByHash"); err != nil {
370369
return nil, err
371370
}
372371

@@ -400,7 +399,7 @@ func (b *BlockChainAPI) GetBlockByNumber(
400399
Str("blockNumber", blockNumber.String()).
401400
Logger()
402401

403-
if err := rateLimit(ctx, b.limiter, l); err != nil {
402+
if err := b.rateLimiter.Apply(ctx, "GetBlockByNumber"); err != nil {
404403
return nil, err
405404
}
406405

@@ -437,7 +436,7 @@ func (b *BlockChainAPI) GetBlockReceipts(
437436
Str("hash", blockNumberOrHash.String()).
438437
Logger()
439438

440-
if err := rateLimit(ctx, b.limiter, l); err != nil {
439+
if err := b.rateLimiter.Apply(ctx, "GetBlockReceipts"); err != nil {
441440
return nil, err
442441
}
443442

@@ -483,7 +482,7 @@ func (b *BlockChainAPI) GetBlockTransactionCountByHash(
483482
Str("hash", blockHash.String()).
484483
Logger()
485484

486-
if err := rateLimit(ctx, b.limiter, l); err != nil {
485+
if err := b.rateLimiter.Apply(ctx, "GetBlockTransactionCountByHash"); err != nil {
487486
return nil, err
488487
}
489488

@@ -507,7 +506,7 @@ func (b *BlockChainAPI) GetBlockTransactionCountByNumber(
507506
Str("number", blockNumber.String()).
508507
Logger()
509508

510-
if err := rateLimit(ctx, b.limiter, l); err != nil {
509+
if err := b.rateLimiter.Apply(ctx, "GetBlockTransactionCountByNumber"); err != nil {
511510
return nil, err
512511
}
513512

@@ -544,7 +543,7 @@ func (b *BlockChainAPI) Call(
544543
Str("args", fmt.Sprintf("%v", args)).
545544
Logger()
546545

547-
if err := rateLimit(ctx, b.limiter, l); err != nil {
546+
if err := b.rateLimiter.Apply(ctx, "Call"); err != nil {
548547
return nil, err
549548
}
550549

@@ -592,7 +591,7 @@ func (b *BlockChainAPI) GetLogs(
592591
Str("criteria", fmt.Sprintf("%v", criteria)).
593592
Logger()
594593

595-
if err := rateLimit(ctx, b.limiter, l); err != nil {
594+
if err := b.rateLimiter.Apply(ctx, "GetLogs"); err != nil {
596595
return nil, err
597596
}
598597

@@ -681,7 +680,7 @@ func (b *BlockChainAPI) GetTransactionCount(
681680
Str("address", address.String()).
682681
Logger()
683682

684-
if err := rateLimit(ctx, b.limiter, l); err != nil {
683+
if err := b.rateLimiter.Apply(ctx, "GetTransactionCount"); err != nil {
685684
return nil, err
686685
}
687686

@@ -714,7 +713,7 @@ func (b *BlockChainAPI) EstimateGas(
714713
Str("args", fmt.Sprintf("%v", args)).
715714
Logger()
716715

717-
if err := rateLimit(ctx, b.limiter, l); err != nil {
716+
if err := b.rateLimiter.Apply(ctx, "EstimateGas"); err != nil {
718717
return 0, err
719718
}
720719

@@ -764,7 +763,7 @@ func (b *BlockChainAPI) GetCode(
764763
Str("address", address.String()).
765764
Logger()
766765

767-
if err := rateLimit(ctx, b.limiter, l); err != nil {
766+
if err := b.rateLimiter.Apply(ctx, "GetCode"); err != nil {
768767
return nil, err
769768
}
770769

@@ -880,7 +879,7 @@ func (b *BlockChainAPI) GetStorageAt(
880879
Str("address", address.String()).
881880
Logger()
882881

883-
if err := rateLimit(ctx, b.limiter, l); err != nil {
882+
if err := b.rateLimiter.Apply(ctx, "GetStorageAt"); err != nil {
884883
return nil, err
885884
}
886885

api/debug.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@ import (
1515
"github.com/onflow/go-ethereum/eth/tracers/logger"
1616
"github.com/onflow/go-ethereum/rpc"
1717
"github.com/rs/zerolog"
18-
"github.com/sethvargo/go-limiter"
1918

2019
"github.com/onflow/flow-evm-gateway/config"
2120
ethTypes "github.com/onflow/flow-evm-gateway/eth/types"
22-
"github.com/onflow/flow-evm-gateway/metrics"
2321
"github.com/onflow/flow-evm-gateway/models"
2422
errs "github.com/onflow/flow-evm-gateway/models/errors"
2523
"github.com/onflow/flow-evm-gateway/services/evm"
@@ -53,8 +51,7 @@ type DebugAPI struct {
5351
receipts storage.ReceiptIndexer
5452
client *requester.CrossSporkClient
5553
config config.Config
56-
collector metrics.Collector
57-
limiter limiter.Store
54+
rateLimiter RateLimiter
5855
}
5956

6057
func NewDebugAPI(
@@ -66,8 +63,7 @@ func NewDebugAPI(
6663
client *requester.CrossSporkClient,
6764
config config.Config,
6865
logger zerolog.Logger,
69-
collector metrics.Collector,
70-
limiter limiter.Store,
66+
rateLimiter RateLimiter,
7167
) *DebugAPI {
7268
return &DebugAPI{
7369
registerStore: registerStore,
@@ -78,8 +74,7 @@ func NewDebugAPI(
7874
receipts: receipts,
7975
client: client,
8076
config: config,
81-
collector: collector,
82-
limiter: limiter,
77+
rateLimiter: rateLimiter,
8378
}
8479
}
8580

@@ -89,7 +84,7 @@ func (d *DebugAPI) TraceTransaction(
8984
hash gethCommon.Hash,
9085
config *tracers.TraceConfig,
9186
) (json.RawMessage, error) {
92-
if err := rateLimit(ctx, d.limiter, d.logger); err != nil {
87+
if err := d.rateLimiter.Apply(ctx, "TraceTransaction"); err != nil {
9388
return nil, err
9489
}
9590

@@ -101,7 +96,7 @@ func (d *DebugAPI) TraceBlockByNumber(
10196
number rpc.BlockNumber,
10297
config *tracers.TraceConfig,
10398
) ([]*txTraceResult, error) {
104-
if err := rateLimit(ctx, d.limiter, d.logger); err != nil {
99+
if err := d.rateLimiter.Apply(ctx, "TraceBlockByNumber"); err != nil {
105100
return nil, err
106101
}
107102

@@ -118,7 +113,7 @@ func (d *DebugAPI) TraceBlockByHash(
118113
hash gethCommon.Hash,
119114
config *tracers.TraceConfig,
120115
) ([]*txTraceResult, error) {
121-
if err := rateLimit(ctx, d.limiter, d.logger); err != nil {
116+
if err := d.rateLimiter.Apply(ctx, "TraceBlockByHash"); err != nil {
122117
return nil, err
123118
}
124119

@@ -136,7 +131,7 @@ func (d *DebugAPI) TraceCall(
136131
blockNrOrHash rpc.BlockNumberOrHash,
137132
config *tracers.TraceCallConfig,
138133
) (interface{}, error) {
139-
if err := rateLimit(ctx, d.limiter, d.logger); err != nil {
134+
if err := d.rateLimiter.Apply(ctx, "TraceCall"); err != nil {
140135
return nil, err
141136
}
142137

@@ -259,7 +254,7 @@ func (d *DebugAPI) FlowHeightByBlock(
259254
ctx context.Context,
260255
blockNrOrHash rpc.BlockNumberOrHash,
261256
) (uint64, error) {
262-
if err := rateLimit(ctx, d.limiter, d.logger); err != nil {
257+
if err := d.rateLimiter.Apply(ctx, "FlowHeightByBlock"); err != nil {
263258
return 0, err
264259
}
265260

api/pull.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/onflow/go-ethereum/eth/filters"
1313
"github.com/onflow/go-ethereum/rpc"
1414
"github.com/rs/zerolog"
15-
"github.com/sethvargo/go-limiter"
1615

1716
"github.com/onflow/flow-evm-gateway/config"
1817
ethTypes "github.com/onflow/flow-evm-gateway/eth/types"
@@ -135,7 +134,7 @@ type PullAPI struct {
135134
receipts storage.ReceiptIndexer
136135
filters map[rpc.ID]filter
137136
mux sync.Mutex
138-
ratelimiter limiter.Store
137+
rateLimiter RateLimiter
139138
}
140139

141140
func NewPullAPI(
@@ -144,7 +143,7 @@ func NewPullAPI(
144143
blocks storage.BlockIndexer,
145144
transactions storage.TransactionIndexer,
146145
receipts storage.ReceiptIndexer,
147-
ratelimiter limiter.Store,
146+
rateLimiter RateLimiter,
148147
) *PullAPI {
149148
api := &PullAPI{
150149
logger: logger,
@@ -153,7 +152,7 @@ func NewPullAPI(
153152
transactions: transactions,
154153
receipts: receipts,
155154
filters: make(map[rpc.ID]filter),
156-
ratelimiter: ratelimiter,
155+
rateLimiter: rateLimiter,
157156
}
158157

159158
go api.filterExpiryChecker()
@@ -170,7 +169,7 @@ func (api *PullAPI) NewPendingTransactionFilter(
170169
ctx context.Context,
171170
fullTx *bool,
172171
) (rpc.ID, error) {
173-
if err := rateLimit(ctx, api.ratelimiter, api.logger); err != nil {
172+
if err := api.rateLimiter.Apply(ctx, "NewPendingTransactionFilter"); err != nil {
174173
return "", err
175174
}
176175

@@ -198,7 +197,7 @@ func (api *PullAPI) NewPendingTransactionFilter(
198197
// NewBlockFilter creates a filter that fetches blocks that are imported into the chain.
199198
// It is part of the filter package since polling goes with eth_getFilterChanges.
200199
func (api *PullAPI) NewBlockFilter(ctx context.Context) (rpc.ID, error) {
201-
if err := rateLimit(ctx, api.ratelimiter, api.logger); err != nil {
200+
if err := api.rateLimiter.Apply(ctx, "NewBlockFilter"); err != nil {
202201
return "", err
203202
}
204203

@@ -247,7 +246,7 @@ func (api *PullAPI) uninstallFilter(id rpc.ID) bool {
247246
//
248247
// In case "fromBlock" > "toBlock" an error is returned.
249248
func (api *PullAPI) NewFilter(ctx context.Context, criteria filters.FilterCriteria) (rpc.ID, error) {
250-
if err := rateLimit(ctx, api.ratelimiter, api.logger); err != nil {
249+
if err := api.rateLimiter.Apply(ctx, "NewFilter"); err != nil {
251250
return "", err
252251
}
253252

@@ -291,7 +290,7 @@ func (api *PullAPI) GetFilterLogs(
291290
ctx context.Context,
292291
id rpc.ID,
293292
) ([]*gethTypes.Log, error) {
294-
if err := rateLimit(ctx, api.ratelimiter, api.logger); err != nil {
293+
if err := api.rateLimiter.Apply(ctx, "GetFilterLogs"); err != nil {
295294
return nil, err
296295
}
297296

@@ -341,7 +340,7 @@ func (api *PullAPI) GetFilterLogs(
341340
// For pending transaction and block filters the result is []common.Hash.
342341
// (pending)Log filters return []Log.
343342
func (api *PullAPI) GetFilterChanges(ctx context.Context, id rpc.ID) (any, error) {
344-
if err := rateLimit(ctx, api.ratelimiter, api.logger); err != nil {
343+
if err := api.rateLimiter.Apply(ctx, "GetFilterChanges"); err != nil {
345344
return nil, err
346345
}
347346

0 commit comments

Comments
 (0)