2019-07-22 09:17:27 +00:00
|
|
|
// Copyright 2019 The go-ethereum Authors
|
2019-02-04 12:47:34 +00:00
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// The entry points for incoming messages are:
|
|
|
|
//
|
2022-09-10 11:25:40 +00:00
|
|
|
// h.handleMsg(message)
|
|
|
|
// h.handleBatch(message)
|
2019-02-04 12:47:34 +00:00
|
|
|
//
|
|
|
|
// Outgoing calls use the requestOp struct. Register the request before sending it
|
|
|
|
// on the connection:
|
|
|
|
//
|
2022-09-10 11:25:40 +00:00
|
|
|
// op := &requestOp{ids: ...}
|
|
|
|
// h.addRequestOp(op)
|
2019-02-04 12:47:34 +00:00
|
|
|
//
|
|
|
|
// Now send the request, then wait for the reply to be delivered through handleMsg:
|
|
|
|
//
|
2022-09-10 11:25:40 +00:00
|
|
|
// if err := op.wait(...); err != nil {
|
|
|
|
// h.removeRequestOp(op) // timeout, etc.
|
|
|
|
// }
|
2019-02-04 12:47:34 +00:00
|
|
|
type handler struct {
|
rpc: add limit for batch request items and response size (#26681)
This PR adds server-side limits for JSON-RPC batch requests. Before this change, batches
were limited only by processing time. The server would pick calls from the batch and
answer them until the response timeout occurred, then stop processing the remaining batch
items.
Here, we are adding two additional limits which can be configured:
- the 'item limit': batches can have at most N items
- the 'response size limit': batches can contain at most X response bytes
These limits are optional in package rpc. In Geth, we set a default limit of 1000 items
and 25MB response size.
When a batch goes over the limit, an error response is returned to the client. However,
doing this correctly isn't always possible. In JSON-RPC, only method calls with a valid
`id` can be responded to. Since batches may also contain non-call messages or
notifications, the best effort thing we can do to report an error with the batch itself is
reporting the limit violation as an error for the first method call in the batch. If a batch is
too large, but contains only notifications and responses, the error will be reported with
a null `id`.
The RPC client was also changed so it can deal with errors resulting from too large
batches. An older client connected to the server code in this PR could get stuck
until the request timeout occurred when the batch is too large. **Upgrading to a version
of the RPC client containing this change is strongly recommended to avoid timeout issues.**
For some weird reason, when writing the original client implementation, @fjl worked off of
the assumption that responses could be distributed across batches arbitrarily. So for a
batch request containing requests `[A B C]`, the server could respond with `[A B C]` but
also with `[A B] [C]` or even `[A] [B] [C]` and it wouldn't make a difference to the
client.
So in the implementation of BatchCallContext, the client waited for all requests in the
batch individually. If the server didn't respond to some of the requests in the batch, the
client would eventually just time out (if a context was used).
With the addition of batch limits into the server, we anticipate that people will hit this
kind of error way more often. To handle this properly, the client now waits for a single
response batch and expects it to contain all responses to the requests.
---------
Co-authored-by: Felix Lange <fjl@twurst.com>
Co-authored-by: Martin Holst Swende <martin@swende.se>
2023-06-13 11:38:58 +00:00
|
|
|
reg *serviceRegistry
|
|
|
|
unsubscribeCb *callback
|
|
|
|
idgen func() ID // subscription ID generator
|
|
|
|
respWait map[string]*requestOp // active client requests
|
|
|
|
clientSubs map[string]*ClientSubscription // active client subscriptions
|
|
|
|
callWG sync.WaitGroup // pending call goroutines
|
|
|
|
rootCtx context.Context // canceled by close()
|
|
|
|
cancelRoot func() // cancel function for rootCtx
|
|
|
|
conn jsonWriter // where responses will be sent
|
|
|
|
log log.Logger
|
|
|
|
allowSubscribe bool
|
|
|
|
batchRequestLimit int
|
|
|
|
batchResponseMaxSize int
|
2019-02-04 12:47:34 +00:00
|
|
|
|
|
|
|
subLock sync.Mutex
|
|
|
|
serverSubs map[ID]*Subscription
|
|
|
|
}
|
|
|
|
|
|
|
|
type callProc struct {
|
|
|
|
ctx context.Context
|
|
|
|
notifiers []*Notifier
|
|
|
|
}
|
|
|
|
|
rpc: add limit for batch request items and response size (#26681)
This PR adds server-side limits for JSON-RPC batch requests. Before this change, batches
were limited only by processing time. The server would pick calls from the batch and
answer them until the response timeout occurred, then stop processing the remaining batch
items.
Here, we are adding two additional limits which can be configured:
- the 'item limit': batches can have at most N items
- the 'response size limit': batches can contain at most X response bytes
These limits are optional in package rpc. In Geth, we set a default limit of 1000 items
and 25MB response size.
When a batch goes over the limit, an error response is returned to the client. However,
doing this correctly isn't always possible. In JSON-RPC, only method calls with a valid
`id` can be responded to. Since batches may also contain non-call messages or
notifications, the best effort thing we can do to report an error with the batch itself is
reporting the limit violation as an error for the first method call in the batch. If a batch is
too large, but contains only notifications and responses, the error will be reported with
a null `id`.
The RPC client was also changed so it can deal with errors resulting from too large
batches. An older client connected to the server code in this PR could get stuck
until the request timeout occurred when the batch is too large. **Upgrading to a version
of the RPC client containing this change is strongly recommended to avoid timeout issues.**
For some weird reason, when writing the original client implementation, @fjl worked off of
the assumption that responses could be distributed across batches arbitrarily. So for a
batch request containing requests `[A B C]`, the server could respond with `[A B C]` but
also with `[A B] [C]` or even `[A] [B] [C]` and it wouldn't make a difference to the
client.
So in the implementation of BatchCallContext, the client waited for all requests in the
batch individually. If the server didn't respond to some of the requests in the batch, the
client would eventually just time out (if a context was used).
With the addition of batch limits into the server, we anticipate that people will hit this
kind of error way more often. To handle this properly, the client now waits for a single
response batch and expects it to contain all responses to the requests.
---------
Co-authored-by: Felix Lange <fjl@twurst.com>
Co-authored-by: Martin Holst Swende <martin@swende.se>
2023-06-13 11:38:58 +00:00
|
|
|
func newHandler(connCtx context.Context, conn jsonWriter, idgen func() ID, reg *serviceRegistry, batchRequestLimit, batchResponseMaxSize int) *handler {
|
2019-02-04 12:47:34 +00:00
|
|
|
rootCtx, cancelRoot := context.WithCancel(connCtx)
|
|
|
|
h := &handler{
|
rpc: add limit for batch request items and response size (#26681)
This PR adds server-side limits for JSON-RPC batch requests. Before this change, batches
were limited only by processing time. The server would pick calls from the batch and
answer them until the response timeout occurred, then stop processing the remaining batch
items.
Here, we are adding two additional limits which can be configured:
- the 'item limit': batches can have at most N items
- the 'response size limit': batches can contain at most X response bytes
These limits are optional in package rpc. In Geth, we set a default limit of 1000 items
and 25MB response size.
When a batch goes over the limit, an error response is returned to the client. However,
doing this correctly isn't always possible. In JSON-RPC, only method calls with a valid
`id` can be responded to. Since batches may also contain non-call messages or
notifications, the best effort thing we can do to report an error with the batch itself is
reporting the limit violation as an error for the first method call in the batch. If a batch is
too large, but contains only notifications and responses, the error will be reported with
a null `id`.
The RPC client was also changed so it can deal with errors resulting from too large
batches. An older client connected to the server code in this PR could get stuck
until the request timeout occurred when the batch is too large. **Upgrading to a version
of the RPC client containing this change is strongly recommended to avoid timeout issues.**
For some weird reason, when writing the original client implementation, @fjl worked off of
the assumption that responses could be distributed across batches arbitrarily. So for a
batch request containing requests `[A B C]`, the server could respond with `[A B C]` but
also with `[A B] [C]` or even `[A] [B] [C]` and it wouldn't make a difference to the
client.
So in the implementation of BatchCallContext, the client waited for all requests in the
batch individually. If the server didn't respond to some of the requests in the batch, the
client would eventually just time out (if a context was used).
With the addition of batch limits into the server, we anticipate that people will hit this
kind of error way more often. To handle this properly, the client now waits for a single
response batch and expects it to contain all responses to the requests.
---------
Co-authored-by: Felix Lange <fjl@twurst.com>
Co-authored-by: Martin Holst Swende <martin@swende.se>
2023-06-13 11:38:58 +00:00
|
|
|
reg: reg,
|
|
|
|
idgen: idgen,
|
|
|
|
conn: conn,
|
|
|
|
respWait: make(map[string]*requestOp),
|
|
|
|
clientSubs: make(map[string]*ClientSubscription),
|
|
|
|
rootCtx: rootCtx,
|
|
|
|
cancelRoot: cancelRoot,
|
|
|
|
allowSubscribe: true,
|
|
|
|
serverSubs: make(map[ID]*Subscription),
|
|
|
|
log: log.Root(),
|
|
|
|
batchRequestLimit: batchRequestLimit,
|
|
|
|
batchResponseMaxSize: batchResponseMaxSize,
|
2019-02-04 12:47:34 +00:00
|
|
|
}
|
2019-11-18 08:40:59 +00:00
|
|
|
if conn.remoteAddr() != "" {
|
|
|
|
h.log = h.log.New("conn", conn.remoteAddr())
|
2019-02-04 12:47:34 +00:00
|
|
|
}
|
|
|
|
h.unsubscribeCb = newCallback(reflect.Value{}, reflect.ValueOf(h.unsubscribe))
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2022-12-07 13:02:14 +00:00
|
|
|
// batchCallBuffer manages in progress call messages and their responses during a batch
|
|
|
|
// call. Calls need to be synchronized between the processing and timeout-triggering
|
|
|
|
// goroutines.
|
|
|
|
type batchCallBuffer struct {
|
|
|
|
mutex sync.Mutex
|
|
|
|
calls []*jsonrpcMessage
|
|
|
|
resp []*jsonrpcMessage
|
|
|
|
wrote bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// nextCall returns the next unprocessed message.
|
|
|
|
func (b *batchCallBuffer) nextCall() *jsonrpcMessage {
|
|
|
|
b.mutex.Lock()
|
|
|
|
defer b.mutex.Unlock()
|
|
|
|
|
|
|
|
if len(b.calls) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// The popping happens in `pushAnswer`. The in progress call is kept
|
|
|
|
// so we can return an error for it in case of timeout.
|
|
|
|
msg := b.calls[0]
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
|
|
|
// pushResponse adds the response to last call returned by nextCall.
|
|
|
|
func (b *batchCallBuffer) pushResponse(answer *jsonrpcMessage) {
|
|
|
|
b.mutex.Lock()
|
|
|
|
defer b.mutex.Unlock()
|
|
|
|
|
|
|
|
if answer != nil {
|
|
|
|
b.resp = append(b.resp, answer)
|
|
|
|
}
|
|
|
|
b.calls = b.calls[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
// write sends the responses.
|
|
|
|
func (b *batchCallBuffer) write(ctx context.Context, conn jsonWriter) {
|
|
|
|
b.mutex.Lock()
|
|
|
|
defer b.mutex.Unlock()
|
|
|
|
|
|
|
|
b.doWrite(ctx, conn, false)
|
|
|
|
}
|
|
|
|
|
rpc: add limit for batch request items and response size (#26681)
This PR adds server-side limits for JSON-RPC batch requests. Before this change, batches
were limited only by processing time. The server would pick calls from the batch and
answer them until the response timeout occurred, then stop processing the remaining batch
items.
Here, we are adding two additional limits which can be configured:
- the 'item limit': batches can have at most N items
- the 'response size limit': batches can contain at most X response bytes
These limits are optional in package rpc. In Geth, we set a default limit of 1000 items
and 25MB response size.
When a batch goes over the limit, an error response is returned to the client. However,
doing this correctly isn't always possible. In JSON-RPC, only method calls with a valid
`id` can be responded to. Since batches may also contain non-call messages or
notifications, the best effort thing we can do to report an error with the batch itself is
reporting the limit violation as an error for the first method call in the batch. If a batch is
too large, but contains only notifications and responses, the error will be reported with
a null `id`.
The RPC client was also changed so it can deal with errors resulting from too large
batches. An older client connected to the server code in this PR could get stuck
until the request timeout occurred when the batch is too large. **Upgrading to a version
of the RPC client containing this change is strongly recommended to avoid timeout issues.**
For some weird reason, when writing the original client implementation, @fjl worked off of
the assumption that responses could be distributed across batches arbitrarily. So for a
batch request containing requests `[A B C]`, the server could respond with `[A B C]` but
also with `[A B] [C]` or even `[A] [B] [C]` and it wouldn't make a difference to the
client.
So in the implementation of BatchCallContext, the client waited for all requests in the
batch individually. If the server didn't respond to some of the requests in the batch, the
client would eventually just time out (if a context was used).
With the addition of batch limits into the server, we anticipate that people will hit this
kind of error way more often. To handle this properly, the client now waits for a single
response batch and expects it to contain all responses to the requests.
---------
Co-authored-by: Felix Lange <fjl@twurst.com>
Co-authored-by: Martin Holst Swende <martin@swende.se>
2023-06-13 11:38:58 +00:00
|
|
|
// respondWithError sends the responses added so far. For the remaining unanswered call
|
|
|
|
// messages, it responds with the given error.
|
|
|
|
func (b *batchCallBuffer) respondWithError(ctx context.Context, conn jsonWriter, err error) {
|
2022-12-07 13:02:14 +00:00
|
|
|
b.mutex.Lock()
|
|
|
|
defer b.mutex.Unlock()
|
|
|
|
|
|
|
|
for _, msg := range b.calls {
|
|
|
|
if !msg.isNotification() {
|
rpc: add limit for batch request items and response size (#26681)
This PR adds server-side limits for JSON-RPC batch requests. Before this change, batches
were limited only by processing time. The server would pick calls from the batch and
answer them until the response timeout occurred, then stop processing the remaining batch
items.
Here, we are adding two additional limits which can be configured:
- the 'item limit': batches can have at most N items
- the 'response size limit': batches can contain at most X response bytes
These limits are optional in package rpc. In Geth, we set a default limit of 1000 items
and 25MB response size.
When a batch goes over the limit, an error response is returned to the client. However,
doing this correctly isn't always possible. In JSON-RPC, only method calls with a valid
`id` can be responded to. Since batches may also contain non-call messages or
notifications, the best effort thing we can do to report an error with the batch itself is
reporting the limit violation as an error for the first method call in the batch. If a batch is
too large, but contains only notifications and responses, the error will be reported with
a null `id`.
The RPC client was also changed so it can deal with errors resulting from too large
batches. An older client connected to the server code in this PR could get stuck
until the request timeout occurred when the batch is too large. **Upgrading to a version
of the RPC client containing this change is strongly recommended to avoid timeout issues.**
For some weird reason, when writing the original client implementation, @fjl worked off of
the assumption that responses could be distributed across batches arbitrarily. So for a
batch request containing requests `[A B C]`, the server could respond with `[A B C]` but
also with `[A B] [C]` or even `[A] [B] [C]` and it wouldn't make a difference to the
client.
So in the implementation of BatchCallContext, the client waited for all requests in the
batch individually. If the server didn't respond to some of the requests in the batch, the
client would eventually just time out (if a context was used).
With the addition of batch limits into the server, we anticipate that people will hit this
kind of error way more often. To handle this properly, the client now waits for a single
response batch and expects it to contain all responses to the requests.
---------
Co-authored-by: Felix Lange <fjl@twurst.com>
Co-authored-by: Martin Holst Swende <martin@swende.se>
2023-06-13 11:38:58 +00:00
|
|
|
b.resp = append(b.resp, msg.errorResponse(err))
|
2022-12-07 13:02:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
b.doWrite(ctx, conn, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// doWrite actually writes the response.
|
|
|
|
// This assumes b.mutex is held.
|
|
|
|
func (b *batchCallBuffer) doWrite(ctx context.Context, conn jsonWriter, isErrorResponse bool) {
|
|
|
|
if b.wrote {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
b.wrote = true // can only write once
|
|
|
|
if len(b.resp) > 0 {
|
|
|
|
conn.writeJSON(ctx, b.resp, isErrorResponse)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-04 12:47:34 +00:00
|
|
|
// handleBatch executes all messages in a batch and returns the responses.
|
|
|
|
func (h *handler) handleBatch(msgs []*jsonrpcMessage) {
|
|
|
|
// Emit error response for empty batches:
|
|
|
|
if len(msgs) == 0 {
|
|
|
|
h.startCallProc(func(cp *callProc) {
|
2022-12-07 13:02:14 +00:00
|
|
|
resp := errorMessage(&invalidRequestError{"empty batch"})
|
|
|
|
h.conn.writeJSON(cp.ctx, resp, true)
|
2019-02-04 12:47:34 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
rpc: add limit for batch request items and response size (#26681)
This PR adds server-side limits for JSON-RPC batch requests. Before this change, batches
were limited only by processing time. The server would pick calls from the batch and
answer them until the response timeout occurred, then stop processing the remaining batch
items.
Here, we are adding two additional limits which can be configured:
- the 'item limit': batches can have at most N items
- the 'response size limit': batches can contain at most X response bytes
These limits are optional in package rpc. In Geth, we set a default limit of 1000 items
and 25MB response size.
When a batch goes over the limit, an error response is returned to the client. However,
doing this correctly isn't always possible. In JSON-RPC, only method calls with a valid
`id` can be responded to. Since batches may also contain non-call messages or
notifications, the best effort thing we can do to report an error with the batch itself is
reporting the limit violation as an error for the first method call in the batch. If a batch is
too large, but contains only notifications and responses, the error will be reported with
a null `id`.
The RPC client was also changed so it can deal with errors resulting from too large
batches. An older client connected to the server code in this PR could get stuck
until the request timeout occurred when the batch is too large. **Upgrading to a version
of the RPC client containing this change is strongly recommended to avoid timeout issues.**
For some weird reason, when writing the original client implementation, @fjl worked off of
the assumption that responses could be distributed across batches arbitrarily. So for a
batch request containing requests `[A B C]`, the server could respond with `[A B C]` but
also with `[A B] [C]` or even `[A] [B] [C]` and it wouldn't make a difference to the
client.
So in the implementation of BatchCallContext, the client waited for all requests in the
batch individually. If the server didn't respond to some of the requests in the batch, the
client would eventually just time out (if a context was used).
With the addition of batch limits into the server, we anticipate that people will hit this
kind of error way more often. To handle this properly, the client now waits for a single
response batch and expects it to contain all responses to the requests.
---------
Co-authored-by: Felix Lange <fjl@twurst.com>
Co-authored-by: Martin Holst Swende <martin@swende.se>
2023-06-13 11:38:58 +00:00
|
|
|
// Apply limit on total number of requests.
|
|
|
|
if h.batchRequestLimit != 0 && len(msgs) > h.batchRequestLimit {
|
|
|
|
h.startCallProc(func(cp *callProc) {
|
|
|
|
h.respondWithBatchTooLarge(cp, msgs)
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2019-02-04 12:47:34 +00:00
|
|
|
|
rpc: add limit for batch request items and response size (#26681)
This PR adds server-side limits for JSON-RPC batch requests. Before this change, batches
were limited only by processing time. The server would pick calls from the batch and
answer them until the response timeout occurred, then stop processing the remaining batch
items.
Here, we are adding two additional limits which can be configured:
- the 'item limit': batches can have at most N items
- the 'response size limit': batches can contain at most X response bytes
These limits are optional in package rpc. In Geth, we set a default limit of 1000 items
and 25MB response size.
When a batch goes over the limit, an error response is returned to the client. However,
doing this correctly isn't always possible. In JSON-RPC, only method calls with a valid
`id` can be responded to. Since batches may also contain non-call messages or
notifications, the best effort thing we can do to report an error with the batch itself is
reporting the limit violation as an error for the first method call in the batch. If a batch is
too large, but contains only notifications and responses, the error will be reported with
a null `id`.
The RPC client was also changed so it can deal with errors resulting from too large
batches. An older client connected to the server code in this PR could get stuck
until the request timeout occurred when the batch is too large. **Upgrading to a version
of the RPC client containing this change is strongly recommended to avoid timeout issues.**
For some weird reason, when writing the original client implementation, @fjl worked off of
the assumption that responses could be distributed across batches arbitrarily. So for a
batch request containing requests `[A B C]`, the server could respond with `[A B C]` but
also with `[A B] [C]` or even `[A] [B] [C]` and it wouldn't make a difference to the
client.
So in the implementation of BatchCallContext, the client waited for all requests in the
batch individually. If the server didn't respond to some of the requests in the batch, the
client would eventually just time out (if a context was used).
With the addition of batch limits into the server, we anticipate that people will hit this
kind of error way more often. To handle this properly, the client now waits for a single
response batch and expects it to contain all responses to the requests.
---------
Co-authored-by: Felix Lange <fjl@twurst.com>
Co-authored-by: Martin Holst Swende <martin@swende.se>
2023-06-13 11:38:58 +00:00
|
|
|
// Handle non-call messages first.
|
|
|
|
// Here we need to find the requestOp that sent the request batch.
|
2019-02-04 12:47:34 +00:00
|
|
|
calls := make([]*jsonrpcMessage, 0, len(msgs))
|
rpc: add limit for batch request items and response size (#26681)
This PR adds server-side limits for JSON-RPC batch requests. Before this change, batches
were limited only by processing time. The server would pick calls from the batch and
answer them until the response timeout occurred, then stop processing the remaining batch
items.
Here, we are adding two additional limits which can be configured:
- the 'item limit': batches can have at most N items
- the 'response size limit': batches can contain at most X response bytes
These limits are optional in package rpc. In Geth, we set a default limit of 1000 items
and 25MB response size.
When a batch goes over the limit, an error response is returned to the client. However,
doing this correctly isn't always possible. In JSON-RPC, only method calls with a valid
`id` can be responded to. Since batches may also contain non-call messages or
notifications, the best effort thing we can do to report an error with the batch itself is
reporting the limit violation as an error for the first method call in the batch. If a batch is
too large, but contains only notifications and responses, the error will be reported with
a null `id`.
The RPC client was also changed so it can deal with errors resulting from too large
batches. An older client connected to the server code in this PR could get stuck
until the request timeout occurred when the batch is too large. **Upgrading to a version
of the RPC client containing this change is strongly recommended to avoid timeout issues.**
For some weird reason, when writing the original client implementation, @fjl worked off of
the assumption that responses could be distributed across batches arbitrarily. So for a
batch request containing requests `[A B C]`, the server could respond with `[A B C]` but
also with `[A B] [C]` or even `[A] [B] [C]` and it wouldn't make a difference to the
client.
So in the implementation of BatchCallContext, the client waited for all requests in the
batch individually. If the server didn't respond to some of the requests in the batch, the
client would eventually just time out (if a context was used).
With the addition of batch limits into the server, we anticipate that people will hit this
kind of error way more often. To handle this properly, the client now waits for a single
response batch and expects it to contain all responses to the requests.
---------
Co-authored-by: Felix Lange <fjl@twurst.com>
Co-authored-by: Martin Holst Swende <martin@swende.se>
2023-06-13 11:38:58 +00:00
|
|
|
h.handleResponses(msgs, func(msg *jsonrpcMessage) {
|
|
|
|
calls = append(calls, msg)
|
|
|
|
})
|
2019-02-04 12:47:34 +00:00
|
|
|
if len(calls) == 0 {
|
|
|
|
return
|
|
|
|
}
|
rpc: add limit for batch request items and response size (#26681)
This PR adds server-side limits for JSON-RPC batch requests. Before this change, batches
were limited only by processing time. The server would pick calls from the batch and
answer them until the response timeout occurred, then stop processing the remaining batch
items.
Here, we are adding two additional limits which can be configured:
- the 'item limit': batches can have at most N items
- the 'response size limit': batches can contain at most X response bytes
These limits are optional in package rpc. In Geth, we set a default limit of 1000 items
and 25MB response size.
When a batch goes over the limit, an error response is returned to the client. However,
doing this correctly isn't always possible. In JSON-RPC, only method calls with a valid
`id` can be responded to. Since batches may also contain non-call messages or
notifications, the best effort thing we can do to report an error with the batch itself is
reporting the limit violation as an error for the first method call in the batch. If a batch is
too large, but contains only notifications and responses, the error will be reported with
a null `id`.
The RPC client was also changed so it can deal with errors resulting from too large
batches. An older client connected to the server code in this PR could get stuck
until the request timeout occurred when the batch is too large. **Upgrading to a version
of the RPC client containing this change is strongly recommended to avoid timeout issues.**
For some weird reason, when writing the original client implementation, @fjl worked off of
the assumption that responses could be distributed across batches arbitrarily. So for a
batch request containing requests `[A B C]`, the server could respond with `[A B C]` but
also with `[A B] [C]` or even `[A] [B] [C]` and it wouldn't make a difference to the
client.
So in the implementation of BatchCallContext, the client waited for all requests in the
batch individually. If the server didn't respond to some of the requests in the batch, the
client would eventually just time out (if a context was used).
With the addition of batch limits into the server, we anticipate that people will hit this
kind of error way more often. To handle this properly, the client now waits for a single
response batch and expects it to contain all responses to the requests.
---------
Co-authored-by: Felix Lange <fjl@twurst.com>
Co-authored-by: Martin Holst Swende <martin@swende.se>
2023-06-13 11:38:58 +00:00
|
|
|
|
2019-02-04 12:47:34 +00:00
|
|
|
// Process calls on a goroutine because they may block indefinitely:
|
|
|
|
h.startCallProc(func(cp *callProc) {
|
2022-12-07 13:02:14 +00:00
|
|
|
var (
|
|
|
|
timer *time.Timer
|
|
|
|
cancel context.CancelFunc
|
|
|
|
callBuffer = &batchCallBuffer{calls: calls, resp: make([]*jsonrpcMessage, 0, len(calls))}
|
|
|
|
)
|
|
|
|
|
|
|
|
cp.ctx, cancel = context.WithCancel(cp.ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// Cancel the request context after timeout and send an error response. Since the
|
|
|
|
// currently-running method might not return immediately on timeout, we must wait
|
|
|
|
// for the timeout concurrently with processing the request.
|
|
|
|
if timeout, ok := ContextRequestTimeout(cp.ctx); ok {
|
|
|
|
timer = time.AfterFunc(timeout, func() {
|
|
|
|
cancel()
|
rpc: add limit for batch request items and response size (#26681)
This PR adds server-side limits for JSON-RPC batch requests. Before this change, batches
were limited only by processing time. The server would pick calls from the batch and
answer them until the response timeout occurred, then stop processing the remaining batch
items.
Here, we are adding two additional limits which can be configured:
- the 'item limit': batches can have at most N items
- the 'response size limit': batches can contain at most X response bytes
These limits are optional in package rpc. In Geth, we set a default limit of 1000 items
and 25MB response size.
When a batch goes over the limit, an error response is returned to the client. However,
doing this correctly isn't always possible. In JSON-RPC, only method calls with a valid
`id` can be responded to. Since batches may also contain non-call messages or
notifications, the best effort thing we can do to report an error with the batch itself is
reporting the limit violation as an error for the first method call in the batch. If a batch is
too large, but contains only notifications and responses, the error will be reported with
a null `id`.
The RPC client was also changed so it can deal with errors resulting from too large
batches. An older client connected to the server code in this PR could get stuck
until the request timeout occurred when the batch is too large. **Upgrading to a version
of the RPC client containing this change is strongly recommended to avoid timeout issues.**
For some weird reason, when writing the original client implementation, @fjl worked off of
the assumption that responses could be distributed across batches arbitrarily. So for a
batch request containing requests `[A B C]`, the server could respond with `[A B C]` but
also with `[A B] [C]` or even `[A] [B] [C]` and it wouldn't make a difference to the
client.
So in the implementation of BatchCallContext, the client waited for all requests in the
batch individually. If the server didn't respond to some of the requests in the batch, the
client would eventually just time out (if a context was used).
With the addition of batch limits into the server, we anticipate that people will hit this
kind of error way more often. To handle this properly, the client now waits for a single
response batch and expects it to contain all responses to the requests.
---------
Co-authored-by: Felix Lange <fjl@twurst.com>
Co-authored-by: Martin Holst Swende <martin@swende.se>
2023-06-13 11:38:58 +00:00
|
|
|
err := &internalServerError{errcodeTimeout, errMsgTimeout}
|
|
|
|
callBuffer.respondWithError(cp.ctx, h.conn, err)
|
2022-12-07 13:02:14 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
rpc: add limit for batch request items and response size (#26681)
This PR adds server-side limits for JSON-RPC batch requests. Before this change, batches
were limited only by processing time. The server would pick calls from the batch and
answer them until the response timeout occurred, then stop processing the remaining batch
items.
Here, we are adding two additional limits which can be configured:
- the 'item limit': batches can have at most N items
- the 'response size limit': batches can contain at most X response bytes
These limits are optional in package rpc. In Geth, we set a default limit of 1000 items
and 25MB response size.
When a batch goes over the limit, an error response is returned to the client. However,
doing this correctly isn't always possible. In JSON-RPC, only method calls with a valid
`id` can be responded to. Since batches may also contain non-call messages or
notifications, the best effort thing we can do to report an error with the batch itself is
reporting the limit violation as an error for the first method call in the batch. If a batch is
too large, but contains only notifications and responses, the error will be reported with
a null `id`.
The RPC client was also changed so it can deal with errors resulting from too large
batches. An older client connected to the server code in this PR could get stuck
until the request timeout occurred when the batch is too large. **Upgrading to a version
of the RPC client containing this change is strongly recommended to avoid timeout issues.**
For some weird reason, when writing the original client implementation, @fjl worked off of
the assumption that responses could be distributed across batches arbitrarily. So for a
batch request containing requests `[A B C]`, the server could respond with `[A B C]` but
also with `[A B] [C]` or even `[A] [B] [C]` and it wouldn't make a difference to the
client.
So in the implementation of BatchCallContext, the client waited for all requests in the
batch individually. If the server didn't respond to some of the requests in the batch, the
client would eventually just time out (if a context was used).
With the addition of batch limits into the server, we anticipate that people will hit this
kind of error way more often. To handle this properly, the client now waits for a single
response batch and expects it to contain all responses to the requests.
---------
Co-authored-by: Felix Lange <fjl@twurst.com>
Co-authored-by: Martin Holst Swende <martin@swende.se>
2023-06-13 11:38:58 +00:00
|
|
|
responseBytes := 0
|
2022-12-07 13:02:14 +00:00
|
|
|
for {
|
|
|
|
// No need to handle rest of calls if timed out.
|
|
|
|
if cp.ctx.Err() != nil {
|
|
|
|
break
|
2019-02-04 12:47:34 +00:00
|
|
|
}
|
2022-12-07 13:02:14 +00:00
|
|
|
msg := callBuffer.nextCall()
|
|
|
|
if msg == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
resp := h.handleCallMsg(cp, msg)
|
|
|
|
callBuffer.pushResponse(resp)
|
rpc: add limit for batch request items and response size (#26681)
This PR adds server-side limits for JSON-RPC batch requests. Before this change, batches
were limited only by processing time. The server would pick calls from the batch and
answer them until the response timeout occurred, then stop processing the remaining batch
items.
Here, we are adding two additional limits which can be configured:
- the 'item limit': batches can have at most N items
- the 'response size limit': batches can contain at most X response bytes
These limits are optional in package rpc. In Geth, we set a default limit of 1000 items
and 25MB response size.
When a batch goes over the limit, an error response is returned to the client. However,
doing this correctly isn't always possible. In JSON-RPC, only method calls with a valid
`id` can be responded to. Since batches may also contain non-call messages or
notifications, the best effort thing we can do to report an error with the batch itself is
reporting the limit violation as an error for the first method call in the batch. If a batch is
too large, but contains only notifications and responses, the error will be reported with
a null `id`.
The RPC client was also changed so it can deal with errors resulting from too large
batches. An older client connected to the server code in this PR could get stuck
until the request timeout occurred when the batch is too large. **Upgrading to a version
of the RPC client containing this change is strongly recommended to avoid timeout issues.**
For some weird reason, when writing the original client implementation, @fjl worked off of
the assumption that responses could be distributed across batches arbitrarily. So for a
batch request containing requests `[A B C]`, the server could respond with `[A B C]` but
also with `[A B] [C]` or even `[A] [B] [C]` and it wouldn't make a difference to the
client.
So in the implementation of BatchCallContext, the client waited for all requests in the
batch individually. If the server didn't respond to some of the requests in the batch, the
client would eventually just time out (if a context was used).
With the addition of batch limits into the server, we anticipate that people will hit this
kind of error way more often. To handle this properly, the client now waits for a single
response batch and expects it to contain all responses to the requests.
---------
Co-authored-by: Felix Lange <fjl@twurst.com>
Co-authored-by: Martin Holst Swende <martin@swende.se>
2023-06-13 11:38:58 +00:00
|
|
|
if resp != nil && h.batchResponseMaxSize != 0 {
|
|
|
|
responseBytes += len(resp.Result)
|
|
|
|
if responseBytes > h.batchResponseMaxSize {
|
|
|
|
err := &internalServerError{errcodeResponseTooLarge, errMsgResponseTooLarge}
|
|
|
|
callBuffer.respondWithError(cp.ctx, h.conn, err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-02-04 12:47:34 +00:00
|
|
|
}
|
2022-12-07 13:02:14 +00:00
|
|
|
if timer != nil {
|
|
|
|
timer.Stop()
|
2019-02-04 12:47:34 +00:00
|
|
|
}
|
rpc: add limit for batch request items and response size (#26681)
This PR adds server-side limits for JSON-RPC batch requests. Before this change, batches
were limited only by processing time. The server would pick calls from the batch and
answer them until the response timeout occurred, then stop processing the remaining batch
items.
Here, we are adding two additional limits which can be configured:
- the 'item limit': batches can have at most N items
- the 'response size limit': batches can contain at most X response bytes
These limits are optional in package rpc. In Geth, we set a default limit of 1000 items
and 25MB response size.
When a batch goes over the limit, an error response is returned to the client. However,
doing this correctly isn't always possible. In JSON-RPC, only method calls with a valid
`id` can be responded to. Since batches may also contain non-call messages or
notifications, the best effort thing we can do to report an error with the batch itself is
reporting the limit violation as an error for the first method call in the batch. If a batch is
too large, but contains only notifications and responses, the error will be reported with
a null `id`.
The RPC client was also changed so it can deal with errors resulting from too large
batches. An older client connected to the server code in this PR could get stuck
until the request timeout occurred when the batch is too large. **Upgrading to a version
of the RPC client containing this change is strongly recommended to avoid timeout issues.**
For some weird reason, when writing the original client implementation, @fjl worked off of
the assumption that responses could be distributed across batches arbitrarily. So for a
batch request containing requests `[A B C]`, the server could respond with `[A B C]` but
also with `[A B] [C]` or even `[A] [B] [C]` and it wouldn't make a difference to the
client.
So in the implementation of BatchCallContext, the client waited for all requests in the
batch individually. If the server didn't respond to some of the requests in the batch, the
client would eventually just time out (if a context was used).
With the addition of batch limits into the server, we anticipate that people will hit this
kind of error way more often. To handle this properly, the client now waits for a single
response batch and expects it to contain all responses to the requests.
---------
Co-authored-by: Felix Lange <fjl@twurst.com>
Co-authored-by: Martin Holst Swende <martin@swende.se>
2023-06-13 11:38:58 +00:00
|
|
|
|
2022-12-07 13:02:14 +00:00
|
|
|
h.addSubscriptions(cp.notifiers)
|
rpc: add limit for batch request items and response size (#26681)
This PR adds server-side limits for JSON-RPC batch requests. Before this change, batches
were limited only by processing time. The server would pick calls from the batch and
answer them until the response timeout occurred, then stop processing the remaining batch
items.
Here, we are adding two additional limits which can be configured:
- the 'item limit': batches can have at most N items
- the 'response size limit': batches can contain at most X response bytes
These limits are optional in package rpc. In Geth, we set a default limit of 1000 items
and 25MB response size.
When a batch goes over the limit, an error response is returned to the client. However,
doing this correctly isn't always possible. In JSON-RPC, only method calls with a valid
`id` can be responded to. Since batches may also contain non-call messages or
notifications, the best effort thing we can do to report an error with the batch itself is
reporting the limit violation as an error for the first method call in the batch. If a batch is
too large, but contains only notifications and responses, the error will be reported with
a null `id`.
The RPC client was also changed so it can deal with errors resulting from too large
batches. An older client connected to the server code in this PR could get stuck
until the request timeout occurred when the batch is too large. **Upgrading to a version
of the RPC client containing this change is strongly recommended to avoid timeout issues.**
For some weird reason, when writing the original client implementation, @fjl worked off of
the assumption that responses could be distributed across batches arbitrarily. So for a
batch request containing requests `[A B C]`, the server could respond with `[A B C]` but
also with `[A B] [C]` or even `[A] [B] [C]` and it wouldn't make a difference to the
client.
So in the implementation of BatchCallContext, the client waited for all requests in the
batch individually. If the server didn't respond to some of the requests in the batch, the
client would eventually just time out (if a context was used).
With the addition of batch limits into the server, we anticipate that people will hit this
kind of error way more often. To handle this properly, the client now waits for a single
response batch and expects it to contain all responses to the requests.
---------
Co-authored-by: Felix Lange <fjl@twurst.com>
Co-authored-by: Martin Holst Swende <martin@swende.se>
2023-06-13 11:38:58 +00:00
|
|
|
callBuffer.write(cp.ctx, h.conn)
|
2019-02-04 12:47:34 +00:00
|
|
|
for _, n := range cp.notifiers {
|
|
|
|
n.activate()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
rpc: add limit for batch request items and response size (#26681)
This PR adds server-side limits for JSON-RPC batch requests. Before this change, batches
were limited only by processing time. The server would pick calls from the batch and
answer them until the response timeout occurred, then stop processing the remaining batch
items.
Here, we are adding two additional limits which can be configured:
- the 'item limit': batches can have at most N items
- the 'response size limit': batches can contain at most X response bytes
These limits are optional in package rpc. In Geth, we set a default limit of 1000 items
and 25MB response size.
When a batch goes over the limit, an error response is returned to the client. However,
doing this correctly isn't always possible. In JSON-RPC, only method calls with a valid
`id` can be responded to. Since batches may also contain non-call messages or
notifications, the best effort thing we can do to report an error with the batch itself is
reporting the limit violation as an error for the first method call in the batch. If a batch is
too large, but contains only notifications and responses, the error will be reported with
a null `id`.
The RPC client was also changed so it can deal with errors resulting from too large
batches. An older client connected to the server code in this PR could get stuck
until the request timeout occurred when the batch is too large. **Upgrading to a version
of the RPC client containing this change is strongly recommended to avoid timeout issues.**
For some weird reason, when writing the original client implementation, @fjl worked off of
the assumption that responses could be distributed across batches arbitrarily. So for a
batch request containing requests `[A B C]`, the server could respond with `[A B C]` but
also with `[A B] [C]` or even `[A] [B] [C]` and it wouldn't make a difference to the
client.
So in the implementation of BatchCallContext, the client waited for all requests in the
batch individually. If the server didn't respond to some of the requests in the batch, the
client would eventually just time out (if a context was used).
With the addition of batch limits into the server, we anticipate that people will hit this
kind of error way more often. To handle this properly, the client now waits for a single
response batch and expects it to contain all responses to the requests.
---------
Co-authored-by: Felix Lange <fjl@twurst.com>
Co-authored-by: Martin Holst Swende <martin@swende.se>
2023-06-13 11:38:58 +00:00
|
|
|
func (h *handler) respondWithBatchTooLarge(cp *callProc, batch []*jsonrpcMessage) {
|
|
|
|
resp := errorMessage(&invalidRequestError{errMsgBatchTooLarge})
|
|
|
|
// Find the first call and add its "id" field to the error.
|
|
|
|
// This is the best we can do, given that the protocol doesn't have a way
|
|
|
|
// of reporting an error for the entire batch.
|
|
|
|
for _, msg := range batch {
|
|
|
|
if msg.isCall() {
|
|
|
|
resp.ID = msg.ID
|
|
|
|
break
|
|
|
|
}
|
2019-02-04 12:47:34 +00:00
|
|
|
}
|
rpc: add limit for batch request items and response size (#26681)
This PR adds server-side limits for JSON-RPC batch requests. Before this change, batches
were limited only by processing time. The server would pick calls from the batch and
answer them until the response timeout occurred, then stop processing the remaining batch
items.
Here, we are adding two additional limits which can be configured:
- the 'item limit': batches can have at most N items
- the 'response size limit': batches can contain at most X response bytes
These limits are optional in package rpc. In Geth, we set a default limit of 1000 items
and 25MB response size.
When a batch goes over the limit, an error response is returned to the client. However,
doing this correctly isn't always possible. In JSON-RPC, only method calls with a valid
`id` can be responded to. Since batches may also contain non-call messages or
notifications, the best effort thing we can do to report an error with the batch itself is
reporting the limit violation as an error for the first method call in the batch. If a batch is
too large, but contains only notifications and responses, the error will be reported with
a null `id`.
The RPC client was also changed so it can deal with errors resulting from too large
batches. An older client connected to the server code in this PR could get stuck
until the request timeout occurred when the batch is too large. **Upgrading to a version
of the RPC client containing this change is strongly recommended to avoid timeout issues.**
For some weird reason, when writing the original client implementation, @fjl worked off of
the assumption that responses could be distributed across batches arbitrarily. So for a
batch request containing requests `[A B C]`, the server could respond with `[A B C]` but
also with `[A B] [C]` or even `[A] [B] [C]` and it wouldn't make a difference to the
client.
So in the implementation of BatchCallContext, the client waited for all requests in the
batch individually. If the server didn't respond to some of the requests in the batch, the
client would eventually just time out (if a context was used).
With the addition of batch limits into the server, we anticipate that people will hit this
kind of error way more often. To handle this properly, the client now waits for a single
response batch and expects it to contain all responses to the requests.
---------
Co-authored-by: Felix Lange <fjl@twurst.com>
Co-authored-by: Martin Holst Swende <martin@swende.se>
2023-06-13 11:38:58 +00:00
|
|
|
h.conn.writeJSON(cp.ctx, []*jsonrpcMessage{resp}, true)
|
|
|
|
}
|
2022-12-07 13:02:14 +00:00
|
|
|
|
rpc: add limit for batch request items and response size (#26681)
This PR adds server-side limits for JSON-RPC batch requests. Before this change, batches
were limited only by processing time. The server would pick calls from the batch and
answer them until the response timeout occurred, then stop processing the remaining batch
items.
Here, we are adding two additional limits which can be configured:
- the 'item limit': batches can have at most N items
- the 'response size limit': batches can contain at most X response bytes
These limits are optional in package rpc. In Geth, we set a default limit of 1000 items
and 25MB response size.
When a batch goes over the limit, an error response is returned to the client. However,
doing this correctly isn't always possible. In JSON-RPC, only method calls with a valid
`id` can be responded to. Since batches may also contain non-call messages or
notifications, the best effort thing we can do to report an error with the batch itself is
reporting the limit violation as an error for the first method call in the batch. If a batch is
too large, but contains only notifications and responses, the error will be reported with
a null `id`.
The RPC client was also changed so it can deal with errors resulting from too large
batches. An older client connected to the server code in this PR could get stuck
until the request timeout occurred when the batch is too large. **Upgrading to a version
of the RPC client containing this change is strongly recommended to avoid timeout issues.**
For some weird reason, when writing the original client implementation, @fjl worked off of
the assumption that responses could be distributed across batches arbitrarily. So for a
batch request containing requests `[A B C]`, the server could respond with `[A B C]` but
also with `[A B] [C]` or even `[A] [B] [C]` and it wouldn't make a difference to the
client.
So in the implementation of BatchCallContext, the client waited for all requests in the
batch individually. If the server didn't respond to some of the requests in the batch, the
client would eventually just time out (if a context was used).
With the addition of batch limits into the server, we anticipate that people will hit this
kind of error way more often. To handle this properly, the client now waits for a single
response batch and expects it to contain all responses to the requests.
---------
Co-authored-by: Felix Lange <fjl@twurst.com>
Co-authored-by: Martin Holst Swende <martin@swende.se>
2023-06-13 11:38:58 +00:00
|
|
|
// handleMsg handles a single non-batch message.
|
|
|
|
func (h *handler) handleMsg(msg *jsonrpcMessage) {
|
|
|
|
msgs := []*jsonrpcMessage{msg}
|
|
|
|
h.handleResponses(msgs, func(msg *jsonrpcMessage) {
|
|
|
|
h.startCallProc(func(cp *callProc) {
|
|
|
|
h.handleNonBatchCall(cp, msg)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2022-12-07 13:02:14 +00:00
|
|
|
|
rpc: add limit for batch request items and response size (#26681)
This PR adds server-side limits for JSON-RPC batch requests. Before this change, batches
were limited only by processing time. The server would pick calls from the batch and
answer them until the response timeout occurred, then stop processing the remaining batch
items.
Here, we are adding two additional limits which can be configured:
- the 'item limit': batches can have at most N items
- the 'response size limit': batches can contain at most X response bytes
These limits are optional in package rpc. In Geth, we set a default limit of 1000 items
and 25MB response size.
When a batch goes over the limit, an error response is returned to the client. However,
doing this correctly isn't always possible. In JSON-RPC, only method calls with a valid
`id` can be responded to. Since batches may also contain non-call messages or
notifications, the best effort thing we can do to report an error with the batch itself is
reporting the limit violation as an error for the first method call in the batch. If a batch is
too large, but contains only notifications and responses, the error will be reported with
a null `id`.
The RPC client was also changed so it can deal with errors resulting from too large
batches. An older client connected to the server code in this PR could get stuck
until the request timeout occurred when the batch is too large. **Upgrading to a version
of the RPC client containing this change is strongly recommended to avoid timeout issues.**
For some weird reason, when writing the original client implementation, @fjl worked off of
the assumption that responses could be distributed across batches arbitrarily. So for a
batch request containing requests `[A B C]`, the server could respond with `[A B C]` but
also with `[A B] [C]` or even `[A] [B] [C]` and it wouldn't make a difference to the
client.
So in the implementation of BatchCallContext, the client waited for all requests in the
batch individually. If the server didn't respond to some of the requests in the batch, the
client would eventually just time out (if a context was used).
With the addition of batch limits into the server, we anticipate that people will hit this
kind of error way more often. To handle this properly, the client now waits for a single
response batch and expects it to contain all responses to the requests.
---------
Co-authored-by: Felix Lange <fjl@twurst.com>
Co-authored-by: Martin Holst Swende <martin@swende.se>
2023-06-13 11:38:58 +00:00
|
|
|
func (h *handler) handleNonBatchCall(cp *callProc, msg *jsonrpcMessage) {
|
|
|
|
var (
|
|
|
|
responded sync.Once
|
|
|
|
timer *time.Timer
|
|
|
|
cancel context.CancelFunc
|
|
|
|
)
|
|
|
|
cp.ctx, cancel = context.WithCancel(cp.ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// Cancel the request context after timeout and send an error response. Since the
|
|
|
|
// running method might not return immediately on timeout, we must wait for the
|
|
|
|
// timeout concurrently with processing the request.
|
|
|
|
if timeout, ok := ContextRequestTimeout(cp.ctx); ok {
|
|
|
|
timer = time.AfterFunc(timeout, func() {
|
|
|
|
cancel()
|
2022-12-07 13:02:14 +00:00
|
|
|
responded.Do(func() {
|
rpc: add limit for batch request items and response size (#26681)
This PR adds server-side limits for JSON-RPC batch requests. Before this change, batches
were limited only by processing time. The server would pick calls from the batch and
answer them until the response timeout occurred, then stop processing the remaining batch
items.
Here, we are adding two additional limits which can be configured:
- the 'item limit': batches can have at most N items
- the 'response size limit': batches can contain at most X response bytes
These limits are optional in package rpc. In Geth, we set a default limit of 1000 items
and 25MB response size.
When a batch goes over the limit, an error response is returned to the client. However,
doing this correctly isn't always possible. In JSON-RPC, only method calls with a valid
`id` can be responded to. Since batches may also contain non-call messages or
notifications, the best effort thing we can do to report an error with the batch itself is
reporting the limit violation as an error for the first method call in the batch. If a batch is
too large, but contains only notifications and responses, the error will be reported with
a null `id`.
The RPC client was also changed so it can deal with errors resulting from too large
batches. An older client connected to the server code in this PR could get stuck
until the request timeout occurred when the batch is too large. **Upgrading to a version
of the RPC client containing this change is strongly recommended to avoid timeout issues.**
For some weird reason, when writing the original client implementation, @fjl worked off of
the assumption that responses could be distributed across batches arbitrarily. So for a
batch request containing requests `[A B C]`, the server could respond with `[A B C]` but
also with `[A B] [C]` or even `[A] [B] [C]` and it wouldn't make a difference to the
client.
So in the implementation of BatchCallContext, the client waited for all requests in the
batch individually. If the server didn't respond to some of the requests in the batch, the
client would eventually just time out (if a context was used).
With the addition of batch limits into the server, we anticipate that people will hit this
kind of error way more often. To handle this properly, the client now waits for a single
response batch and expects it to contain all responses to the requests.
---------
Co-authored-by: Felix Lange <fjl@twurst.com>
Co-authored-by: Martin Holst Swende <martin@swende.se>
2023-06-13 11:38:58 +00:00
|
|
|
resp := msg.errorResponse(&internalServerError{errcodeTimeout, errMsgTimeout})
|
|
|
|
h.conn.writeJSON(cp.ctx, resp, true)
|
2022-12-07 13:02:14 +00:00
|
|
|
})
|
rpc: add limit for batch request items and response size (#26681)
This PR adds server-side limits for JSON-RPC batch requests. Before this change, batches
were limited only by processing time. The server would pick calls from the batch and
answer them until the response timeout occurred, then stop processing the remaining batch
items.
Here, we are adding two additional limits which can be configured:
- the 'item limit': batches can have at most N items
- the 'response size limit': batches can contain at most X response bytes
These limits are optional in package rpc. In Geth, we set a default limit of 1000 items
and 25MB response size.
When a batch goes over the limit, an error response is returned to the client. However,
doing this correctly isn't always possible. In JSON-RPC, only method calls with a valid
`id` can be responded to. Since batches may also contain non-call messages or
notifications, the best effort thing we can do to report an error with the batch itself is
reporting the limit violation as an error for the first method call in the batch. If a batch is
too large, but contains only notifications and responses, the error will be reported with
a null `id`.
The RPC client was also changed so it can deal with errors resulting from too large
batches. An older client connected to the server code in this PR could get stuck
until the request timeout occurred when the batch is too large. **Upgrading to a version
of the RPC client containing this change is strongly recommended to avoid timeout issues.**
For some weird reason, when writing the original client implementation, @fjl worked off of
the assumption that responses could be distributed across batches arbitrarily. So for a
batch request containing requests `[A B C]`, the server could respond with `[A B C]` but
also with `[A B] [C]` or even `[A] [B] [C]` and it wouldn't make a difference to the
client.
So in the implementation of BatchCallContext, the client waited for all requests in the
batch individually. If the server didn't respond to some of the requests in the batch, the
client would eventually just time out (if a context was used).
With the addition of batch limits into the server, we anticipate that people will hit this
kind of error way more often. To handle this properly, the client now waits for a single
response batch and expects it to contain all responses to the requests.
---------
Co-authored-by: Felix Lange <fjl@twurst.com>
Co-authored-by: Martin Holst Swende <martin@swende.se>
2023-06-13 11:38:58 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
answer := h.handleCallMsg(cp, msg)
|
|
|
|
if timer != nil {
|
|
|
|
timer.Stop()
|
|
|
|
}
|
|
|
|
h.addSubscriptions(cp.notifiers)
|
|
|
|
if answer != nil {
|
|
|
|
responded.Do(func() {
|
|
|
|
h.conn.writeJSON(cp.ctx, answer, false)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
for _, n := range cp.notifiers {
|
|
|
|
n.activate()
|
|
|
|
}
|
2019-02-04 12:47:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// close cancels all requests except for inflightReq and waits for
|
|
|
|
// call goroutines to shut down.
|
|
|
|
func (h *handler) close(err error, inflightReq *requestOp) {
|
|
|
|
h.cancelAllRequests(err, inflightReq)
|
|
|
|
h.callWG.Wait()
|
2019-04-10 08:47:09 +00:00
|
|
|
h.cancelRoot()
|
2019-02-04 12:47:34 +00:00
|
|
|
h.cancelServerSubscriptions(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// addRequestOp registers a request operation.
|
|
|
|
func (h *handler) addRequestOp(op *requestOp) {
|
|
|
|
for _, id := range op.ids {
|
|
|
|
h.respWait[string(id)] = op
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// removeRequestOps stops waiting for the given request IDs.
|
|
|
|
func (h *handler) removeRequestOp(op *requestOp) {
|
|
|
|
for _, id := range op.ids {
|
|
|
|
delete(h.respWait, string(id))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// cancelAllRequests unblocks and removes pending requests and active subscriptions.
|
|
|
|
func (h *handler) cancelAllRequests(err error, inflightReq *requestOp) {
|
|
|
|
didClose := make(map[*requestOp]bool)
|
|
|
|
if inflightReq != nil {
|
|
|
|
didClose[inflightReq] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
for id, op := range h.respWait {
|
|
|
|
// Remove the op so that later calls will not close op.resp again.
|
|
|
|
delete(h.respWait, id)
|
|
|
|
|
|
|
|
if !didClose[op] {
|
|
|
|
op.err = err
|
|
|
|
close(op.resp)
|
|
|
|
didClose[op] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for id, sub := range h.clientSubs {
|
|
|
|
delete(h.clientSubs, id)
|
2021-03-30 18:09:30 +00:00
|
|
|
sub.close(err)
|
2019-02-04 12:47:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) addSubscriptions(nn []*Notifier) {
|
|
|
|
h.subLock.Lock()
|
|
|
|
defer h.subLock.Unlock()
|
|
|
|
|
|
|
|
for _, n := range nn {
|
|
|
|
if sub := n.takeSubscription(); sub != nil {
|
|
|
|
h.serverSubs[sub.ID] = sub
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// cancelServerSubscriptions removes all subscriptions and closes their error channels.
|
|
|
|
func (h *handler) cancelServerSubscriptions(err error) {
|
|
|
|
h.subLock.Lock()
|
|
|
|
defer h.subLock.Unlock()
|
|
|
|
|
|
|
|
for id, s := range h.serverSubs {
|
|
|
|
s.err <- err
|
|
|
|
close(s.err)
|
|
|
|
delete(h.serverSubs, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// startCallProc runs fn in a new goroutine and starts tracking it in the h.calls wait group.
|
|
|
|
func (h *handler) startCallProc(fn func(*callProc)) {
|
|
|
|
h.callWG.Add(1)
|
|
|
|
go func() {
|
|
|
|
ctx, cancel := context.WithCancel(h.rootCtx)
|
|
|
|
defer h.callWG.Done()
|
|
|
|
defer cancel()
|
|
|
|
fn(&callProc{ctx: ctx})
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
rpc: add limit for batch request items and response size (#26681)
This PR adds server-side limits for JSON-RPC batch requests. Before this change, batches
were limited only by processing time. The server would pick calls from the batch and
answer them until the response timeout occurred, then stop processing the remaining batch
items.
Here, we are adding two additional limits which can be configured:
- the 'item limit': batches can have at most N items
- the 'response size limit': batches can contain at most X response bytes
These limits are optional in package rpc. In Geth, we set a default limit of 1000 items
and 25MB response size.
When a batch goes over the limit, an error response is returned to the client. However,
doing this correctly isn't always possible. In JSON-RPC, only method calls with a valid
`id` can be responded to. Since batches may also contain non-call messages or
notifications, the best effort thing we can do to report an error with the batch itself is
reporting the limit violation as an error for the first method call in the batch. If a batch is
too large, but contains only notifications and responses, the error will be reported with
a null `id`.
The RPC client was also changed so it can deal with errors resulting from too large
batches. An older client connected to the server code in this PR could get stuck
until the request timeout occurred when the batch is too large. **Upgrading to a version
of the RPC client containing this change is strongly recommended to avoid timeout issues.**
For some weird reason, when writing the original client implementation, @fjl worked off of
the assumption that responses could be distributed across batches arbitrarily. So for a
batch request containing requests `[A B C]`, the server could respond with `[A B C]` but
also with `[A B] [C]` or even `[A] [B] [C]` and it wouldn't make a difference to the
client.
So in the implementation of BatchCallContext, the client waited for all requests in the
batch individually. If the server didn't respond to some of the requests in the batch, the
client would eventually just time out (if a context was used).
With the addition of batch limits into the server, we anticipate that people will hit this
kind of error way more often. To handle this properly, the client now waits for a single
response batch and expects it to contain all responses to the requests.
---------
Co-authored-by: Felix Lange <fjl@twurst.com>
Co-authored-by: Martin Holst Swende <martin@swende.se>
2023-06-13 11:38:58 +00:00
|
|
|
// handleResponse processes method call responses.
|
|
|
|
func (h *handler) handleResponses(batch []*jsonrpcMessage, handleCall func(*jsonrpcMessage)) {
|
|
|
|
var resolvedops []*requestOp
|
|
|
|
handleResp := func(msg *jsonrpcMessage) {
|
|
|
|
op := h.respWait[string(msg.ID)]
|
|
|
|
if op == nil {
|
|
|
|
h.log.Debug("Unsolicited RPC response", "reqid", idForLog{msg.ID})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
resolvedops = append(resolvedops, op)
|
|
|
|
delete(h.respWait, string(msg.ID))
|
|
|
|
|
|
|
|
// For subscription responses, start the subscription if the server
|
|
|
|
// indicates success. EthSubscribe gets unblocked in either case through
|
|
|
|
// the op.resp channel.
|
|
|
|
if op.sub != nil {
|
|
|
|
if msg.Error != nil {
|
|
|
|
op.err = msg.Error
|
|
|
|
} else {
|
|
|
|
op.err = json.Unmarshal(msg.Result, &op.sub.subid)
|
|
|
|
if op.err == nil {
|
|
|
|
go op.sub.run()
|
|
|
|
h.clientSubs[op.sub.subid] = op.sub
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !op.hadResponse {
|
|
|
|
op.hadResponse = true
|
|
|
|
op.resp <- batch
|
2019-02-04 12:47:34 +00:00
|
|
|
}
|
rpc: add limit for batch request items and response size (#26681)
This PR adds server-side limits for JSON-RPC batch requests. Before this change, batches
were limited only by processing time. The server would pick calls from the batch and
answer them until the response timeout occurred, then stop processing the remaining batch
items.
Here, we are adding two additional limits which can be configured:
- the 'item limit': batches can have at most N items
- the 'response size limit': batches can contain at most X response bytes
These limits are optional in package rpc. In Geth, we set a default limit of 1000 items
and 25MB response size.
When a batch goes over the limit, an error response is returned to the client. However,
doing this correctly isn't always possible. In JSON-RPC, only method calls with a valid
`id` can be responded to. Since batches may also contain non-call messages or
notifications, the best effort thing we can do to report an error with the batch itself is
reporting the limit violation as an error for the first method call in the batch. If a batch is
too large, but contains only notifications and responses, the error will be reported with
a null `id`.
The RPC client was also changed so it can deal with errors resulting from too large
batches. An older client connected to the server code in this PR could get stuck
until the request timeout occurred when the batch is too large. **Upgrading to a version
of the RPC client containing this change is strongly recommended to avoid timeout issues.**
For some weird reason, when writing the original client implementation, @fjl worked off of
the assumption that responses could be distributed across batches arbitrarily. So for a
batch request containing requests `[A B C]`, the server could respond with `[A B C]` but
also with `[A B] [C]` or even `[A] [B] [C]` and it wouldn't make a difference to the
client.
So in the implementation of BatchCallContext, the client waited for all requests in the
batch individually. If the server didn't respond to some of the requests in the batch, the
client would eventually just time out (if a context was used).
With the addition of batch limits into the server, we anticipate that people will hit this
kind of error way more often. To handle this properly, the client now waits for a single
response batch and expects it to contain all responses to the requests.
---------
Co-authored-by: Felix Lange <fjl@twurst.com>
Co-authored-by: Martin Holst Swende <martin@swende.se>
2023-06-13 11:38:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, msg := range batch {
|
|
|
|
start := time.Now()
|
|
|
|
switch {
|
|
|
|
case msg.isResponse():
|
|
|
|
handleResp(msg)
|
|
|
|
h.log.Trace("Handled RPC response", "reqid", idForLog{msg.ID}, "duration", time.Since(start))
|
|
|
|
|
|
|
|
case msg.isNotification():
|
|
|
|
if strings.HasSuffix(msg.Method, notificationMethodSuffix) {
|
|
|
|
h.handleSubscriptionResult(msg)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
handleCall(msg)
|
|
|
|
|
|
|
|
default:
|
|
|
|
handleCall(msg)
|
2019-02-04 12:47:34 +00:00
|
|
|
}
|
rpc: add limit for batch request items and response size (#26681)
This PR adds server-side limits for JSON-RPC batch requests. Before this change, batches
were limited only by processing time. The server would pick calls from the batch and
answer them until the response timeout occurred, then stop processing the remaining batch
items.
Here, we are adding two additional limits which can be configured:
- the 'item limit': batches can have at most N items
- the 'response size limit': batches can contain at most X response bytes
These limits are optional in package rpc. In Geth, we set a default limit of 1000 items
and 25MB response size.
When a batch goes over the limit, an error response is returned to the client. However,
doing this correctly isn't always possible. In JSON-RPC, only method calls with a valid
`id` can be responded to. Since batches may also contain non-call messages or
notifications, the best effort thing we can do to report an error with the batch itself is
reporting the limit violation as an error for the first method call in the batch. If a batch is
too large, but contains only notifications and responses, the error will be reported with
a null `id`.
The RPC client was also changed so it can deal with errors resulting from too large
batches. An older client connected to the server code in this PR could get stuck
until the request timeout occurred when the batch is too large. **Upgrading to a version
of the RPC client containing this change is strongly recommended to avoid timeout issues.**
For some weird reason, when writing the original client implementation, @fjl worked off of
the assumption that responses could be distributed across batches arbitrarily. So for a
batch request containing requests `[A B C]`, the server could respond with `[A B C]` but
also with `[A B] [C]` or even `[A] [B] [C]` and it wouldn't make a difference to the
client.
So in the implementation of BatchCallContext, the client waited for all requests in the
batch individually. If the server didn't respond to some of the requests in the batch, the
client would eventually just time out (if a context was used).
With the addition of batch limits into the server, we anticipate that people will hit this
kind of error way more often. To handle this properly, the client now waits for a single
response batch and expects it to contain all responses to the requests.
---------
Co-authored-by: Felix Lange <fjl@twurst.com>
Co-authored-by: Martin Holst Swende <martin@swende.se>
2023-06-13 11:38:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, op := range resolvedops {
|
|
|
|
h.removeRequestOp(op)
|
2019-02-04 12:47:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleSubscriptionResult processes subscription notifications.
|
|
|
|
func (h *handler) handleSubscriptionResult(msg *jsonrpcMessage) {
|
|
|
|
var result subscriptionResult
|
|
|
|
if err := json.Unmarshal(msg.Params, &result); err != nil {
|
|
|
|
h.log.Debug("Dropping invalid subscription message")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if h.clientSubs[result.ID] != nil {
|
|
|
|
h.clientSubs[result.ID].deliver(result.Result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleCallMsg executes a call message and returns the answer.
|
|
|
|
func (h *handler) handleCallMsg(ctx *callProc, msg *jsonrpcMessage) *jsonrpcMessage {
|
|
|
|
start := time.Now()
|
|
|
|
switch {
|
|
|
|
case msg.isNotification():
|
|
|
|
h.handleCall(ctx, msg)
|
2021-12-15 14:30:54 +00:00
|
|
|
h.log.Debug("Served "+msg.Method, "duration", time.Since(start))
|
2019-02-04 12:47:34 +00:00
|
|
|
return nil
|
rpc: add limit for batch request items and response size (#26681)
This PR adds server-side limits for JSON-RPC batch requests. Before this change, batches
were limited only by processing time. The server would pick calls from the batch and
answer them until the response timeout occurred, then stop processing the remaining batch
items.
Here, we are adding two additional limits which can be configured:
- the 'item limit': batches can have at most N items
- the 'response size limit': batches can contain at most X response bytes
These limits are optional in package rpc. In Geth, we set a default limit of 1000 items
and 25MB response size.
When a batch goes over the limit, an error response is returned to the client. However,
doing this correctly isn't always possible. In JSON-RPC, only method calls with a valid
`id` can be responded to. Since batches may also contain non-call messages or
notifications, the best effort thing we can do to report an error with the batch itself is
reporting the limit violation as an error for the first method call in the batch. If a batch is
too large, but contains only notifications and responses, the error will be reported with
a null `id`.
The RPC client was also changed so it can deal with errors resulting from too large
batches. An older client connected to the server code in this PR could get stuck
until the request timeout occurred when the batch is too large. **Upgrading to a version
of the RPC client containing this change is strongly recommended to avoid timeout issues.**
For some weird reason, when writing the original client implementation, @fjl worked off of
the assumption that responses could be distributed across batches arbitrarily. So for a
batch request containing requests `[A B C]`, the server could respond with `[A B C]` but
also with `[A B] [C]` or even `[A] [B] [C]` and it wouldn't make a difference to the
client.
So in the implementation of BatchCallContext, the client waited for all requests in the
batch individually. If the server didn't respond to some of the requests in the batch, the
client would eventually just time out (if a context was used).
With the addition of batch limits into the server, we anticipate that people will hit this
kind of error way more often. To handle this properly, the client now waits for a single
response batch and expects it to contain all responses to the requests.
---------
Co-authored-by: Felix Lange <fjl@twurst.com>
Co-authored-by: Martin Holst Swende <martin@swende.se>
2023-06-13 11:38:58 +00:00
|
|
|
|
2019-02-04 12:47:34 +00:00
|
|
|
case msg.isCall():
|
|
|
|
resp := h.handleCall(ctx, msg)
|
2020-06-08 08:09:49 +00:00
|
|
|
var ctx []interface{}
|
2021-12-15 14:30:54 +00:00
|
|
|
ctx = append(ctx, "reqid", idForLog{msg.ID}, "duration", time.Since(start))
|
2019-02-04 12:47:34 +00:00
|
|
|
if resp.Error != nil {
|
2020-06-08 08:09:49 +00:00
|
|
|
ctx = append(ctx, "err", resp.Error.Message)
|
|
|
|
if resp.Error.Data != nil {
|
|
|
|
ctx = append(ctx, "errdata", resp.Error.Data)
|
|
|
|
}
|
|
|
|
h.log.Warn("Served "+msg.Method, ctx...)
|
2019-02-04 12:47:34 +00:00
|
|
|
} else {
|
2020-06-08 08:09:49 +00:00
|
|
|
h.log.Debug("Served "+msg.Method, ctx...)
|
2019-02-04 12:47:34 +00:00
|
|
|
}
|
|
|
|
return resp
|
rpc: add limit for batch request items and response size (#26681)
This PR adds server-side limits for JSON-RPC batch requests. Before this change, batches
were limited only by processing time. The server would pick calls from the batch and
answer them until the response timeout occurred, then stop processing the remaining batch
items.
Here, we are adding two additional limits which can be configured:
- the 'item limit': batches can have at most N items
- the 'response size limit': batches can contain at most X response bytes
These limits are optional in package rpc. In Geth, we set a default limit of 1000 items
and 25MB response size.
When a batch goes over the limit, an error response is returned to the client. However,
doing this correctly isn't always possible. In JSON-RPC, only method calls with a valid
`id` can be responded to. Since batches may also contain non-call messages or
notifications, the best effort thing we can do to report an error with the batch itself is
reporting the limit violation as an error for the first method call in the batch. If a batch is
too large, but contains only notifications and responses, the error will be reported with
a null `id`.
The RPC client was also changed so it can deal with errors resulting from too large
batches. An older client connected to the server code in this PR could get stuck
until the request timeout occurred when the batch is too large. **Upgrading to a version
of the RPC client containing this change is strongly recommended to avoid timeout issues.**
For some weird reason, when writing the original client implementation, @fjl worked off of
the assumption that responses could be distributed across batches arbitrarily. So for a
batch request containing requests `[A B C]`, the server could respond with `[A B C]` but
also with `[A B] [C]` or even `[A] [B] [C]` and it wouldn't make a difference to the
client.
So in the implementation of BatchCallContext, the client waited for all requests in the
batch individually. If the server didn't respond to some of the requests in the batch, the
client would eventually just time out (if a context was used).
With the addition of batch limits into the server, we anticipate that people will hit this
kind of error way more often. To handle this properly, the client now waits for a single
response batch and expects it to contain all responses to the requests.
---------
Co-authored-by: Felix Lange <fjl@twurst.com>
Co-authored-by: Martin Holst Swende <martin@swende.se>
2023-06-13 11:38:58 +00:00
|
|
|
|
2019-02-04 12:47:34 +00:00
|
|
|
case msg.hasValidID():
|
|
|
|
return msg.errorResponse(&invalidRequestError{"invalid request"})
|
rpc: add limit for batch request items and response size (#26681)
This PR adds server-side limits for JSON-RPC batch requests. Before this change, batches
were limited only by processing time. The server would pick calls from the batch and
answer them until the response timeout occurred, then stop processing the remaining batch
items.
Here, we are adding two additional limits which can be configured:
- the 'item limit': batches can have at most N items
- the 'response size limit': batches can contain at most X response bytes
These limits are optional in package rpc. In Geth, we set a default limit of 1000 items
and 25MB response size.
When a batch goes over the limit, an error response is returned to the client. However,
doing this correctly isn't always possible. In JSON-RPC, only method calls with a valid
`id` can be responded to. Since batches may also contain non-call messages or
notifications, the best effort thing we can do to report an error with the batch itself is
reporting the limit violation as an error for the first method call in the batch. If a batch is
too large, but contains only notifications and responses, the error will be reported with
a null `id`.
The RPC client was also changed so it can deal with errors resulting from too large
batches. An older client connected to the server code in this PR could get stuck
until the request timeout occurred when the batch is too large. **Upgrading to a version
of the RPC client containing this change is strongly recommended to avoid timeout issues.**
For some weird reason, when writing the original client implementation, @fjl worked off of
the assumption that responses could be distributed across batches arbitrarily. So for a
batch request containing requests `[A B C]`, the server could respond with `[A B C]` but
also with `[A B] [C]` or even `[A] [B] [C]` and it wouldn't make a difference to the
client.
So in the implementation of BatchCallContext, the client waited for all requests in the
batch individually. If the server didn't respond to some of the requests in the batch, the
client would eventually just time out (if a context was used).
With the addition of batch limits into the server, we anticipate that people will hit this
kind of error way more often. To handle this properly, the client now waits for a single
response batch and expects it to contain all responses to the requests.
---------
Co-authored-by: Felix Lange <fjl@twurst.com>
Co-authored-by: Martin Holst Swende <martin@swende.se>
2023-06-13 11:38:58 +00:00
|
|
|
|
2019-02-04 12:47:34 +00:00
|
|
|
default:
|
|
|
|
return errorMessage(&invalidRequestError{"invalid request"})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleCall processes method calls.
|
|
|
|
func (h *handler) handleCall(cp *callProc, msg *jsonrpcMessage) *jsonrpcMessage {
|
|
|
|
if msg.isSubscribe() {
|
|
|
|
return h.handleSubscribe(cp, msg)
|
|
|
|
}
|
|
|
|
var callb *callback
|
|
|
|
if msg.isUnsubscribe() {
|
|
|
|
callb = h.unsubscribeCb
|
|
|
|
} else {
|
|
|
|
callb = h.reg.callback(msg.Method)
|
|
|
|
}
|
|
|
|
if callb == nil {
|
|
|
|
return msg.errorResponse(&methodNotFoundError{method: msg.Method})
|
|
|
|
}
|
rpc: add limit for batch request items and response size (#26681)
This PR adds server-side limits for JSON-RPC batch requests. Before this change, batches
were limited only by processing time. The server would pick calls from the batch and
answer them until the response timeout occurred, then stop processing the remaining batch
items.
Here, we are adding two additional limits which can be configured:
- the 'item limit': batches can have at most N items
- the 'response size limit': batches can contain at most X response bytes
These limits are optional in package rpc. In Geth, we set a default limit of 1000 items
and 25MB response size.
When a batch goes over the limit, an error response is returned to the client. However,
doing this correctly isn't always possible. In JSON-RPC, only method calls with a valid
`id` can be responded to. Since batches may also contain non-call messages or
notifications, the best effort thing we can do to report an error with the batch itself is
reporting the limit violation as an error for the first method call in the batch. If a batch is
too large, but contains only notifications and responses, the error will be reported with
a null `id`.
The RPC client was also changed so it can deal with errors resulting from too large
batches. An older client connected to the server code in this PR could get stuck
until the request timeout occurred when the batch is too large. **Upgrading to a version
of the RPC client containing this change is strongly recommended to avoid timeout issues.**
For some weird reason, when writing the original client implementation, @fjl worked off of
the assumption that responses could be distributed across batches arbitrarily. So for a
batch request containing requests `[A B C]`, the server could respond with `[A B C]` but
also with `[A B] [C]` or even `[A] [B] [C]` and it wouldn't make a difference to the
client.
So in the implementation of BatchCallContext, the client waited for all requests in the
batch individually. If the server didn't respond to some of the requests in the batch, the
client would eventually just time out (if a context was used).
With the addition of batch limits into the server, we anticipate that people will hit this
kind of error way more often. To handle this properly, the client now waits for a single
response batch and expects it to contain all responses to the requests.
---------
Co-authored-by: Felix Lange <fjl@twurst.com>
Co-authored-by: Martin Holst Swende <martin@swende.se>
2023-06-13 11:38:58 +00:00
|
|
|
|
2019-02-04 12:47:34 +00:00
|
|
|
args, err := parsePositionalArguments(msg.Params, callb.argTypes)
|
|
|
|
if err != nil {
|
|
|
|
return msg.errorResponse(&invalidParamsError{err.Error()})
|
|
|
|
}
|
2021-09-16 18:55:39 +00:00
|
|
|
|
2022-03-29 03:02:51 +00:00
|
|
|
//begin PluGeth code injection
|
2021-09-16 18:55:39 +00:00
|
|
|
pluginGetRPCCalls(string(msg.ID), string(msg.Method), string(msg.Params))
|
2022-03-29 03:02:51 +00:00
|
|
|
//begin PluGeth code injection
|
2020-04-03 10:36:44 +00:00
|
|
|
start := time.Now()
|
|
|
|
answer := h.runMethod(cp.ctx, msg, callb, args)
|
rpc: add limit for batch request items and response size (#26681)
This PR adds server-side limits for JSON-RPC batch requests. Before this change, batches
were limited only by processing time. The server would pick calls from the batch and
answer them until the response timeout occurred, then stop processing the remaining batch
items.
Here, we are adding two additional limits which can be configured:
- the 'item limit': batches can have at most N items
- the 'response size limit': batches can contain at most X response bytes
These limits are optional in package rpc. In Geth, we set a default limit of 1000 items
and 25MB response size.
When a batch goes over the limit, an error response is returned to the client. However,
doing this correctly isn't always possible. In JSON-RPC, only method calls with a valid
`id` can be responded to. Since batches may also contain non-call messages or
notifications, the best effort thing we can do to report an error with the batch itself is
reporting the limit violation as an error for the first method call in the batch. If a batch is
too large, but contains only notifications and responses, the error will be reported with
a null `id`.
The RPC client was also changed so it can deal with errors resulting from too large
batches. An older client connected to the server code in this PR could get stuck
until the request timeout occurred when the batch is too large. **Upgrading to a version
of the RPC client containing this change is strongly recommended to avoid timeout issues.**
For some weird reason, when writing the original client implementation, @fjl worked off of
the assumption that responses could be distributed across batches arbitrarily. So for a
batch request containing requests `[A B C]`, the server could respond with `[A B C]` but
also with `[A B] [C]` or even `[A] [B] [C]` and it wouldn't make a difference to the
client.
So in the implementation of BatchCallContext, the client waited for all requests in the
batch individually. If the server didn't respond to some of the requests in the batch, the
client would eventually just time out (if a context was used).
With the addition of batch limits into the server, we anticipate that people will hit this
kind of error way more often. To handle this properly, the client now waits for a single
response batch and expects it to contain all responses to the requests.
---------
Co-authored-by: Felix Lange <fjl@twurst.com>
Co-authored-by: Martin Holst Swende <martin@swende.se>
2023-06-13 11:38:58 +00:00
|
|
|
|
2020-04-03 10:36:44 +00:00
|
|
|
// Collect the statistics for RPC calls if metrics is enabled.
|
|
|
|
// We only care about pure rpc call. Filter out subscription.
|
|
|
|
if callb != h.unsubscribeCb {
|
|
|
|
rpcRequestGauge.Inc(1)
|
|
|
|
if answer.Error != nil {
|
2022-04-05 07:45:20 +00:00
|
|
|
failedRequestGauge.Inc(1)
|
2020-04-03 10:36:44 +00:00
|
|
|
} else {
|
|
|
|
successfulRequestGauge.Inc(1)
|
|
|
|
}
|
|
|
|
rpcServingTimer.UpdateSince(start)
|
2022-06-08 13:24:33 +00:00
|
|
|
updateServeTimeHistogram(msg.Method, answer.Error == nil, time.Since(start))
|
2020-04-03 10:36:44 +00:00
|
|
|
}
|
rpc: add limit for batch request items and response size (#26681)
This PR adds server-side limits for JSON-RPC batch requests. Before this change, batches
were limited only by processing time. The server would pick calls from the batch and
answer them until the response timeout occurred, then stop processing the remaining batch
items.
Here, we are adding two additional limits which can be configured:
- the 'item limit': batches can have at most N items
- the 'response size limit': batches can contain at most X response bytes
These limits are optional in package rpc. In Geth, we set a default limit of 1000 items
and 25MB response size.
When a batch goes over the limit, an error response is returned to the client. However,
doing this correctly isn't always possible. In JSON-RPC, only method calls with a valid
`id` can be responded to. Since batches may also contain non-call messages or
notifications, the best effort thing we can do to report an error with the batch itself is
reporting the limit violation as an error for the first method call in the batch. If a batch is
too large, but contains only notifications and responses, the error will be reported with
a null `id`.
The RPC client was also changed so it can deal with errors resulting from too large
batches. An older client connected to the server code in this PR could get stuck
until the request timeout occurred when the batch is too large. **Upgrading to a version
of the RPC client containing this change is strongly recommended to avoid timeout issues.**
For some weird reason, when writing the original client implementation, @fjl worked off of
the assumption that responses could be distributed across batches arbitrarily. So for a
batch request containing requests `[A B C]`, the server could respond with `[A B C]` but
also with `[A B] [C]` or even `[A] [B] [C]` and it wouldn't make a difference to the
client.
So in the implementation of BatchCallContext, the client waited for all requests in the
batch individually. If the server didn't respond to some of the requests in the batch, the
client would eventually just time out (if a context was used).
With the addition of batch limits into the server, we anticipate that people will hit this
kind of error way more often. To handle this properly, the client now waits for a single
response batch and expects it to contain all responses to the requests.
---------
Co-authored-by: Felix Lange <fjl@twurst.com>
Co-authored-by: Martin Holst Swende <martin@swende.se>
2023-06-13 11:38:58 +00:00
|
|
|
|
2020-04-03 10:36:44 +00:00
|
|
|
return answer
|
2019-02-04 12:47:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// handleSubscribe processes *_subscribe method calls.
|
|
|
|
func (h *handler) handleSubscribe(cp *callProc, msg *jsonrpcMessage) *jsonrpcMessage {
|
|
|
|
if !h.allowSubscribe {
|
2023-06-14 12:04:41 +00:00
|
|
|
return msg.errorResponse(ErrNotificationsUnsupported)
|
2019-02-04 12:47:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Subscription method name is first argument.
|
|
|
|
name, err := parseSubscriptionName(msg.Params)
|
|
|
|
if err != nil {
|
|
|
|
return msg.errorResponse(&invalidParamsError{err.Error()})
|
|
|
|
}
|
|
|
|
namespace := msg.namespace()
|
|
|
|
callb := h.reg.subscription(namespace, name)
|
|
|
|
if callb == nil {
|
|
|
|
return msg.errorResponse(&subscriptionNotFoundError{namespace, name})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse subscription name arg too, but remove it before calling the callback.
|
|
|
|
argTypes := append([]reflect.Type{stringType}, callb.argTypes...)
|
|
|
|
args, err := parsePositionalArguments(msg.Params, argTypes)
|
|
|
|
if err != nil {
|
|
|
|
return msg.errorResponse(&invalidParamsError{err.Error()})
|
|
|
|
}
|
|
|
|
args = args[1:]
|
|
|
|
|
|
|
|
// Install notifier in context so the subscription handler can find it.
|
|
|
|
n := &Notifier{h: h, namespace: namespace}
|
|
|
|
cp.notifiers = append(cp.notifiers, n)
|
|
|
|
ctx := context.WithValue(cp.ctx, notifierKey{}, n)
|
|
|
|
|
|
|
|
return h.runMethod(ctx, msg, callb, args)
|
|
|
|
}
|
|
|
|
|
|
|
|
// runMethod runs the Go callback for an RPC method.
|
|
|
|
func (h *handler) runMethod(ctx context.Context, msg *jsonrpcMessage, callb *callback, args []reflect.Value) *jsonrpcMessage {
|
|
|
|
result, err := callb.call(ctx, msg.Method, args)
|
|
|
|
if err != nil {
|
|
|
|
return msg.errorResponse(err)
|
|
|
|
}
|
|
|
|
return msg.response(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
// unsubscribe is the callback function for all *_unsubscribe calls.
|
|
|
|
func (h *handler) unsubscribe(ctx context.Context, id ID) (bool, error) {
|
|
|
|
h.subLock.Lock()
|
|
|
|
defer h.subLock.Unlock()
|
|
|
|
|
|
|
|
s := h.serverSubs[id]
|
|
|
|
if s == nil {
|
|
|
|
return false, ErrSubscriptionNotFound
|
|
|
|
}
|
|
|
|
close(s.err)
|
|
|
|
delete(h.serverSubs, id)
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type idForLog struct{ json.RawMessage }
|
|
|
|
|
|
|
|
func (id idForLog) String() string {
|
|
|
|
if s, err := strconv.Unquote(string(id.RawMessage)); err == nil {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
return string(id.RawMessage)
|
|
|
|
}
|