From 18eef141d0997995a73ea1751591b7336d997b3c Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Sun, 4 Sep 2022 17:49:00 +0100 Subject: [PATCH] Actorise links, remove mutex --- src/core/api.go | 11 ++++++----- src/core/link.go | 45 ++++++++++++++++++++------------------------- 2 files changed, 26 insertions(+), 30 deletions(-) diff --git a/src/core/api.go b/src/core/api.go index 571b007d..657b5510 100644 --- a/src/core/api.go +++ b/src/core/api.go @@ -16,6 +16,7 @@ import ( //"sort" //"time" + "github.com/Arceliar/phony" "github.com/yggdrasil-network/yggdrasil-go/src/address" "github.com/yggdrasil-network/yggdrasil-go/src/util" //"github.com/yggdrasil-network/yggdrasil-go/src/crypto" @@ -69,11 +70,11 @@ func (c *Core) GetSelf() SelfInfo { func (c *Core) GetPeers() []PeerInfo { var peers []PeerInfo names := make(map[net.Conn]string) - c.links.mutex.Lock() - for _, info := range c.links.links { - names[info.conn] = info.lname - } - c.links.mutex.Unlock() + phony.Block(&c.links, func() { + for _, info := range c.links._links { + names[info.conn] = info.lname + } + }) ps := c.PacketConn.PacketConn.Debug.GetPeers() for _, p := range ps { var info PeerInfo diff --git a/src/core/link.go b/src/core/link.go index 6a8a0527..e0bd4592 100644 --- a/src/core/link.go +++ b/src/core/link.go @@ -9,7 +9,6 @@ import ( "net" "net/url" "strings" - "sync" //"sync/atomic" "time" @@ -23,12 +22,12 @@ import ( ) type links struct { - core *Core - tcp *linkTCP // TCP interface support - tls *linkTLS // TLS interface support - unix *linkUNIX // UNIX interface support - mutex sync.RWMutex // protects links below - links map[linkInfo]*link // *link is nil if connection in progress + phony.Inbox + core *Core + tcp *linkTCP // TCP interface support + tls *linkTLS // TLS interface support + unix *linkUNIX // UNIX interface support + _links map[linkInfo]*link // *link is nil if connection in progress // TODO timeout (to remove from switch), read from config.ReadTimeout } @@ -69,10 +68,7 @@ func (l *links) init(c *Core) error { l.tcp = l.newLinkTCP() l.tls = l.newLinkTLS(l.tcp) l.unix = l.newLinkUNIX() - - l.mutex.Lock() - l.links = make(map[linkInfo]*link) - l.mutex.Unlock() + l._links = make(map[linkInfo]*link) var listeners []ListenAddress phony.Block(c, func() { @@ -105,9 +101,10 @@ func (l *links) shutdown() error { } func (l *links) isConnectedTo(info linkInfo) bool { - l.mutex.RLock() - defer l.mutex.RUnlock() - _, isConnected := l.links[info] + var isConnected bool + phony.Block(l, func() { + _, isConnected = l._links[info] + }) return isConnected } @@ -231,16 +228,14 @@ func (intf *link) handler() error { } // Mark the connection as in progress. - intf.links.mutex.Lock() - intf.links.links[intf.info] = nil - intf.links.mutex.Unlock() + phony.Block(intf.links, func() { + intf.links._links[intf.info] = nil + }) // When we're done, clean up the connection entry. - defer func() { - intf.links.mutex.Lock() - delete(intf.links.links, intf.info) - intf.links.mutex.Unlock() - }() + defer phony.Block(intf.links, func() { + delete(intf.links._links, intf.info) + }) // TODO split some of this into shorter functions, so it's easier to read, and for the FIXME duplicate peer issue mentioned later meta := version_getBaseMetadata() @@ -318,9 +313,9 @@ func (intf *link) handler() error { return fmt.Errorf("forbidden connection") } - intf.links.mutex.Lock() - intf.links.links[intf.info] = intf - intf.links.mutex.Unlock() + phony.Block(intf.links, func() { + intf.links._links[intf.info] = intf + }) remoteAddr := net.IP(address.AddrForKey(meta.key)[:]).String() remoteStr := fmt.Sprintf("%s@%s", remoteAddr, intf.info.remote)