summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJakob Borg <jakob@kastelo.net>2024-09-10 19:41:22 +0200
committerJakob Borg <jakob@kastelo.net>2024-09-13 08:48:04 +0200
commitb794726e1fc172e699dede1c33a3cbc65263e598 (patch)
tree00f121cf3021d9806aa485e8db19767451d7e869
parent3d59740a0a151d199aa48d9e6ea754c0abf988a2 (diff)
chore(stdiscosrv): sched in loop
-rw-r--r--cmd/stdiscosrv/database.go16
-rw-r--r--cmd/stdiscosrv/main.go2
2 files changed, 16 insertions, 2 deletions
diff --git a/cmd/stdiscosrv/database.go b/cmd/stdiscosrv/database.go
index c016a5ec8d..e36fa72cb9 100644
--- a/cmd/stdiscosrv/database.go
+++ b/cmd/stdiscosrv/database.go
@@ -14,12 +14,14 @@ import (
"cmp"
"context"
"encoding/binary"
+ "errors"
"io"
"log"
"net"
"net/url"
"os"
"path"
+ "runtime"
"slices"
"time"
@@ -159,7 +161,13 @@ func (s *inMemoryStore) calculateStatistics() {
cutoff1w := t0.Add(-7 * 24 * time.Hour).UnixNano()
current, currentIPv4, currentIPv6, last24h, last1w, errors := 0, 0, 0, 0, 0, 0
+ n := 0
s.m.Range(func(key protocol.DeviceID, rec DatabaseRecord) bool {
+ if n%1000 == 0 {
+ runtime.Gosched()
+ }
+ n++
+
// If there are addresses that have not expired it's a current
// record, otherwise account it based on when it was last seen
// (last 24 hours or last week) or finally as inactice.
@@ -234,7 +242,13 @@ func (s *inMemoryStore) write() (err error) {
var rangeErr error
now := s.clock.Now().UnixNano()
cutoff1w := s.clock.Now().Add(-7 * 24 * time.Hour).UnixNano()
+ n := 0
s.m.Range(func(key protocol.DeviceID, value DatabaseRecord) bool {
+ if n%1000 == 0 {
+ runtime.Gosched()
+ }
+ n++
+
if value.Seen < cutoff1w {
// drop the record if it's older than a week
return true
@@ -306,7 +320,7 @@ func (s *inMemoryStore) read() error {
for {
var n uint32
if err := binary.Read(br, binary.BigEndian, &n); err != nil {
- if err == io.EOF {
+ if errors.Is(err, io.EOF) {
break
}
return err
diff --git a/cmd/stdiscosrv/main.go b/cmd/stdiscosrv/main.go
index 330cb049b9..9f118ab162 100644
--- a/cmd/stdiscosrv/main.go
+++ b/cmd/stdiscosrv/main.go
@@ -74,7 +74,7 @@ func main() {
var flushInterval time.Duration
log.SetOutput(os.Stdout)
- log.SetFlags(0)
+ // log.SetFlags(0)
flag.StringVar(&certFile, "cert", "./cert.pem", "Certificate file")
flag.StringVar(&keyFile, "key", "./key.pem", "Key file")