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

misc(syncer): make timeout configurable #162

Closed
Closed
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
18 changes: 17 additions & 1 deletion sync/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,22 @@ type Parameters struct {
// considered "recent". The default is blockTime + 5 seconds.
recencyThreshold time.Duration
// metrics is a flag that enables metrics collection.
metrics bool
// maxAwait is the maximum duration to wait for a new header to be received.
//
// This parameter defines the upper bound on the time the system is willing
// to wait for a header before considering the possibility of a network or
// validator issue. It acts as a safeguard against indefinite waiting periods
// and helps maintain the responsiveness of the system.
// default value is set to 2 seconds.
maxAwait time.Duration
metrics bool
}

// DefaultParameters returns the default params to configure the syncer.
func DefaultParameters() Parameters {
return Parameters{
TrustingPeriod: 336 * time.Hour, // tendermint's default trusting period
maxAwait: 2 * time.Second, // default maxAwait value
}
}

Expand Down Expand Up @@ -77,6 +86,13 @@ func WithTrustingPeriod(duration time.Duration) Option {
}
}

// WithMaxAwait is a functional option that configures the `maxAwait` parameter.
func WithMaxAwait(duration time.Duration) Option {
return func(p *Parameters) {
p.maxAwait = duration
}
}

// WithParams is a functional option that overrides Parameters.
func WithParams(new Parameters) Option {
return func(old *Parameters) {
Expand Down
3 changes: 2 additions & 1 deletion sync/sync_head.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ func (s *Syncer[H]) Head(ctx context.Context, _ ...header.HeadOption[H]) (H, err
return s.Head(ctx)
}
defer s.getter.Unlock()

// limit time to get a recent header
// if we can't get it - give what we have
reqCtx, cancel := context.WithTimeout(ctx, time.Second*2) // TODO(@vgonkivs): make timeout configurable
reqCtx, cancel := context.WithTimeout(ctx, s.Params.maxAwait)
defer cancel()
s.metrics.unrecentHead(s.ctx)
netHead, err := s.getter.Head(reqCtx, header.WithTrustedHead[H](sbjHead))
Expand Down