@@ -18,7 +18,7 @@ import (
1818
1919const (
2020 pingTimeout = time .Second * 15
21- workers = 8
21+ workers = 16
2222 retryAfterDuration = time .Minute * 5
2323)
2424
@@ -32,8 +32,8 @@ type reacher struct {
3232 mu sync.Mutex
3333 peers map [string ]* peer
3434
35- work chan struct {}
36- quit chan struct {}
35+ newPeer chan struct {}
36+ quit chan struct {}
3737
3838 pinger p2p.Pinger
3939 notifier p2p.ReachableNotifier
@@ -53,7 +53,7 @@ type Options struct {
5353func New (streamer p2p.Pinger , notifier p2p.ReachableNotifier , o * Options ) * reacher {
5454
5555 r := & reacher {
56- work : make (chan struct {}, 1 ),
56+ newPeer : make (chan struct {}, 1 ),
5757 quit : make (chan struct {}),
5858 pinger : streamer ,
5959 peers : make (map [string ]* peer ),
@@ -103,7 +103,7 @@ func (r *reacher) manage() {
103103 select {
104104 case <- r .quit :
105105 return
106- case <- r .work :
106+ case <- r .newPeer :
107107 continue
108108 case <- time .After (tryAfter ):
109109 continue
@@ -115,12 +115,12 @@ func (r *reacher) manage() {
115115 select {
116116 case <- r .quit :
117117 return
118- case <- r .work :
118+ case <- r .newPeer :
119119 continue
120120 }
121121 }
122122
123- // send p to channel
123+ // ping peer
124124 select {
125125 case <- r .quit :
126126 return
@@ -135,10 +135,6 @@ func (r *reacher) ping(c chan *peer, ctx context.Context) {
135135
136136 for p := range c {
137137
138- r .mu .Lock ()
139- overlay := p .overlay
140- r .mu .Unlock ()
141-
142138 now := time .Now ()
143139
144140 ctxt , cancel := context .WithTimeout (ctx , r .options .PingTimeout )
@@ -149,14 +145,12 @@ func (r *reacher) ping(c chan *peer, ctx context.Context) {
149145 if err == nil {
150146 r .metrics .Pings .WithLabelValues ("success" ).Inc ()
151147 r .metrics .PingTime .WithLabelValues ("success" ).Observe (time .Since (now ).Seconds ())
152- r .notifier .Reachable (overlay , p2p .ReachabilityStatusPublic )
148+ r .notifier .Reachable (p . overlay , p2p .ReachabilityStatusPublic )
153149 } else {
154150 r .metrics .Pings .WithLabelValues ("failure" ).Inc ()
155151 r .metrics .PingTime .WithLabelValues ("failure" ).Observe (time .Since (now ).Seconds ())
156- r .notifier .Reachable (overlay , p2p .ReachabilityStatusPrivate )
152+ r .notifier .Reachable (p . overlay , p2p .ReachabilityStatusPrivate )
157153 }
158-
159- r .notifyManage ()
160154 }
161155}
162156
@@ -191,13 +185,6 @@ func (r *reacher) tryAcquirePeer() (*peer, time.Duration) {
191185 return nil , time .Until (nextClosest )
192186}
193187
194- func (r * reacher ) notifyManage () {
195- select {
196- case r .work <- struct {}{}:
197- default :
198- }
199- }
200-
201188// Connected adds a new peer to the queue for testing reachability.
202189func (r * reacher ) Connected (overlay swarm.Address , addr ma.Multiaddr ) {
203190 r .mu .Lock ()
@@ -207,7 +194,10 @@ func (r *reacher) Connected(overlay swarm.Address, addr ma.Multiaddr) {
207194 r .peers [overlay .ByteString ()] = & peer {overlay : overlay , addr : addr }
208195 }
209196
210- r .notifyManage ()
197+ select {
198+ case r .newPeer <- struct {}{}:
199+ default :
200+ }
211201}
212202
213203// Disconnected removes a peer from the queue.
0 commit comments