discosrv: Expire nodes, reduce debug logging

This commit is contained in:
Jakob Borg 2014-02-17 09:23:37 +01:00
parent 8f0a015abf
commit 9744629c4b

View File

@ -4,6 +4,7 @@ import (
"log" "log"
"net" "net"
"sync" "sync"
"time"
"github.com/calmh/syncthing/discover" "github.com/calmh/syncthing/discover"
) )
@ -11,11 +12,13 @@ import (
type Node struct { type Node struct {
IP []byte IP []byte
Port uint16 Port uint16
Updated time.Time
} }
var ( var (
nodes = make(map[string]Node) nodes = make(map[string]Node)
lock sync.Mutex lock sync.Mutex
queries = 0
) )
func main() { func main() {
@ -25,6 +28,26 @@ func main() {
panic(err) panic(err)
} }
go func() {
for {
time.Sleep(600 * time.Second)
lock.Lock()
var deleted = 0
for id, node := range nodes {
if time.Since(node.Updated) > 60*time.Minute {
delete(nodes, id)
deleted++
}
}
log.Printf("Expired %d nodes; %d nodes in registry; %d queries", deleted, len(nodes), queries)
queries = 0
lock.Unlock()
}
}()
var buf = make([]byte, 1024) var buf = make([]byte, 1024)
for { for {
n, addr, err := conn.ReadFromUDP(buf) n, addr, err := conn.ReadFromUDP(buf)
@ -40,20 +63,25 @@ func main() {
switch pkt.Magic { switch pkt.Magic {
case 0x20121025: case 0x20121025:
// Announcement // Announcement
//lock.Lock() lock.Lock()
ip := addr.IP.To4() ip := addr.IP.To4()
if ip == nil { if ip == nil {
ip = addr.IP.To16() ip = addr.IP.To16()
} }
node := Node{ip, uint16(pkt.Port)} node := Node{
log.Println("<-", pkt.ID, node) IP: ip,
Port: uint16(pkt.Port),
Updated: time.Now(),
}
//log.Println("<-", pkt.ID, node)
nodes[pkt.ID] = node nodes[pkt.ID] = node
//lock.Unlock() lock.Unlock()
case 0x19760309: case 0x19760309:
// Query // Query
//lock.Lock() lock.Lock()
node, ok := nodes[pkt.ID] node, ok := nodes[pkt.ID]
//lock.Unlock() queries++
lock.Unlock()
if ok { if ok {
pkt := discover.Packet{ pkt := discover.Packet{
Magic: 0x20121025, Magic: 0x20121025,
@ -64,11 +92,8 @@ func main() {
_, _, err = conn.WriteMsgUDP(discover.EncodePacket(pkt), nil, addr) _, _, err = conn.WriteMsgUDP(discover.EncodePacket(pkt), nil, addr)
if err != nil { if err != nil {
log.Println("Warning:", err) log.Println("Warning:", err)
} else {
log.Println("->", pkt.ID, node)
} }
} }
} }
} }
} }