Actorise links, remove mutex

This commit is contained in:
Neil Alexander 2022-09-04 17:49:00 +01:00
parent 3ff2b83e76
commit 18eef141d0
2 changed files with 26 additions and 30 deletions

View file

@ -16,6 +16,7 @@ import (
//"sort" //"sort"
//"time" //"time"
"github.com/Arceliar/phony"
"github.com/yggdrasil-network/yggdrasil-go/src/address" "github.com/yggdrasil-network/yggdrasil-go/src/address"
"github.com/yggdrasil-network/yggdrasil-go/src/util" "github.com/yggdrasil-network/yggdrasil-go/src/util"
//"github.com/yggdrasil-network/yggdrasil-go/src/crypto" //"github.com/yggdrasil-network/yggdrasil-go/src/crypto"
@ -69,11 +70,11 @@ func (c *Core) GetSelf() SelfInfo {
func (c *Core) GetPeers() []PeerInfo { func (c *Core) GetPeers() []PeerInfo {
var peers []PeerInfo var peers []PeerInfo
names := make(map[net.Conn]string) names := make(map[net.Conn]string)
c.links.mutex.Lock() phony.Block(&c.links, func() {
for _, info := range c.links.links { for _, info := range c.links._links {
names[info.conn] = info.lname names[info.conn] = info.lname
} }
c.links.mutex.Unlock() })
ps := c.PacketConn.PacketConn.Debug.GetPeers() ps := c.PacketConn.PacketConn.Debug.GetPeers()
for _, p := range ps { for _, p := range ps {
var info PeerInfo var info PeerInfo

View file

@ -9,7 +9,6 @@ import (
"net" "net"
"net/url" "net/url"
"strings" "strings"
"sync"
//"sync/atomic" //"sync/atomic"
"time" "time"
@ -23,12 +22,12 @@ import (
) )
type links struct { type links struct {
core *Core phony.Inbox
tcp *linkTCP // TCP interface support core *Core
tls *linkTLS // TLS interface support tcp *linkTCP // TCP interface support
unix *linkUNIX // UNIX interface support tls *linkTLS // TLS interface support
mutex sync.RWMutex // protects links below unix *linkUNIX // UNIX interface support
links map[linkInfo]*link // *link is nil if connection in progress _links map[linkInfo]*link // *link is nil if connection in progress
// TODO timeout (to remove from switch), read from config.ReadTimeout // 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.tcp = l.newLinkTCP()
l.tls = l.newLinkTLS(l.tcp) l.tls = l.newLinkTLS(l.tcp)
l.unix = l.newLinkUNIX() l.unix = l.newLinkUNIX()
l._links = make(map[linkInfo]*link)
l.mutex.Lock()
l.links = make(map[linkInfo]*link)
l.mutex.Unlock()
var listeners []ListenAddress var listeners []ListenAddress
phony.Block(c, func() { phony.Block(c, func() {
@ -105,9 +101,10 @@ func (l *links) shutdown() error {
} }
func (l *links) isConnectedTo(info linkInfo) bool { func (l *links) isConnectedTo(info linkInfo) bool {
l.mutex.RLock() var isConnected bool
defer l.mutex.RUnlock() phony.Block(l, func() {
_, isConnected := l.links[info] _, isConnected = l._links[info]
})
return isConnected return isConnected
} }
@ -231,16 +228,14 @@ func (intf *link) handler() error {
} }
// Mark the connection as in progress. // Mark the connection as in progress.
intf.links.mutex.Lock() phony.Block(intf.links, func() {
intf.links.links[intf.info] = nil intf.links._links[intf.info] = nil
intf.links.mutex.Unlock() })
// When we're done, clean up the connection entry. // When we're done, clean up the connection entry.
defer func() { defer phony.Block(intf.links, func() {
intf.links.mutex.Lock() delete(intf.links._links, intf.info)
delete(intf.links.links, intf.info) })
intf.links.mutex.Unlock()
}()
// TODO split some of this into shorter functions, so it's easier to read, and for the FIXME duplicate peer issue mentioned later // 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() meta := version_getBaseMetadata()
@ -318,9 +313,9 @@ func (intf *link) handler() error {
return fmt.Errorf("forbidden connection") return fmt.Errorf("forbidden connection")
} }
intf.links.mutex.Lock() phony.Block(intf.links, func() {
intf.links.links[intf.info] = intf intf.links._links[intf.info] = intf
intf.links.mutex.Unlock() })
remoteAddr := net.IP(address.AddrForKey(meta.key)[:]).String() remoteAddr := net.IP(address.AddrForKey(meta.key)[:]).String()
remoteStr := fmt.Sprintf("%s@%s", remoteAddr, intf.info.remote) remoteStr := fmt.Sprintf("%s@%s", remoteAddr, intf.info.remote)