From 5a90a6822fc0d6a0d0d07b4b4583f9dbc20ba0e6 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Thu, 31 Jul 2025 22:32:29 +0700 Subject: all: limit the history in ServiceReport When the service keep scanning longer than let say 1 day, the history of service report keep growing. We did not want this history consume memory, so at one point we need to purge it. This changes limit the maximum history to 120 items. --- service_report.go | 24 ++++++++++++++++++++++++ service_report_test.go | 30 ++++++++++++++++++++++++++++++ worker.go | 7 ++----- 3 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 service_report_test.go diff --git a/service_report.go b/service_report.go index 2627bc2..291cd2b 100644 --- a/service_report.go +++ b/service_report.go @@ -3,9 +3,33 @@ package lilin +// historyKeepSize maximum report to keep in history. +const historyKeepSize = 120 + +// historyMax limit of history before its being purged to historyKeepSize. +const historyMax = historyKeepSize * 2 + // ServiceReport contains the scan report for single service. type ServiceReport struct { Name string Last ScanReport History []ScanReport } + +func NewServiceReport(name string) (svcReport *ServiceReport) { + svcReport = &ServiceReport{ + Name: name, + History: make([]ScanReport, 0, historyMax), + } + return svcReport +} + +func (svcReport *ServiceReport) Store(scanReport ScanReport) { + svcReport.Last = scanReport + svcReport.History = append(svcReport.History, scanReport) + if len(svcReport.History) == historyMax { + var start = len(svcReport.History) - historyKeepSize + copy(svcReport.History, svcReport.History[start:]) + svcReport.History = svcReport.History[:historyKeepSize-1] + } +} diff --git a/service_report_test.go b/service_report_test.go new file mode 100644 index 0000000..71652f6 --- /dev/null +++ b/service_report_test.go @@ -0,0 +1,30 @@ +// SPDX-FileCopyrightText: 2025 M. Shulhan +// SPDX-License-Identifier: GPL-3.0-only + +package lilin + +import ( + "testing" + "time" + + "git.sr.ht/~shulhan/pakakeh.go/lib/test" +) + +func TestServiceReport_push(t *testing.T) { + var svcReport = NewServiceReport(`test`) + var x int64 + for x = 1; x <= historyMax+1; x++ { + svcReport.Store(ScanReport{At: time.Unix(x, 0)}) + } + test.Assert(t, `len(History)`, historyKeepSize, len(svcReport.History)) + test.Assert(t, `cap(History)`, historyMax, cap(svcReport.History)) + + var expAtUnix int64 = int64(historyKeepSize + 1) + var gotAtUnix = svcReport.History[0].At.Unix() + test.Assert(t, `History[0].At`, expAtUnix, gotAtUnix) + + expAtUnix = int64(historyMax) + 1 + var lastIdx = len(svcReport.History) - 1 + gotAtUnix = svcReport.History[lastIdx].At.Unix() + test.Assert(t, `History[last].At`, expAtUnix, gotAtUnix) +} diff --git a/worker.go b/worker.go index f3348a7..48c78c6 100644 --- a/worker.go +++ b/worker.go @@ -37,9 +37,7 @@ func newWorker(configDir string) (wrk *worker, err error) { Services: make(map[string]*ServiceReport, len(wrk.Services)), } for name := range wrk.Services { - wrk.Reports.Services[name] = &ServiceReport{ - Name: name, - } + wrk.Reports.Services[name] = NewServiceReport(name) } return wrk, nil } @@ -119,8 +117,7 @@ func (wrk *worker) start(updateq chan<- struct{}) { wrk.reportsMutex.Lock() var svcReport = wrk.Reports.Services[scanReport.Name] - svcReport.Last = scanReport - svcReport.History = append(svcReport.History, scanReport) + svcReport.Store(scanReport) wrk.reportsMutex.Unlock() updateq <- struct{}{} -- cgit v1.3