Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: exclude context canccel error from forward error counting #98

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions proxyd/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,8 @@ func (b *Backend) Forward(ctx context.Context, reqs []*RPCReq, isBatch bool) ([]
res, err := b.doForward(ctx, reqs, isBatch)
switch err {
case nil: // do nothing
case context.Canceled:
return nil, err
case ErrBackendResponseTooLarge:
log.Warn(
"backend response too large",
Expand Down Expand Up @@ -583,6 +585,10 @@ func (b *Backend) doForward(ctx context.Context, rpcReqs []*RPCReq, isBatch bool
start := time.Now()
httpRes, err := b.client.DoLimited(httpReq)
if err != nil {
// if it's canceld, we don't want to count it as an error
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit; canceld -> canceled

if errors.Is(err, context.Canceled) {
return nil, err
}
Comment on lines +589 to +591
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to add a metric for canceled contexts here?

b.intermittentErrorsSlidingWindow.Incr()
RecordBackendNetworkErrorRateSlidingWindow(b, b.ErrorRate())
return nil, wrapErr(err, "error in backend request")
Expand Down Expand Up @@ -1327,6 +1333,9 @@ type LimitedHTTPClient struct {

func (c *LimitedHTTPClient) DoLimited(req *http.Request) (*http.Response, error) {
if err := c.sem.Acquire(req.Context(), 1); err != nil {
if errors.Is(err, context.Canceled) {
return nil, err
}
tooManyRequestErrorsTotal.WithLabelValues(c.backendName).Inc()
return nil, wrapErr(err, "too many requests")
}
Expand Down Expand Up @@ -1406,6 +1415,14 @@ func (bg *BackendGroup) ForwardRequestToBackendGroup(
if len(rpcReqs) > 0 {

res, err = back.Forward(ctx, rpcReqs, isBatch)
if err != nil && errors.Is(err, context.Canceled) {
log.Info("context canceled", "req_id", GetReqID(ctx), "auth", GetAuthCtx(ctx))
return &BackendGroupRPCResponse{
RPCRes: nil,
ServedBy: "",
error: err,
}
}

if errors.Is(err, ErrConsensusGetReceiptsCantBeBatched) ||
errors.Is(err, ErrConsensusGetReceiptsInvalidTarget) ||
Expand Down
22 changes: 22 additions & 0 deletions proxyd/backend_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package proxyd

import (
"context"
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
"golang.org/x/sync/semaphore"
)

func TestStripXFF(t *testing.T) {
Expand All @@ -20,3 +23,22 @@ func TestStripXFF(t *testing.T) {
assert.Equal(t, test.out, actual)
}
}

func TestForwardContextCanceled(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, would it be possible to add a few more tests and cases such as incrementing the intermittentErrorsSlidingWindow first and then checking that they are not incremented again.

Maybe asset that the debug log exists too in the case of context canceled.

Also test the multicall routing strategy

ctx, cancel := context.WithCancel(context.Background())
cancel()
sem := semaphore.NewWeighted(100)
backend := NewBackend(
"test",
"http://localhost:8545",
"ws://localhost:8545",
sem,
)
_, err := backend.Forward(ctx, []*RPCReq{
{JSONRPC: "2.0", Method: "eth_blockNumber", ID: json.RawMessage("1")},
}, true)
assert.ErrorIs(t, context.Canceled, err)
assert.Equal(t, uint(1), backend.networkRequestsSlidingWindow.Count())
assert.Equal(t, uint(0), backend.intermittentErrorsSlidingWindow.Count())
assert.Zero(t, backend.ErrorRate())
}