Rewrite network tree saving and cleaning procedures
This commit is contained in:
@@ -5,35 +5,33 @@ package network
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/Safing/portbase/log"
|
||||
"github.com/Safing/portmaster/process"
|
||||
)
|
||||
|
||||
var (
|
||||
cleanerTickDuration = 10 * time.Second
|
||||
deadLinksTimeout = 3 * time.Minute
|
||||
thresholdDuration = 3 * time.Minute
|
||||
cleanerTickDuration = 10 * time.Second
|
||||
deleteLinksAfterEndedThreshold = 5 * time.Minute
|
||||
deleteCommsWithoutLinksThreshhold = 3 * time.Minute
|
||||
lastEstablishedUpdateThreshold = 30 * time.Second
|
||||
)
|
||||
|
||||
func cleaner() {
|
||||
for {
|
||||
time.Sleep(cleanerTickDuration)
|
||||
|
||||
cleanLinks()
|
||||
time.Sleep(2 * time.Second)
|
||||
cleanComms()
|
||||
time.Sleep(2 * time.Second)
|
||||
cleanProcesses()
|
||||
activeComms := cleanLinks()
|
||||
activeProcs := cleanComms(activeComms)
|
||||
process.CleanProcessStorage(activeProcs)
|
||||
}
|
||||
}
|
||||
|
||||
func cleanLinks() {
|
||||
func cleanLinks() (activeComms map[string]struct{}) {
|
||||
activeComms = make(map[string]struct{})
|
||||
activeIDs := process.GetActiveConnectionIDs()
|
||||
|
||||
now := time.Now().Unix()
|
||||
deleteOlderThan := time.Now().Add(-deadLinksTimeout).Unix()
|
||||
|
||||
// log.Tracef("network.clean: now=%d", now)
|
||||
// log.Tracef("network.clean: deleteOlderThan=%d", deleteOlderThan)
|
||||
deleteOlderThan := time.Now().Add(-deleteLinksAfterEndedThreshold).Unix()
|
||||
|
||||
linksLock.RLock()
|
||||
defer linksLock.RUnlock()
|
||||
@@ -42,18 +40,21 @@ func cleanLinks() {
|
||||
for key, link := range links {
|
||||
|
||||
// delete dead links
|
||||
if link.Ended > 0 {
|
||||
link.Lock()
|
||||
deleteThis := link.Ended < deleteOlderThan
|
||||
link.Unlock()
|
||||
if deleteThis {
|
||||
// log.Tracef("network.clean: deleted %s", link.DatabaseKey())
|
||||
go link.Delete()
|
||||
}
|
||||
|
||||
link.Lock()
|
||||
deleteThis := link.Ended > 0 && link.Ended < deleteOlderThan
|
||||
link.Unlock()
|
||||
if deleteThis {
|
||||
log.Tracef("network.clean: deleted %s (ended at %d)", link.DatabaseKey(), link.Ended)
|
||||
go link.Delete()
|
||||
continue
|
||||
}
|
||||
|
||||
// not yet deleted, so its still a valid link regarding link count
|
||||
comm := link.Communication()
|
||||
comm.Lock()
|
||||
markActive(activeComms, comm.DatabaseKey())
|
||||
comm.Unlock()
|
||||
|
||||
// check if link is dead
|
||||
found = false
|
||||
for _, activeID := range activeIDs {
|
||||
@@ -63,31 +64,53 @@ func cleanLinks() {
|
||||
}
|
||||
}
|
||||
|
||||
// mark end time
|
||||
if !found {
|
||||
// mark end time
|
||||
link.Lock()
|
||||
link.Ended = now
|
||||
// log.Tracef("network.clean: marked %s as ended.", link.DatabaseKey())
|
||||
go link.Save()
|
||||
link.Unlock()
|
||||
log.Tracef("network.clean: marked %s as ended", link.DatabaseKey())
|
||||
go link.save()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func cleanComms() {
|
||||
func cleanComms(activeLinks map[string]struct{}) (activeComms map[string]struct{}) {
|
||||
activeComms = make(map[string]struct{})
|
||||
|
||||
commsLock.RLock()
|
||||
defer commsLock.RUnlock()
|
||||
|
||||
threshold := time.Now().Add(-thresholdDuration).Unix()
|
||||
threshold := time.Now().Add(-deleteCommsWithoutLinksThreshhold).Unix()
|
||||
for _, comm := range comms {
|
||||
// has links?
|
||||
_, hasLinks := activeLinks[comm.DatabaseKey()]
|
||||
|
||||
// comm created
|
||||
comm.Lock()
|
||||
if comm.FirstLinkEstablished < threshold && comm.LinkCount == 0 {
|
||||
// log.Tracef("network.clean: deleted %s", comm.DatabaseKey())
|
||||
go comm.Delete()
|
||||
}
|
||||
created := comm.Meta().Created
|
||||
comm.Unlock()
|
||||
|
||||
if !hasLinks && created < threshold {
|
||||
log.Tracef("network.clean: deleted %s", comm.DatabaseKey())
|
||||
go comm.Delete()
|
||||
} else {
|
||||
p := comm.Process()
|
||||
p.Lock()
|
||||
markActive(activeComms, p.DatabaseKey())
|
||||
p.Unlock()
|
||||
}
|
||||
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func cleanProcesses() {
|
||||
process.CleanProcessStorage(thresholdDuration)
|
||||
func markActive(activeMap map[string]struct{}, key string) {
|
||||
_, ok := activeMap[key]
|
||||
if !ok {
|
||||
activeMap[key] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user