Skip to content

Commit aa5a016

Browse files
vgonkivsWondertan
andauthored
fix(syncer): set timeout for head request (#152)
Co-authored-by: Hlib Kanunnikov <[email protected]>
1 parent c34c25d commit aa5a016

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

sync/sync_head.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ import (
1111
// Head returns the Network Head.
1212
//
1313
// Known subjective head is considered network head if it is recent enough(now-timestamp<=blocktime)
14-
// Otherwise, head is requested from a trusted peer and
14+
// Otherwise, we attempt to request recent network head from a trusted peer and
1515
// set as the new subjective head, assuming that trusted peer is always fully synced.
16+
//
17+
// The request is limited with 2 seconds and otherwise potentially unrecent header is returned.
1618
func (s *Syncer[H]) Head(ctx context.Context, _ ...header.HeadOption[H]) (H, error) {
1719
sbjHead, err := s.subjectiveHead(ctx)
1820
if err != nil {
@@ -23,12 +25,7 @@ func (s *Syncer[H]) Head(ctx context.Context, _ ...header.HeadOption[H]) (H, err
2325
return sbjHead, nil
2426
}
2527
// otherwise, request head from the network
26-
//
27-
// TODO(@Wondertan): Here is another potential networking optimization:
28-
// * From sbjHead's timestamp and current time predict the time to the next header(TNH)
29-
// * If now >= TNH && now <= TNH + (THP) header propagation time
30-
// * Wait for header to arrive instead of requesting it
31-
// * This way we don't request as we know the new network header arrives exactly
28+
// TODO: Besides requesting we should listen for new gossiped headers and cancel request if so
3229
//
3330
// single-flight protection
3431
// ensure only one Head is requested at the time
@@ -38,7 +35,11 @@ func (s *Syncer[H]) Head(ctx context.Context, _ ...header.HeadOption[H]) (H, err
3835
return s.Head(ctx)
3936
}
4037
defer s.getter.Unlock()
41-
netHead, err := s.getter.Head(ctx, header.WithTrustedHead[H](sbjHead))
38+
// limit time to get a recent header
39+
// if we can't get it - give what we have
40+
reqCtx, cancel := context.WithTimeout(ctx, time.Second*2) // TODO(@vgonkivs): make timeout configurable
41+
defer cancel()
42+
netHead, err := s.getter.Head(reqCtx, header.WithTrustedHead[H](sbjHead))
4243
if err != nil {
4344
log.Warnw("failed to get recent head, returning current subjective", "sbjHead", sbjHead.Height(), "err", err)
4445
return s.subjectiveHead(ctx)

0 commit comments

Comments
 (0)