-
Notifications
You must be signed in to change notification settings - Fork 30
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
base: main
Are you sure you want to change the base?
Changes from all commits
f09d6e0
076b7e9
5c3ed88
47e99bf
92f1e20
858c4c5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", | ||
|
@@ -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 | ||
if errors.Is(err, context.Canceled) { | ||
return nil, err | ||
} | ||
Comment on lines
+589
to
+591
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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") | ||
|
@@ -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, | ||
|
@@ -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") | ||
} | ||
|
@@ -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) || | ||
|
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) { | ||
|
@@ -20,3 +26,34 @@ func TestStripXFF(t *testing.T) { | |
assert.Equal(t, test.out, actual) | ||
} | ||
} | ||
|
||
func TestForwardContextCanceled(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit; canceld -> canceled