move Connections from Peer to LocalPeer
further shrinking the former's API
This commit is contained in:
1 parent
5bf6889250
commit
86ba3be4f7
7 files changed
+62
-38
No files matched your search
@@ -206,8 +206,13 @@ func (cm *ConnectionMaker) ourConnections() (PeerNameSet, map[string]struct{}, m
|
|||||||
|
|
||||||
func (cm *ConnectionMaker) addPeerTargets(ourConnectedPeers PeerNameSet, addTarget func(string)) {
|
func (cm *ConnectionMaker) addPeerTargets(ourConnectedPeers PeerNameSet, addTarget func(string)) {
|
||||||
cm.peers.ForEach(func(peer *Peer) {
|
cm.peers.ForEach(func(peer *Peer) {
|
||||||
for conn := range peer.Connections() {
|
if peer == cm.ourself.Peer {
|
||||||
otherPeer := conn.Remote().Name
|
return
|
||||||
|
}
|
||||||
|
// Modifying peer.connections requires a write lock on Peers,
|
||||||
|
// and since we are holding a read lock (due to the ForEach),
|
||||||
|
// access without locking the peer is safe.
|
||||||
|
for otherPeer, conn := range peer.connections {
|
||||||
if otherPeer == cm.ourself.Name {
|
if otherPeer == cm.ourself.Name {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|||||||
+24
-17
@@ -35,8 +35,30 @@ func (cache *MacCache) MarshalJSON() ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (peers *Peers) MarshalJSON() ([]byte, error) {
|
func (peers *Peers) MarshalJSON() ([]byte, error) {
|
||||||
var ps []*Peer
|
type p struct {
|
||||||
peers.ForEach(func(peer *Peer) { ps = append(ps, peer) })
|
Name string
|
||||||
|
NickName string
|
||||||
|
UID uint64
|
||||||
|
Version uint64
|
||||||
|
Connections []Connection
|
||||||
|
}
|
||||||
|
var ps []*p
|
||||||
|
peers.ForEach(func(peer *Peer) {
|
||||||
|
var connections []Connection
|
||||||
|
if peer == peers.ourself.Peer {
|
||||||
|
for conn := range peers.ourself.Connections() {
|
||||||
|
connections = append(connections, conn)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Modifying peer.connections requires a write lock on
|
||||||
|
// Peers, and since we are holding a read lock (due to the
|
||||||
|
// ForEach), access without locking the peer is safe.
|
||||||
|
for _, conn := range peer.connections {
|
||||||
|
connections = append(connections, conn)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ps = append(ps, &p{peer.Name.String(), peer.NickName, peer.UID, peer.version, connections})
|
||||||
|
})
|
||||||
return json.Marshal(ps)
|
return json.Marshal(ps)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,21 +85,6 @@ func (routes *Routes) MarshalJSON() ([]byte, error) {
|
|||||||
return json.Marshal(r)
|
return json.Marshal(r)
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
|
||||||
Name string
|
|
||||||
NickName string
|
|
||||||
UID uint64
|
|
||||||
Version uint64
|
|
||||||
Connections []Connection
|
|
||||||
}{peer.Name.String(), peer.NickName, peer.UID, peer.version, connections})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (conn *RemoteConnection) MarshalJSON() ([]byte, error) {
|
func (conn *RemoteConnection) MarshalJSON() ([]byte, error) {
|
||||||
return json.Marshal(struct {
|
return json.Marshal(struct {
|
||||||
Name string
|
Name string
|
||||||
|
|||||||
@@ -75,6 +75,16 @@ func (peer *LocalPeer) RelayBroadcast(srcPeer *Peer, df bool, frame []byte, dec
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (peer *LocalPeer) Connections() ConnectionSet {
|
||||||
|
connections := make(ConnectionSet)
|
||||||
|
peer.RLock()
|
||||||
|
defer peer.RUnlock()
|
||||||
|
for _, conn := range peer.connections {
|
||||||
|
connections[conn] = void
|
||||||
|
}
|
||||||
|
return connections
|
||||||
|
}
|
||||||
|
|
||||||
func (peer *LocalPeer) ConnectionTo(name PeerName) (Connection, bool) {
|
func (peer *LocalPeer) ConnectionTo(name PeerName) (Connection, bool) {
|
||||||
peer.RLock()
|
peer.RLock()
|
||||||
defer peer.RUnlock()
|
defer peer.RUnlock()
|
||||||
|
|||||||
@@ -42,16 +42,6 @@ func (peer *Peer) Info() string {
|
|||||||
return fmt.Sprint(peer.String(), " (v", peer.version, ") (UID ", peer.UID, ")")
|
return fmt.Sprint(peer.String(), " (v", peer.version, ") (UID ", peer.UID, ")")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (peer *Peer) Connections() ConnectionSet {
|
|
||||||
connections := make(ConnectionSet)
|
|
||||||
peer.RLock()
|
|
||||||
defer peer.RUnlock()
|
|
||||||
for _, conn := range peer.connections {
|
|
||||||
connections[conn] = void
|
|
||||||
}
|
|
||||||
return connections
|
|
||||||
}
|
|
||||||
|
|
||||||
func (peer *Peer) SetVersionAndConnections(version uint64, connections map[PeerName]Connection) {
|
func (peer *Peer) SetVersionAndConnections(version uint64, connections map[PeerName]Connection) {
|
||||||
peer.Lock()
|
peer.Lock()
|
||||||
defer peer.Unlock()
|
defer peer.Unlock()
|
||||||
|
|||||||
+17
-5
@@ -152,14 +152,26 @@ func (peers *Peers) GarbageCollect() []*Peer {
|
|||||||
|
|
||||||
func (peers *Peers) String() string {
|
func (peers *Peers) String() string {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
|
printConnection := func(conn Connection) {
|
||||||
|
established := ""
|
||||||
|
if !conn.Established() {
|
||||||
|
established = " (unestablished)"
|
||||||
|
}
|
||||||
|
fmt.Fprintf(&buf, " -> %s [%v%s]\n", conn.Remote(), conn.RemoteTCPAddr(), established)
|
||||||
|
}
|
||||||
peers.ForEach(func(peer *Peer) {
|
peers.ForEach(func(peer *Peer) {
|
||||||
fmt.Fprintln(&buf, peer.Info())
|
fmt.Fprintln(&buf, peer.Info())
|
||||||
for conn := range peer.Connections() {
|
if peer == peers.ourself.Peer {
|
||||||
established := ""
|
for conn := range peers.ourself.Connections() {
|
||||||
if !conn.Established() {
|
printConnection(conn)
|
||||||
established = " (unestablished)"
|
}
|
||||||
|
} else {
|
||||||
|
// Modifying peer.connections requires a write lock on
|
||||||
|
// Peers, and since we are holding a read lock (due to the
|
||||||
|
// ForEach), access without locking the peer is safe.
|
||||||
|
for _, conn := range peer.connections {
|
||||||
|
printConnection(conn)
|
||||||
}
|
}
|
||||||
fmt.Fprintf(&buf, " -> %s [%v%s]\n", conn.Remote(), conn.RemoteTCPAddr(), established)
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return buf.String()
|
return buf.String()
|
||||||
|
|||||||
+1
-1
@@ -66,7 +66,7 @@ func NewRouter(config RouterConfig, name PeerName, nickName string) *Router {
|
|||||||
router.Macs = NewMacCache(macMaxAge, onMacExpiry)
|
router.Macs = NewMacCache(macMaxAge, onMacExpiry)
|
||||||
router.Peers = NewPeers(router.Ourself, onPeerGC)
|
router.Peers = NewPeers(router.Ourself, onPeerGC)
|
||||||
router.Peers.FetchWithDefault(router.Ourself.Peer)
|
router.Peers.FetchWithDefault(router.Ourself.Peer)
|
||||||
router.Routes = NewRoutes(router.Ourself.Peer, router.Peers)
|
router.Routes = NewRoutes(router.Ourself, router.Peers)
|
||||||
router.ConnectionMaker = NewConnectionMaker(router.Ourself, router.Peers, router.Port)
|
router.ConnectionMaker = NewConnectionMaker(router.Ourself, router.Peers, router.Port)
|
||||||
router.TopologyGossip = router.NewGossip("topology", router)
|
router.TopologyGossip = router.NewGossip("topology", router)
|
||||||
return router
|
return router
|
||||||
|
|||||||
+3
-3
@@ -9,7 +9,7 @@ import (
|
|||||||
|
|
||||||
type Routes struct {
|
type Routes struct {
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
ourself *Peer
|
ourself *LocalPeer
|
||||||
peers *Peers
|
peers *Peers
|
||||||
unicast map[PeerName]PeerName
|
unicast map[PeerName]PeerName
|
||||||
unicastAll map[PeerName]PeerName // [1]
|
unicastAll map[PeerName]PeerName // [1]
|
||||||
@@ -21,7 +21,7 @@ type Routes struct {
|
|||||||
// symmetric ones
|
// symmetric ones
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRoutes(ourself *Peer, peers *Peers) *Routes {
|
func NewRoutes(ourself *LocalPeer, peers *Peers) *Routes {
|
||||||
routes := &Routes{
|
routes := &Routes{
|
||||||
ourself: ourself,
|
ourself: ourself,
|
||||||
peers: peers,
|
peers: peers,
|
||||||
@@ -219,7 +219,7 @@ func (routes *Routes) calculateBroadcast(establishedAndSymmetric bool) map[PeerN
|
|||||||
|
|
||||||
routes.peers.ForEach(func(peer *Peer) {
|
routes.peers.ForEach(func(peer *Peer) {
|
||||||
hops := []PeerName{}
|
hops := []PeerName{}
|
||||||
if found, reached := peer.Routes(ourself, establishedAndSymmetric); found {
|
if found, reached := peer.Routes(ourself.Peer, establishedAndSymmetric); found {
|
||||||
// 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.
|
||||||
|
|||||||
Reference in new issue
Block a user