better cleanup of maps

This commit is contained in:
Arceliar 2018-11-25 12:25:38 -06:00
parent 8d6beebac4
commit 4e156bd4f7
6 changed files with 53 additions and 18 deletions

View File

@ -1,4 +1,4 @@
#!/bin/bash
export GOPATH=$PWD
go get -d yggdrasil
go run -tags debug misc/sim/treesim.go
go run -tags debug misc/sim/treesim.go "$@"

View File

@ -8,6 +8,7 @@ import "strconv"
import "time"
import "log"
import "runtime"
import "runtime/pprof"
import "flag"
@ -280,17 +281,7 @@ func pingNodes(store map[[32]byte]*Node) {
}
destAddr := dest.core.DEBUG_getAddr()[:]
ticker := time.NewTicker(150 * time.Millisecond)
ch := make(chan bool, 1)
ch <- true
doTicker := func() {
for range ticker.C {
select {
case ch <- true:
default:
}
}
}
go doTicker()
sendTo(payload, destAddr)
for loop := true; loop; {
select {
case packet := <-dest.recv:
@ -299,7 +290,7 @@ func pingNodes(store map[[32]byte]*Node) {
loop = false
}
}
case <-ch:
case <-ticker.C:
sendTo(payload, destAddr)
//dumpDHTSize(store) // note that this uses racey functions to read things...
}
@ -458,4 +449,5 @@ func main() {
var block chan struct{}
<-block
}
runtime.GC()
}

View File

@ -273,19 +273,23 @@ func (t *dht) ping(info *dhtInfo, target *NodeID) {
// Periodic maintenance work to keep important DHT nodes alive.
func (t *dht) doMaintenance() {
now := time.Now()
newReqs := make(map[boxPubKey]map[NodeID]time.Time, len(t.reqs))
for key, dests := range t.reqs {
newDests := make(map[NodeID]time.Time, len(dests))
for nodeID, start := range dests {
if now.Sub(start) > 6*time.Second {
if info, isIn := t.table[*getNodeID(&key)]; isIn {
info.pings++
}
delete(dests, nodeID)
}
if len(dests) == 0 {
delete(t.reqs, key)
continue
}
newDests[nodeID] = start
}
if len(newDests) > 0 {
newReqs[key] = newDests
}
}
t.reqs = newReqs
for infoID, info := range t.table {
if now.Sub(info.recv) > time.Minute || info.pings > 3 {
delete(t.table, infoID)

View File

@ -175,7 +175,6 @@ func (p *peer) doSendSwitchMsgs() {
// This must be launched in a separate goroutine by whatever sets up the peer struct.
// It handles link protocol traffic.
func (p *peer) linkLoop() {
go p.doSendSwitchMsgs()
tick := time.NewTicker(time.Second)
defer tick.Stop()
for {

View File

@ -311,6 +311,11 @@ func (ss *sessions) createSession(theirPermKey *boxPubKey) *sessionInfo {
func (ss *sessions) cleanup() {
// Time thresholds almost certainly could use some adjusting
for k := range ss.permShared {
// Delete a key, to make sure this eventually shrinks to 0
delete(ss.permShared, k)
break
}
if time.Since(ss.lastCleanup) < time.Minute {
return
}
@ -319,6 +324,36 @@ func (ss *sessions) cleanup() {
s.close()
}
}
permShared := make(map[boxPubKey]*boxSharedKey, len(ss.permShared))
for k, v := range ss.permShared {
permShared[k] = v
}
ss.permShared = permShared
sinfos := make(map[handle]*sessionInfo, len(ss.sinfos))
for k, v := range ss.sinfos {
sinfos[k] = v
}
ss.sinfos = sinfos
byMySes := make(map[boxPubKey]*handle, len(ss.byMySes))
for k, v := range ss.byMySes {
byMySes[k] = v
}
ss.byMySes = byMySes
byTheirPerm := make(map[boxPubKey]*handle, len(ss.byTheirPerm))
for k, v := range ss.byTheirPerm {
byTheirPerm[k] = v
}
ss.byTheirPerm = byTheirPerm
addrToPerm := make(map[address]*boxPubKey, len(ss.addrToPerm))
for k, v := range ss.addrToPerm {
addrToPerm[k] = v
}
ss.addrToPerm = addrToPerm
subnetToPerm := make(map[subnet]*boxPubKey, len(ss.subnetToPerm))
for k, v := range ss.subnetToPerm {
subnetToPerm[k] = v
}
ss.subnetToPerm = subnetToPerm
ss.lastCleanup = time.Now()
}

View File

@ -86,5 +86,10 @@ func (m *sigManager) cleanup() {
delete(m.checked, s)
}
}
newChecked := make(map[sigBytes]knownSig, len(m.checked))
for s, k := range m.checked {
newChecked[s] = k
}
m.checked = newChecked
m.lastCleaned = time.Now()
}