5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-26 15:21:37 +00:00

peer thread safey for dhtInfo updates

This commit is contained in:
Arceliar 2018-12-14 21:44:31 -06:00
parent 570e85c297
commit 4875ab8954

View File

@ -90,7 +90,7 @@ type peer struct {
firstSeen time.Time // To track uptime for getPeers firstSeen time.Time // To track uptime for getPeers
linkOut (chan []byte) // used for protocol traffic (to bypass queues) linkOut (chan []byte) // used for protocol traffic (to bypass queues)
doSend (chan struct{}) // tell the linkLoop to send a switchMsg doSend (chan struct{}) // tell the linkLoop to send a switchMsg
dinfo *dhtInfo // used to keep the DHT working dinfo (chan *dhtInfo) // used to keep the DHT working
out func([]byte) // Set up by whatever created the peers struct, used to send packets to other nodes out func([]byte) // Set up by whatever created the peers struct, used to send packets to other nodes
close func() // Called when a peer is removed, to close the underlying connection, or via admin api close func() // Called when a peer is removed, to close the underlying connection, or via admin api
} }
@ -105,6 +105,7 @@ func (ps *peers) newPeer(box *boxPubKey, sig *sigPubKey, linkShared *boxSharedKe
endpoint: endpoint, endpoint: endpoint,
firstSeen: now, firstSeen: now,
doSend: make(chan struct{}, 1), doSend: make(chan struct{}, 1),
dinfo: make(chan *dhtInfo, 1),
core: ps.core} core: ps.core}
ps.mutex.Lock() ps.mutex.Lock()
defer ps.mutex.Unlock() defer ps.mutex.Unlock()
@ -178,6 +179,7 @@ func (p *peer) linkLoop() {
tick := time.NewTicker(time.Second) tick := time.NewTicker(time.Second)
defer tick.Stop() defer tick.Stop()
p.doSendSwitchMsgs() p.doSendSwitchMsgs()
var dinfo *dhtInfo
for { for {
select { select {
case _, ok := <-p.doSend: case _, ok := <-p.doSend:
@ -185,8 +187,8 @@ func (p *peer) linkLoop() {
return return
} }
p.sendSwitchMsg() p.sendSwitchMsg()
case dinfo = <-p.dinfo:
case _ = <-tick.C: case _ = <-tick.C:
dinfo := p.dinfo // FIXME? are pointer reads *always* atomic?
if dinfo != nil { if dinfo != nil {
p.core.dht.peers <- dinfo p.core.dht.peers <- dinfo
} }
@ -218,8 +220,9 @@ func (p *peer) handlePacket(packet []byte) {
// Called to handle traffic or protocolTraffic packets. // Called to handle traffic or protocolTraffic packets.
// In either case, this reads from the coords of the packet header, does a switch lookup, and forwards to the next node. // In either case, this reads from the coords of the packet header, does a switch lookup, and forwards to the next node.
func (p *peer) handleTraffic(packet []byte, pTypeLen int) { func (p *peer) handleTraffic(packet []byte, pTypeLen int) {
if p.port != 0 && p.dinfo == nil { table := p.core.switchTable.getTable()
// Drop traffic until the peer manages to send us at least one good switchMsg if _, isIn := table.elems[p.port]; !isIn && p.port != 0 {
// Drop traffic if the peer isn't in the switch
return return
} }
p.core.switchTable.packetIn <- packet p.core.switchTable.packetIn <- packet
@ -323,9 +326,7 @@ func (p *peer) handleSwitchMsg(packet []byte) {
p.core.switchTable.handleMsg(&msg, p.port) p.core.switchTable.handleMsg(&msg, p.port)
if !p.core.switchTable.checkRoot(&msg) { if !p.core.switchTable.checkRoot(&msg) {
// Bad switch message // Bad switch message
// Stop forwarding traffic from it p.dinfo <- nil
// Stop refreshing it in the DHT
p.dinfo = nil
return return
} }
// Pass a mesage to the dht informing it that this peer (still) exists // Pass a mesage to the dht informing it that this peer (still) exists
@ -334,8 +335,7 @@ func (p *peer) handleSwitchMsg(packet []byte) {
key: p.box, key: p.box,
coords: loc.getCoords(), coords: loc.getCoords(),
} }
//p.core.dht.peers <- &dinfo p.dinfo <- &dinfo
p.dinfo = &dinfo
} }
// This generates the bytes that we sign or check the signature of for a switchMsg. // This generates the bytes that we sign or check the signature of for a switchMsg.