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 all 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
24 changes: 23 additions & 1 deletion 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 @@ -798,7 +804,12 @@ func (bg *BackendGroup) Forward(ctx context.Context, rpcReqs []*RPCReq, isBatch
backendResp := <-ch

if backendResp.error != nil {
log.Error("error serving requests",
logfn := log.Error
// If the context was canceled, downgrade the log level to debug.
if errors.Is(backendResp.error, context.Canceled) {
logfn = log.Debug
}
logfn("error serving requests",
"req_id", GetReqID(ctx),
"auth", GetAuthCtx(ctx),
"err", backendResp.error,
Expand Down Expand Up @@ -1327,6 +1338,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 +1420,14 @@ func (bg *BackendGroup) ForwardRequestToBackendGroup(
if len(rpcReqs) > 0 {

res, err = back.Forward(ctx, rpcReqs, isBatch)
if 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
37 changes: 37 additions & 0 deletions proxyd/backend_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package proxyd

import (
"bytes"
"context"
"encoding/json"
"log/slog"
"testing"

"github.com/ethereum/go-ethereum/log"
"github.com/stretchr/testify/assert"
"golang.org/x/sync/semaphore"
)

func TestStripXFF(t *testing.T) {
Expand All @@ -20,3 +26,34 @@ 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

buf := bytes.NewBuffer(nil)
log.SetDefault(log.NewLogger(slog.NewTextHandler(buf, &slog.HandlerOptions{
Level: slog.LevelDebug.Level(),
})))
ctx, cancel := context.WithCancel(context.Background())
cancel()
sem := semaphore.NewWeighted(100)
backend := NewBackend(
"test",
"http://localhost:8545",
"ws://localhost:8545",
sem,
)
backendGroup := &BackendGroup{
Name: "testgroup",
Backends: []*Backend{backend},
routingStrategy: "fallback",
}
_, _, err := backendGroup.Forward(ctx, []*RPCReq{
{JSONRPC: "2.0", Method: "eth_blockNumber", ID: json.RawMessage("1")},
}, true)
assert.ErrorIs(t, context.Canceled, err)
assert.Equalf(t, uint(1), backend.networkRequestsSlidingWindow.Count(), "exact 1 network request should be counted")
assert.Equalf(t, uint(0), backend.intermittentErrorsSlidingWindow.Count(), "no intermittent errors should be counted")
assert.Zerof(t, backend.ErrorRate(), "error rate should be zero")
logs := buf.String()
assert.NotZero(t, logs)
assert.NotContainsf(t, logs, "level=ERROR", "context canceled error should not be logged as a ERROR")
}
7 changes: 6 additions & 1 deletion proxyd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,12 @@ func (s *Server) handleBatchRPC(ctx context.Context, reqs []json.RawMessage, isL
errors.Is(err, ErrConsensusGetReceiptsInvalidTarget) {
return nil, false, "", err
}
log.Error(
logfn := log.Error
// If the context was canceled, downgrade the log level to debug.
if errors.Is(err, context.Canceled) {
logfn = log.Debug
}
logfn(
"error forwarding RPC batch",
"batch_size", len(elems),
"backend_group", group,
Expand Down