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(getter/cascade): cascade getter should return fast on context cancel #2310

Merged
Merged
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: 9 additions & 8 deletions share/getters/cascade.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/hex"
"errors"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"

Expand Down Expand Up @@ -122,16 +121,18 @@ func cascadeGetters[V any](
getCtx, cancel := ctxWithSplitTimeout(ctx, len(getters)-i, 0)
val, getErr := get(getCtx, getter)
cancel()
if getErr == nil {
return val, nil
if getErr == nil || errors.Is(getErr, share.ErrNamespaceNotFound) {
return val, getErr
}
if errors.Is(share.ErrNamespaceNotFound, getErr) {
return zero, getErr

if errors.Is(getErr, errOperationNotSupported) {
continue
}

if !errors.Is(getErr, errOperationNotSupported) {
err = errors.Join(err, getErr)
span.RecordError(getErr, trace.WithAttributes(attribute.Int("getter_idx", i)))
err = errors.Join(err, getErr)
span.RecordError(getErr, trace.WithAttributes(attribute.Int("getter_idx", i)))
if ctx.Err() != nil {
Wondertan marked this conversation as resolved.
Show resolved Hide resolved
return zero, err
walldiss marked this conversation as resolved.
Show resolved Hide resolved
}
}
return zero, err
Expand Down
14 changes: 14 additions & 0 deletions share/getters/cascade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func TestCascade(t *testing.T) {
timeoutGetter := mocks.NewMockGetter(ctrl)
immediateFailGetter := mocks.NewMockGetter(ctrl)
successGetter := mocks.NewMockGetter(ctrl)
ctxGetter := mocks.NewMockGetter(ctrl)
timeoutGetter.EXPECT().GetEDS(gomock.Any(), gomock.Any()).
DoAndReturn(func(ctx context.Context, _ *share.Root) (*rsmt2d.ExtendedDataSquare, error) {
return nil, context.DeadlineExceeded
Expand All @@ -60,6 +61,10 @@ func TestCascade(t *testing.T) {
Return(nil, errors.New("second getter fails immediately")).AnyTimes()
successGetter.EXPECT().GetEDS(gomock.Any(), gomock.Any()).
Return(nil, nil).AnyTimes()
ctxGetter.EXPECT().GetEDS(gomock.Any(), gomock.Any()).
DoAndReturn(func(ctx context.Context, _ *share.Root) (*rsmt2d.ExtendedDataSquare, error) {
return nil, ctx.Err()
}).AnyTimes()

get := func(ctx context.Context, get share.Getter) (*rsmt2d.ExtendedDataSquare, error) {
return get.GetEDS(ctx, nil)
Expand Down Expand Up @@ -96,6 +101,15 @@ func TestCascade(t *testing.T) {
assert.Equal(t, strings.Count(err.Error(), "\n"), 2)
})

t.Run("Context Canceled", func(t *testing.T) {
ctx, cancel := context.WithCancel(ctx)
cancel()
getters := []share.Getter{ctxGetter, ctxGetter, ctxGetter}
_, err := cascadeGetters(ctx, getters, get)
assert.Error(t, err)
assert.Equal(t, strings.Count(err.Error(), "\n"), 0)
})

t.Run("Single", func(t *testing.T) {
getters := []share.Getter{successGetter}
_, err := cascadeGetters(ctx, getters, get)
Expand Down