@@ -18,6 +18,7 @@ package p2p
18
18
19
19
import (
20
20
"context"
21
+ "math/rand/v2"
21
22
"sync"
22
23
"time"
23
24
@@ -109,7 +110,7 @@ func (c *CapabilitiesDiscovery) PeersForCapability(capability Capability, n int)
109
110
}
110
111
111
112
// 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
113
114
// This gets rerun every at the minimum ttl or the maxAdvertisementInterval.
114
115
func (c * CapabilitiesDiscovery ) AdvertiseCapabilities (capabilities ... Capability ) {
115
116
c .wg .Add (1 )
@@ -121,6 +122,15 @@ func (c *CapabilitiesDiscovery) AdvertiseCapabilities(capabilities ...Capability
121
122
}()
122
123
123
124
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
+ }
124
134
select {
125
135
case <- c .dht .Context ().Done ():
126
136
return
@@ -131,17 +141,17 @@ func (c *CapabilitiesDiscovery) AdvertiseCapabilities(capabilities ...Capability
131
141
ttl , err0 := c .advertise (c .dht .Context (), string (capa ))
132
142
if err0 != nil {
133
143
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 )
135
145
break
136
146
}
137
147
if ttl < advertisementInterval {
138
148
advertisementInterval = ttl
139
149
}
140
150
c .log .Infof ("advertised capability %s" , capa )
141
151
}
142
- // If we failed to advertise, retry every 10 seconds until successful
152
+ // If we failed to advertise, retry every 100 seconds until successful
143
153
if err != nil {
144
- nextExecution = time .After (time .Second * 10 )
154
+ nextExecution = time .After (time .Second * 100 )
145
155
} else {
146
156
// Otherwise, ensure we're at the correct interval
147
157
nextExecution = time .After (advertisementInterval )
0 commit comments