Skip to content

Commit

Permalink
fix: add 1s to context.WithTimeout
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-ramos committed Oct 21, 2024
1 parent 38be0dc commit 3c4e40c
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions waku/v2/api/history/cycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,14 +409,10 @@ func (m *StorenodeCycle) SetStorenodeConfigProvider(provider StorenodeConfigProv
m.storenodeConfigProvider = provider
}

func (m *StorenodeCycle) WaitForAvailableStoreNode(ctx context.Context, timeout time.Duration) bool {
// Add 1 second to timeout, because the storenode cycle has 1 second ticker, which doesn't tick on start.
func (m *StorenodeCycle) WaitForAvailableStoreNode(ctx context.Context) bool {
// Note: Add 1 second to timeout, because the storenode cycle has 1 second ticker, which doesn't tick on start.
// This can be improved after merging https://github.com/status-im/status-go/pull/4380.
// NOTE: https://stackoverflow.com/questions/32705582/how-to-get-time-tick-to-tick-immediately
timeout += time.Second

ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()

wg := sync.WaitGroup{}
wg.Add(1)
Expand All @@ -426,14 +422,30 @@ func (m *StorenodeCycle) WaitForAvailableStoreNode(ctx context.Context, timeout
select {
case <-m.StorenodeAvailableOneshotEmitter.Subscribe():
case <-ctx.Done():
if errors.Is(ctx.Err(), context.Canceled) {
return
}

// Wait for an additional second, but handle cancellation
select {
case <-time.After(1 * time.Second):
case <-ctx.Done(): // context was cancelled
}

return

}
}
}()

select {
case <-waitForWaitGroup(&wg):
case <-ctx.Done():
// Wait for an additional second, but handle cancellation
select {
case <-time.After(1 * time.Second):
case <-ctx.Done(): // context was cancelled o
}
}

return m.IsStorenodeAvailable(m.activeStorenode)
Expand Down

0 comments on commit 3c4e40c

Please sign in to comment.