0xPolygonHermez / zkevm-node

Go implementation of a node that operates the Polygon zkEVM Network
Other
528 stars 679 forks source link

Add context in json_rpc method handler #3619

Open Zheaoli opened 4 months ago

Zheaoli commented 4 months ago

Rationale

Why should this feature exist? What are the use-cases?

For now, we don't have any context management in the whole request process. It may cause a whole blocking reaction in the server.

For example, if we call the eth_call method, the database may slow during the query process. If we don't add context management for the request processing make the request cancelable. We may consume the resources in the connection pool very fast. The following request may block forever

sync.runtime_notifyListWait(0xc00053c250, 0x301)
    /usr/local/go/src/runtime/sema.go:527 +0x159
sync.(*Cond).Wait(0x0?)
    /usr/local/go/src/sync/cond.go:70 +0x85
github.com/jackc/puddle.(*Pool).Acquire(0xc000428200, {0x1f93028, 0x2e6ce60})
    /go/pkg/mod/github.com/jackc/puddle@v1.3.0/pool.go:372 +0x265
github.com/jackc/pgx/v4/pgxpool.(*Pool).Acquire(0xc0004c85a0, {0x1f93028, 0x2e6ce60})
    /go/pkg/mod/github.com/jackc/pgx/v4@v4.18.1/pgxpool/pool.go:532 +0x45
github.com/jackc/pgx/v4/pgxpool.(*Pool).BeginTx(0xc0086bc1e0?, {0x1f93028, 0x2e6ce60}, {{0x0, 0x0}, {0x0, 0x0}, {0x0, 0x0}})
    /go/pkg/mod/github.com/jackc/pgx/v4@v4.18.1/pgxpool/pool.go:683 +0x57
github.com/jackc/pgx/v4/pgxpool.(*Pool).Begin(...)
    /go/pkg/mod/github.com/jackc/pgx/v4@v4.18.1/pgxpool/pool.go:675
github.com/0xPolygonHermez/zkevm-node/state.(*State).BeginStateTransaction(0x0?, {0x1f93028?, 0x2e6ce60?})
    /src/state/state.go:67 +0x2f
github.com/0xPolygonHermez/zkevm-node/jsonrpc.(*DBTxManager).NewDbTxScope(0xc0000ab950?, {0x7f8a9b06d510?, 0xc0007dc840?}, 0xc00514eaf8)
    /src/jsonrpc/dbtxmanager.go:24 +0x42
github.com/0xPolygonHermez/zkevm-node/jsonrpc.(*EthEndpoints).GetBlockByNumber(0x4?, 0xfffffffffffffffe, 0x2?, 0x46e353?)
    /src/jsonrpc/endpoints_eth.go:373 +0x8f
reflect.Value.call({0xc0003180e0?, 0xc00054ac78?, 0x175f260?}, {0x1ab920c, 0x4}, {0xc000873140, 0x4, 0x5a2811?})
    /usr/local/go/src/reflect/value.go:596 +0xce7
reflect.Value.Call({0xc0003180e0?, 0xc00054ac78?, 0x10?}, {0xc000873140?, 0xc006bca2d0?, 0x14?})
    /usr/local/go/src/reflect/value.go:380 +0xb9
github.com/0xPolygonHermez/zkevm-node/jsonrpc.(*Handler).Handle(0xc00514f708?, {{{0xc005616270, 0x3}, {0x177f780, 0xc005616278}, {0xc00836f710, 0x14}, {0xc005616390, 0x10, 0x10}}, ...})
    /src/jsonrpc/handler.go:129 +0xae7
github.com/0xPolygonHermez/zkevm-node/jsonrpc.(*Server).handleSingleRequest(0xc00077e2c0, 0xc00bf64000, {0x1f8d440, 0xc0001a2380}, {0xc0007d8200?, 0x0?, 0x411e848000000000?})
    /src/jsonrpc/server.go:320 +0x31d
github.com/0xPolygonHermez/zkevm-node/jsonrpc.(*Server).handle(0xc00026a500?, {0x1f8d440, 0xc0001a2380}, 0xc00bf64000)
    /src/jsonrpc/server.go:258 +0x565
net/http.HandlerFunc.ServeHTTP(0xc1830695bb1a9bb9?, {0x1f8d440?, 0xc0001a2380?}, 0xc000872e30?)
    /usr/local/go/src/net/http/server.go:2136 +0x29
github.com/0xPolygonHermez/zkevm-node/jsonrpc.(*Server).startHTTP.LimitFuncHandler.LimitHandler.func1({0x1f8d440, 0xc0001a2380}, 0x90062d?)
    /go/pkg/mod/github.com/didip/tollbooth/v6@v6.1.2/tollbooth.go:311 +0x133
net/http.HandlerFunc.ServeHTTP(0x10?, {0x1f8d440?, 0xc0001a2380?}, 0xc00049ea2c?)
    /usr/local/go/src/net/http/server.go:2136 +0x29
net/http.(*ServeMux).ServeHTTP(0x4109c5?, {0x1f8d440, 0xc0001a2380}, 0xc00bf64000)
    /usr/local/go/src/net/http/server.go:2514 +0x142
net/http.serverHandler.ServeHTTP({0x1f88160?}, {0x1f8d440?, 0xc0001a2380?}, 0x6?)
    /usr/local/go/src/net/http/server.go:2938 +0x8e
net/http.(*conn).serve(0xc00064c6c0, {0x1f93140, 0xc0003fdec0})
    /usr/local/go/src/net/http/server.go:2009 +0x5f4
created by net/http.(*Server).Serve in goroutine 200
    /usr/local/go/src/net/http/server.go:3086 +0x5cb

About the connection pool part, FYI https://github.com/jackc/puddle/blob/v1.3.0/pool.go#L347-L354

Implementation

We can following the go-eth lib to add conetxt management FYI

https://github.com/ethereum/go-ethereum/blob/master/eth/api_backend.go#L118

https://github.com/ethereum/go-ethereum/blob/master/rpc/http.go#L326-L335

Zheaoli commented 4 months ago

I would like to make PR for this