Skip to content
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
address review
  • Loading branch information
guillaumemichel committed Aug 28, 2025
commit 6ef7cfec0847989935bb05e1496adf9bf0026af9
27 changes: 20 additions & 7 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,9 @@ func (s *SweepingProvider) Close() error {
close(s.done)
s.cancelCtx()
s.wg.Wait()
s.approxPrefixLenRunning.Lock()
_ = struct{}{} // cannot have empty critical section
s.approxPrefixLenRunning.Unlock()

s.scheduleTimer.Stop()
err = cleanup(s.cleanupFuncs)
Expand Down Expand Up @@ -487,7 +490,7 @@ func (s *SweepingProvider) approxPrefixLen() {
logger.Infof("GetClosestPeers failed during prefix len approximation measurement: %s", err)
} else {
if len(peers) < 2 {
return // Ignore result if only 2 other peers in DHT.
return // Ignore result if less than 2 other peers in DHT.
}
cpl := keyspace.KeyLen
firstPeerKey := keyspace.PeerIDToBit256(peers[0])
Expand Down Expand Up @@ -988,15 +991,25 @@ func (s *SweepingProvider) onOffline() {
}

func (s *SweepingProvider) onOnline() {
if s.closed() || !s.approxPrefixLenRunning.TryLock() {
if s.closed() {
return
}
s.wg.Add(1)
s.approxPrefixLen()
s.wg.Done()
s.approxPrefixLenRunning.Unlock()

s.RefreshSchedule()
s.avgPrefixLenLk.Lock()
cachedAvgPrefixLen := s.cachedAvgPrefixLen
s.avgPrefixLenLk.Unlock()

if cachedAvgPrefixLen == -1 {
// Provider was previously Offline (not Disconnected).
// Run prefix length measurement, and refresh schedule afterwards.
if !s.approxPrefixLenRunning.TryLock() {
return
}
s.approxPrefixLen()
s.approxPrefixLenRunning.Unlock()

s.RefreshSchedule()
}

s.catchupPendingWork()
}
Expand Down
Loading