Skip to content

Commit

Permalink
Merge pull request ethereum#38 from hkalina/jkalina-timeout
Browse files Browse the repository at this point in the history
Add possibility to configure RPC execution time limit
  • Loading branch information
uprendis authored Nov 15, 2022
2 parents ee56663 + 03acb91 commit d862fc5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
15 changes: 14 additions & 1 deletion rpc/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ import (
"github.com/ethereum/go-ethereum/log"
)

var (
executionTimeLimit = 5 * time.Second
)

// SetExecutionTimeLimit sets execution limit for RPC method calls
func SetExecutionTimeLimit(limit time.Duration) {
executionTimeLimit = limit
}

// handler handles JSON-RPC messages. There is one handler per connection. Note that
// handler is not safe for concurrent use. Message handling never blocks indefinitely
// because RPCs are processed on background goroutines launched by handler.
Expand Down Expand Up @@ -334,7 +343,11 @@ func (h *handler) handleCall(cp *callProc, msg *jsonrpcMessage) *jsonrpcMessage
return msg.errorResponse(&invalidParamsError{err.Error()})
}
start := time.Now()
answer := h.runMethod(cp.ctx, msg, callb, args)

ctx, cancel := context.WithTimeout(cp.ctx, executionTimeLimit)
defer cancel()

answer := h.runMethod(ctx, msg, callb, args)

// Collect the statistics for RPC calls if metrics is enabled.
// We only care about pure rpc call. Filter out subscription.
Expand Down
4 changes: 0 additions & 4 deletions rpc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"runtime"
"strings"
"sync"
"time"
"unicode"

"github.com/ethereum/go-ethereum/log"
Expand Down Expand Up @@ -183,9 +182,6 @@ func (c *callback) makeArgTypes() {

// call invokes the callback.
func (c *callback) call(ctx context.Context, method string, args []reflect.Value) (res interface{}, errRes error) {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()

// Create the argument slice.
fullargs := make([]reflect.Value, 0, 2+len(args))
if c.rcvr.IsValid() {
Expand Down

0 comments on commit d862fc5

Please sign in to comment.