Skip to content

Commit

Permalink
fix(share/getters): set minTimeout (celestiaorg#3591)
Browse files Browse the repository at this point in the history
  • Loading branch information
vgonkivs authored Aug 6, 2024
1 parent 2ff58bf commit 5117b5a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
11 changes: 10 additions & 1 deletion share/getters/cascade.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package getters
import (
"context"
"errors"
"time"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
Expand Down Expand Up @@ -119,13 +120,21 @@ func cascadeGetters[V any](
}
}()

minTimeout := time.Duration(0)
_, ok := ctx.Deadline()
if !ok {
// in this case minTimeout will be applied for all getters,so each of them
// will have 1 minute timeout.
minTimeout = time.Minute
}

for i, getter := range getters {
log.Debugf("cascade: launching getter #%d", i)
span.AddEvent("getter launched", trace.WithAttributes(attribute.Int("getter_idx", i)))

// we split the timeout between left getters
// once async cascadegetter is implemented, we can remove this
getCtx, cancel := ctxWithSplitTimeout(ctx, len(getters)-i, 0)
getCtx, cancel := ctxWithSplitTimeout(ctx, len(getters)-i, minTimeout)
val, getErr := get(getCtx, getter)
cancel()
if getErr == nil {
Expand Down
13 changes: 13 additions & 0 deletions share/getters/cascade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ func TestCascade(t *testing.T) {
return nil, ctx.Err()
}).AnyTimes()

stuckGetter := mocks.NewMockGetter(ctrl)
stuckGetter.EXPECT().GetEDS(gomock.Any(), gomock.Any()).
DoAndReturn(func(ctx context.Context, _ *header.ExtendedHeader) (*rsmt2d.ExtendedDataSquare, error) {
<-ctx.Done()
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 @@ -116,4 +123,10 @@ func TestCascade(t *testing.T) {
_, err := cascadeGetters(ctx, getters, get)
assert.NoError(t, err)
})

t.Run("Stuck getter", func(t *testing.T) {
getters := []share.Getter{stuckGetter, successGetter}
_, err := cascadeGetters(ctx, getters, get)
assert.NoError(t, err)
})
}

0 comments on commit 5117b5a

Please sign in to comment.