network: hide Peers, make it return a slice
Slice is a bit more efficient, we don't need a map for Peers() users and it's not really interesting to outside users, so better hide this method.
This commit is contained in:
parent
119b4200ac
commit
b55c75d59d
1 changed files with 10 additions and 10 deletions
|
@ -288,7 +288,7 @@ func (s *Server) Shutdown() {
|
||||||
s.transport.Close()
|
s.transport.Close()
|
||||||
s.discovery.Close()
|
s.discovery.Close()
|
||||||
s.consensus.Shutdown()
|
s.consensus.Shutdown()
|
||||||
for p := range s.Peers() {
|
for _, p := range s.getPeers() {
|
||||||
p.Disconnect(errServerShutdown)
|
p.Disconnect(errServerShutdown)
|
||||||
}
|
}
|
||||||
s.bQueue.discard()
|
s.bQueue.discard()
|
||||||
|
@ -431,7 +431,7 @@ func (s *Server) runProto() {
|
||||||
case <-pingTimer.C:
|
case <-pingTimer.C:
|
||||||
if s.chain.BlockHeight() == prevHeight {
|
if s.chain.BlockHeight() == prevHeight {
|
||||||
// Get a copy of s.peers to avoid holding a lock while sending.
|
// Get a copy of s.peers to avoid holding a lock while sending.
|
||||||
for peer := range s.Peers() {
|
for _, peer := range s.getPeers() {
|
||||||
_ = peer.SendPing(NewMessage(CMDPing, payload.NewPing(s.chain.BlockHeight(), s.id)))
|
_ = peer.SendPing(NewMessage(CMDPing, payload.NewPing(s.chain.BlockHeight(), s.id)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -489,15 +489,15 @@ func (s *Server) UnsubscribeFromNotaryRequests(ch chan<- mempoolevent.Event) {
|
||||||
s.notaryRequestPool.UnsubscribeFromTransactions(ch)
|
s.notaryRequestPool.UnsubscribeFromTransactions(ch)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Peers returns the current list of peers connected to
|
// getPeers returns current list of peers connected to
|
||||||
// the server.
|
// the server.
|
||||||
func (s *Server) Peers() map[Peer]bool {
|
func (s *Server) getPeers() []Peer {
|
||||||
s.lock.RLock()
|
s.lock.RLock()
|
||||||
defer s.lock.RUnlock()
|
defer s.lock.RUnlock()
|
||||||
|
|
||||||
peers := make(map[Peer]bool, len(s.peers))
|
peers := make([]Peer, 0, len(s.peers))
|
||||||
for k, v := range s.peers {
|
for k := range s.peers {
|
||||||
peers[k] = v
|
peers = append(peers, k)
|
||||||
}
|
}
|
||||||
|
|
||||||
return peers
|
return peers
|
||||||
|
@ -1142,7 +1142,7 @@ func (s *Server) requestTx(hashes ...util.Uint256) {
|
||||||
// peer is considered invalid if it returns false).
|
// peer is considered invalid if it returns false).
|
||||||
func (s *Server) iteratePeersWithSendMsg(msg *Message, send func(Peer, bool, []byte) error, peerOK func(Peer) bool) {
|
func (s *Server) iteratePeersWithSendMsg(msg *Message, send func(Peer, bool, []byte) error, peerOK func(Peer) bool) {
|
||||||
// Get a copy of s.peers to avoid holding a lock while sending.
|
// Get a copy of s.peers to avoid holding a lock while sending.
|
||||||
peers := s.Peers()
|
peers := s.getPeers()
|
||||||
if len(peers) == 0 {
|
if len(peers) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -1154,7 +1154,7 @@ func (s *Server) iteratePeersWithSendMsg(msg *Message, send func(Peer, bool, []b
|
||||||
success := make(map[Peer]bool, len(peers))
|
success := make(map[Peer]bool, len(peers))
|
||||||
okCount := 0
|
okCount := 0
|
||||||
sentCount := 0
|
sentCount := 0
|
||||||
for peer := range peers {
|
for _, peer := range peers {
|
||||||
if peerOK != nil && !peerOK(peer) {
|
if peerOK != nil && !peerOK(peer) {
|
||||||
success[peer] = false
|
success[peer] = false
|
||||||
continue
|
continue
|
||||||
|
@ -1176,7 +1176,7 @@ func (s *Server) iteratePeersWithSendMsg(msg *Message, send func(Peer, bool, []b
|
||||||
}
|
}
|
||||||
|
|
||||||
// Perform blocking send now.
|
// Perform blocking send now.
|
||||||
for peer := range peers {
|
for _, peer := range peers {
|
||||||
if _, ok := success[peer]; ok || peerOK != nil && !peerOK(peer) {
|
if _, ok := success[peer]; ok || peerOK != nil && !peerOK(peer) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue