@@ -18,6 +18,7 @@ package p2p
1818
1919import (
2020 "context"
21+ "math/rand/v2"
2122 "sync"
2223 "time"
2324
@@ -109,7 +110,7 @@ func (c *CapabilitiesDiscovery) PeersForCapability(capability Capability, n int)
109110}
110111
111112// AdvertiseCapabilities periodically runs the Advertiser interface on the DHT
112- // If a capability fails to advertise we will retry every 10 seconds until full success
113+ // If a capability fails to advertise we will retry every 100 seconds until full success
113114// This gets rerun every at the minimum ttl or the maxAdvertisementInterval.
114115func (c * CapabilitiesDiscovery ) AdvertiseCapabilities (capabilities ... Capability ) {
115116 c .wg .Add (1 )
@@ -121,6 +122,15 @@ func (c *CapabilitiesDiscovery) AdvertiseCapabilities(capabilities ...Capability
121122 }()
122123
123124 for {
125+ // shuffle capabilities to advertise in random order
126+ // since the DHT's internal advertisement happens concurrently for peers in its routing table
127+ // any peer error does not prevent advertisement of other peers.
128+ // on repeated advertisement, we want to avoid the same order to make sure all capabilities are advertised.
129+ if len (capabilities ) > 1 {
130+ rand .Shuffle (len (capabilities ), func (i , j int ) {
131+ capabilities [i ], capabilities [j ] = capabilities [j ], capabilities [i ]
132+ })
133+ }
124134 select {
125135 case <- c .dht .Context ().Done ():
126136 return
@@ -131,17 +141,17 @@ func (c *CapabilitiesDiscovery) AdvertiseCapabilities(capabilities ...Capability
131141 ttl , err0 := c .advertise (c .dht .Context (), string (capa ))
132142 if err0 != nil {
133143 err = err0
134- c .log .Errorf ("failed to advertise for capability %s: %v" , capa , err0 )
144+ c .log .Warnf ("failed to advertise for capability %s: %v" , capa , err0 )
135145 break
136146 }
137147 if ttl < advertisementInterval {
138148 advertisementInterval = ttl
139149 }
140150 c .log .Infof ("advertised capability %s" , capa )
141151 }
142- // If we failed to advertise, retry every 10 seconds until successful
152+ // If we failed to advertise, retry every 100 seconds until successful
143153 if err != nil {
144- nextExecution = time .After (time .Second * 10 )
154+ nextExecution = time .After (time .Second * 100 )
145155 } else {
146156 // Otherwise, ensure we're at the correct interval
147157 nextExecution = time .After (advertisementInterval )
0 commit comments