chore(stdiscosrv): sched in loop
This commit is contained in:
@@ -14,12 +14,14 @@ import (
|
|||||||
"cmp"
|
"cmp"
|
||||||
"context"
|
"context"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"runtime"
|
||||||
"slices"
|
"slices"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -159,7 +161,13 @@ func (s *inMemoryStore) calculateStatistics() {
|
|||||||
cutoff1w := t0.Add(-7 * 24 * time.Hour).UnixNano()
|
cutoff1w := t0.Add(-7 * 24 * time.Hour).UnixNano()
|
||||||
current, currentIPv4, currentIPv6, last24h, last1w, errors := 0, 0, 0, 0, 0, 0
|
current, currentIPv4, currentIPv6, last24h, last1w, errors := 0, 0, 0, 0, 0, 0
|
||||||
|
|
||||||
|
n := 0
|
||||||
s.m.Range(func(key protocol.DeviceID, rec DatabaseRecord) bool {
|
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
|
// If there are addresses that have not expired it's a current
|
||||||
// record, otherwise account it based on when it was last seen
|
// record, otherwise account it based on when it was last seen
|
||||||
// (last 24 hours or last week) or finally as inactice.
|
// (last 24 hours or last week) or finally as inactice.
|
||||||
@@ -234,7 +242,13 @@ func (s *inMemoryStore) write() (err error) {
|
|||||||
var rangeErr error
|
var rangeErr error
|
||||||
now := s.clock.Now().UnixNano()
|
now := s.clock.Now().UnixNano()
|
||||||
cutoff1w := s.clock.Now().Add(-7 * 24 * time.Hour).UnixNano()
|
cutoff1w := s.clock.Now().Add(-7 * 24 * time.Hour).UnixNano()
|
||||||
|
n := 0
|
||||||
s.m.Range(func(key protocol.DeviceID, value DatabaseRecord) bool {
|
s.m.Range(func(key protocol.DeviceID, value DatabaseRecord) bool {
|
||||||
|
if n%1000 == 0 {
|
||||||
|
runtime.Gosched()
|
||||||
|
}
|
||||||
|
n++
|
||||||
|
|
||||||
if value.Seen < cutoff1w {
|
if value.Seen < cutoff1w {
|
||||||
// drop the record if it's older than a week
|
// drop the record if it's older than a week
|
||||||
return true
|
return true
|
||||||
@@ -306,7 +320,7 @@ func (s *inMemoryStore) read() error {
|
|||||||
for {
|
for {
|
||||||
var n uint32
|
var n uint32
|
||||||
if err := binary.Read(br, binary.BigEndian, &n); err != nil {
|
if err := binary.Read(br, binary.BigEndian, &n); err != nil {
|
||||||
if err == io.EOF {
|
if errors.Is(err, io.EOF) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ func main() {
|
|||||||
var flushInterval time.Duration
|
var flushInterval time.Duration
|
||||||
|
|
||||||
log.SetOutput(os.Stdout)
|
log.SetOutput(os.Stdout)
|
||||||
log.SetFlags(0)
|
// log.SetFlags(0)
|
||||||
|
|
||||||
flag.StringVar(&certFile, "cert", "./cert.pem", "Certificate file")
|
flag.StringVar(&certFile, "cert", "./cert.pem", "Certificate file")
|
||||||
flag.StringVar(&keyFile, "key", "./key.pem", "Key file")
|
flag.StringVar(&keyFile, "key", "./key.pem", "Key file")
|
||||||
|
|||||||
Reference in New Issue
Block a user