Skip to content

Commit

Permalink
add SetBatchLimits for client with default limit
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsqe committed Feb 16, 2023
1 parent 6d2ce24 commit 2c04aa0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
14 changes: 8 additions & 6 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -787,14 +787,16 @@ var (
Category: flags.APICategory,
}
BatchRequestLimit = &cli.IntFlag{
Name: "batch.request-limit",
Usage: "Maximum number of requests in a batch",
Value: 100,
Name: "batch.request-limit",
Usage: "Maximum number of requests in a batch",
Value: rpc.BatchRequestLimit,
Category: flags.APICategory,
}
BatchResponseMaxSize = &cli.IntFlag{
Name: "batch.response-max-size",
Usage: "Maximum number of bytes returned from calls (10MB)",
Value: 10 * 1000 * 1000,
Name: "batch.response-max-size",
Usage: "Maximum number of bytes returned from calls (10MB)",
Value: rpc.BatchResponseMaxSize,
Category: flags.APICategory,
}
EnablePersonal = &cli.BoolFlag{
Name: "rpc.enabledeprecatedpersonal",
Expand Down
21 changes: 20 additions & 1 deletion rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ const (
// Timeouts
defaultDialTimeout = 10 * time.Second // used if context has no deadline
subscribeTimeout = 5 * time.Second // overall timeout eth_subscribe, rpc_modules calls

// Batch limits
BatchRequestLimit = 100 // Maximum number of requests in a batch
BatchResponseMaxSize = 10 * 1000 * 1000 // Maximum number of bytes returned from calls (10MB)
)

const (
Expand Down Expand Up @@ -99,6 +103,9 @@ type Client struct {
reqInit chan *requestOp // register response IDs, takes write lock
reqSent chan error // signals write completion, releases write lock
reqTimeout chan *requestOp // removes response IDs when call timeout expires

batchRequestLimit int
batchResponseMaxSize int
}

type reconnectFunc func(context.Context) (ServerCodec, error)
Expand All @@ -114,10 +121,22 @@ func (c *Client) newClientConn(conn ServerCodec) *clientConn {
ctx := context.Background()
ctx = context.WithValue(ctx, clientContextKey{}, c)
ctx = context.WithValue(ctx, peerInfoContextKey{}, conn.peerInfo())
handler := newHandler(ctx, conn, c.idgen, c.services, 0, 0)
if c.batchRequestLimit == 0 {
c.batchRequestLimit = BatchRequestLimit
}
if c.batchResponseMaxSize == 0 {
c.batchResponseMaxSize = BatchResponseMaxSize
}
handler := newHandler(ctx, conn, c.idgen, c.services, c.batchRequestLimit, c.batchResponseMaxSize)
return &clientConn{conn, handler}
}

// SetBatchLimits set maximum number of requests in a batch and maximum number of bytes returned from calls
func (c *Client) SetBatchLimits(limit int, size int) {
c.batchRequestLimit = limit
c.batchResponseMaxSize = size
}

func (cc *clientConn) close(err error, inflightReq *requestOp) {
cc.handler.close(err, inflightReq)
cc.codec.close()
Expand Down
1 change: 1 addition & 0 deletions rpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func (s *Server) ServeCodec(codec ServerCodec, options CodecOption) {
defer s.untrackCodec(codec)

c := initClient(codec, s.idgen, &s.services)
c.SetBatchLimits(s.batchRequestLimit, s.batchResponseMaxSize)
<-codec.closed()
c.Close()
}
Expand Down

0 comments on commit 2c04aa0

Please sign in to comment.