Skip to content

Commit

Permalink
Refactored peer routing
Browse files Browse the repository at this point in the history
  • Loading branch information
crioto committed Mar 22, 2018
1 parent 68e3c1d commit 317d8f2
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions lib/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,17 +313,11 @@ func (np *NetworkPeer) stateWaitingToConnect(ptpc *PeerToPeer) error {
return nil
}

func (np *NetworkPeer) route(ptpc *PeerToPeer) error {
for len(np.EndpointsHeap) == 0 {
return nil
}
func (np *NetworkPeer) sortEndpoints(ptpc *PeerToPeer) ([]PeerEndpoint, []PeerEndpoint, []PeerEndpoint) {
np.EndpointsLock.RLock()
locals := []PeerEndpoint{}
internet := []PeerEndpoint{}
proxies := []PeerEndpoint{}

stat := PeerStats{}

np.EndpointsLock.RLock()
for _, ep := range np.EndpointsHeap {
if time.Since(ep.LastContact) > time.Duration(time.Second*10) {
continue
Expand All @@ -345,7 +339,6 @@ func (np *NetworkPeer) route(ptpc *PeerToPeer) error {
}
if isNew {
proxies = append(proxies, ep)
stat.proxyNum++
}
continue
}
Expand All @@ -362,7 +355,6 @@ func (np *NetworkPeer) route(ptpc *PeerToPeer) error {
}
if isNew {
locals = append(locals, ep)
stat.localNum++
}
continue
}
Expand All @@ -374,17 +366,30 @@ func (np *NetworkPeer) route(ptpc *PeerToPeer) error {
}
if isNew {
internet = append(internet, ep)
stat.internetNum++
}
}
np.EndpointsLock.RUnlock()
return locals, internet, proxies
}

func (np *NetworkPeer) route(ptpc *PeerToPeer) error {
for len(np.EndpointsHeap) == 0 {
return nil
}

stat := PeerStats{}
locals, internet, proxies := np.sortEndpoints(ptpc)

np.EndpointsLock.Lock()
np.EndpointsActive = np.EndpointsActive[:0]
np.EndpointsActive = append(np.EndpointsActive, locals...)
np.EndpointsActive = append(np.EndpointsActive, internet...)
np.EndpointsActive = append(np.EndpointsActive, proxies...)
np.EndpointsLock.Unlock()

stat.localNum = len(locals)
stat.internetNum = len(internet)
stat.proxyNum = len(proxies)
np.Stat = stat

if len(np.EndpointsActive) > 0 {
Expand Down

0 comments on commit 317d8f2

Please sign in to comment.