Skip to content
Open
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
12 changes: 9 additions & 3 deletions pkg/sync/sync_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,11 +436,14 @@ func (syncService *SyncService[H]) initFromP2PWithRetry(ctx context.Context, pee
return true, nil
}

// block with exponential backoff until initialization succeeds or context is canceled.
// block with exponential backoff until initialization succeeds, context is canceled, or timeout.
// If timeout is reached, we return nil to allow startup to continue - DA sync will
// provide headers and WriteToStoreAndBroadcast will lazily initialize the store/syncer.
backoff := 1 * time.Second
maxBackoff := 10 * time.Second

timeoutTimer := time.NewTimer(time.Minute * 10)
p2pInitTimeout := 30 * time.Second
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While extracting the timeout to a variable p2pInitTimeout is a good step for readability, it would be better to define it as a package-level constant. This avoids having a 'magic number' inside the function body and makes it easier to find and modify in the future.

You could add this at the top of the file:

const p2pInitTimeout = 30 * time.Second

Then you could use this constant directly in time.NewTimer and in the logger call, removing the local variable.

timeoutTimer := time.NewTimer(p2pInitTimeout)
defer timeoutTimer.Stop()

for {
Expand All @@ -455,7 +458,10 @@ func (syncService *SyncService[H]) initFromP2PWithRetry(ctx context.Context, pee
case <-ctx.Done():
return ctx.Err()
case <-timeoutTimer.C:
return fmt.Errorf("timeout reached while trying to initialize the store after 10 minutes: %w", err)
syncService.logger.Warn().
Dur("timeout", p2pInitTimeout).
Msg("P2P header sync initialization timed out, deferring to DA sync")
return nil
case <-time.After(backoff):
}

Expand Down
Loading