@@ -90,9 +90,10 @@ type PubSub struct {
90
90
rmTopic chan * rmTopicReq
91
91
92
92
// a notification channel for new peer connections accumulated
93
- newPeers chan struct {}
94
- newPeersSema chan struct {}
95
- newPeersPend map [peer.ID ]struct {}
93
+ newPeers chan struct {}
94
+ newPeersPrioLk sync.RWMutex
95
+ newPeersMx sync.Mutex
96
+ newPeersPend map [peer.ID ]struct {}
96
97
97
98
// a notification channel for new outoging peer streams
98
99
newPeerStream chan network.Stream
@@ -234,7 +235,6 @@ func NewPubSub(ctx context.Context, h host.Host, rt PubSubRouter, opts ...Option
234
235
signPolicy : StrictSign ,
235
236
incoming : make (chan * RPC , 32 ),
236
237
newPeers : make (chan struct {}, 1 ),
237
- newPeersSema : make (chan struct {}, 1 ),
238
238
newPeersPend : make (map [peer.ID ]struct {}),
239
239
newPeerStream : make (chan network.Stream ),
240
240
newPeerError : make (chan peer.ID ),
@@ -264,8 +264,6 @@ func NewPubSub(ctx context.Context, h host.Host, rt PubSubRouter, opts ...Option
264
264
counter : uint64 (time .Now ().UnixNano ()),
265
265
}
266
266
267
- ps .newPeersSema <- struct {}{}
268
-
269
267
for _ , opt := range opts {
270
268
err := opt (ps )
271
269
if err != nil {
@@ -615,16 +613,8 @@ func (p *PubSub) processLoop(ctx context.Context) {
615
613
}
616
614
617
615
func (p * PubSub ) handlePendingPeers () {
618
- select {
619
- case <- p .newPeersSema :
620
- defer func () {
621
- p .newPeersSema <- struct {}{}
622
- }()
623
-
624
- default :
625
- // contention, return and wait for the next notification without blocking the event loop
626
- return
627
- }
616
+ p .newPeersPrioLk .Lock ()
617
+ defer p .newPeersPrioLk .Unlock ()
628
618
629
619
if len (p .newPeersPend ) == 0 {
630
620
return
@@ -634,6 +624,10 @@ func (p *PubSub) handlePendingPeers() {
634
624
p .newPeersPend = make (map [peer.ID ]struct {})
635
625
636
626
for pid := range newPeers {
627
+ if p .host .Network ().Connectedness (pid ) != network .Connected {
628
+ continue
629
+ }
630
+
637
631
if _ , ok := p .peers [pid ]; ok {
638
632
log .Debug ("already have connection to peer: " , pid )
639
633
continue
0 commit comments