refactor: make Peer.Connections() return a set instead of a slice
This is more flexible since it allows callers to perform efficient lookups on the result.
This commit is contained in:
1 parent
fe4d8b7767
commit
9b9d8a03aa
6 files changed
+16
-9
No files matched your search
@@ -116,7 +116,7 @@ func (cm *ConnectionMaker) checkStateAndAttemptConnections() time.Duration {
|
|||||||
// copy the set of things we are connected to, so we can access them without locking
|
// copy the set of things we are connected to, so we can access them without locking
|
||||||
ourConnectedPeers := make(PeerNameSet)
|
ourConnectedPeers := make(PeerNameSet)
|
||||||
ourConnectedTargets := make(map[string]struct{})
|
ourConnectedTargets := make(map[string]struct{})
|
||||||
for _, conn := range cm.ourself.Connections() {
|
for conn := range cm.ourself.Connections() {
|
||||||
ourConnectedPeers[conn.Remote().Name] = void
|
ourConnectedPeers[conn.Remote().Name] = void
|
||||||
ourConnectedTargets[conn.RemoteTCPAddr()] = void
|
ourConnectedTargets[conn.RemoteTCPAddr()] = void
|
||||||
}
|
}
|
||||||
@@ -136,7 +136,7 @@ func (cm *ConnectionMaker) checkStateAndAttemptConnections() time.Duration {
|
|||||||
// Add targets for peers that someone else is connected to, but we
|
// Add targets for peers that someone else is connected to, but we
|
||||||
// aren't
|
// aren't
|
||||||
cm.peers.ForEach(func(peer *Peer) {
|
cm.peers.ForEach(func(peer *Peer) {
|
||||||
for _, conn := range peer.Connections() {
|
for conn := range peer.Connections() {
|
||||||
otherPeer := conn.Remote().Name
|
otherPeer := conn.Remote().Name
|
||||||
if otherPeer == cm.ourself.Name {
|
if otherPeer == cm.ourself.Name {
|
||||||
continue
|
continue
|
||||||
|
|||||||
+1
-1
@@ -179,7 +179,7 @@ func (c *GossipChannel) Send(data GossipData) {
|
|||||||
retainedSenders := make(connectionSenders)
|
retainedSenders := make(connectionSenders)
|
||||||
c.Lock()
|
c.Lock()
|
||||||
defer c.Unlock()
|
defer c.Unlock()
|
||||||
for _, conn := range connections {
|
for conn := range connections {
|
||||||
c.sendDown(conn, data)
|
c.sendDown(conn, data)
|
||||||
retainedSenders[conn] = c.senders[conn]
|
retainedSenders[conn] = c.senders[conn]
|
||||||
delete(c.senders, conn)
|
delete(c.senders, conn)
|
||||||
|
|||||||
+6
-1
@@ -64,13 +64,18 @@ func (routes *Routes) MarshalJSON() ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (peer *Peer) MarshalJSON() ([]byte, error) {
|
func (peer *Peer) MarshalJSON() ([]byte, error) {
|
||||||
|
conns := peer.Connections()
|
||||||
|
connections := make([]Connection, 0, len(conns))
|
||||||
|
for conn := range conns {
|
||||||
|
connections = append(connections, conn)
|
||||||
|
}
|
||||||
return json.Marshal(struct {
|
return json.Marshal(struct {
|
||||||
Name string
|
Name string
|
||||||
NickName string
|
NickName string
|
||||||
UID uint64
|
UID uint64
|
||||||
Version uint64
|
Version uint64
|
||||||
Connections []Connection
|
Connections []Connection
|
||||||
}{peer.Name.String(), peer.NickName, peer.UID, peer.version, peer.Connections()})
|
}{peer.Name.String(), peer.NickName, peer.UID, peer.version, connections})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (conn *RemoteConnection) MarshalJSON() ([]byte, error) {
|
func (conn *RemoteConnection) MarshalJSON() ([]byte, error) {
|
||||||
|
|||||||
+5
-3
@@ -17,6 +17,8 @@ type Peer struct {
|
|||||||
connections map[PeerName]Connection
|
connections map[PeerName]Connection
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ConnectionSet map[Connection]struct{}
|
||||||
|
|
||||||
func NewPeer(name PeerName, nickName string, uid uint64, version uint64) *Peer {
|
func NewPeer(name PeerName, nickName string, uid uint64, version uint64) *Peer {
|
||||||
if uid == 0 {
|
if uid == 0 {
|
||||||
uid = randUint64()
|
uid = randUint64()
|
||||||
@@ -77,12 +79,12 @@ func (peer *Peer) ConnectionTo(name PeerName) (Connection, bool) {
|
|||||||
return conn, found // yes, you really can't inline that. FFS.
|
return conn, found // yes, you really can't inline that. FFS.
|
||||||
}
|
}
|
||||||
|
|
||||||
func (peer *Peer) Connections() []Connection {
|
func (peer *Peer) Connections() ConnectionSet {
|
||||||
|
connections := make(ConnectionSet)
|
||||||
peer.RLock()
|
peer.RLock()
|
||||||
defer peer.RUnlock()
|
defer peer.RUnlock()
|
||||||
connections := make([]Connection, 0, len(peer.connections))
|
|
||||||
for _, conn := range peer.connections {
|
for _, conn := range peer.connections {
|
||||||
connections = append(connections, conn)
|
connections[conn] = void
|
||||||
}
|
}
|
||||||
return connections
|
return connections
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -133,7 +133,7 @@ func (peers *Peers) String() string {
|
|||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
peers.ForEach(func(peer *Peer) {
|
peers.ForEach(func(peer *Peer) {
|
||||||
fmt.Fprint(&buf, peer, "\n")
|
fmt.Fprint(&buf, peer, "\n")
|
||||||
for _, conn := range peer.Connections() {
|
for conn := range peer.Connections() {
|
||||||
established := ""
|
established := ""
|
||||||
if !conn.Established() {
|
if !conn.Established() {
|
||||||
established = " (unestablished)"
|
established = " (unestablished)"
|
||||||
|
|||||||
+1
-1
@@ -162,7 +162,7 @@ func (routes *Routes) calculateBroadcast(establishedAndSymmetric bool) map[PeerN
|
|||||||
// This is rather similar to the inner loop on
|
// This is rather similar to the inner loop on
|
||||||
// peer.Routes(...); the main difference is in the
|
// peer.Routes(...); the main difference is in the
|
||||||
// locking.
|
// locking.
|
||||||
for _, conn := range ourself.Connections() {
|
for conn := range ourself.Connections() {
|
||||||
if establishedAndSymmetric && !conn.Established() {
|
if establishedAndSymmetric && !conn.Established() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user