diff options
| author | Shulhan <ms@kilabit.info> | 2025-07-31 22:32:29 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2025-08-01 00:39:45 +0700 |
| commit | 5a90a6822fc0d6a0d0d07b4b4583f9dbc20ba0e6 (patch) | |
| tree | 299946be1f991f73aabbb34804d60422896d368a | |
| parent | bec99d46d65c451f18000a7247d3df06765a3894 (diff) | |
| download | lilin-5a90a6822fc0d6a0d0d07b4b4583f9dbc20ba0e6.tar.xz | |
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.
| -rw-r--r-- | service_report.go | 24 | ||||
| -rw-r--r-- | service_report_test.go | 30 | ||||
| -rw-r--r-- | worker.go | 7 |
3 files changed, 56 insertions, 5 deletions
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 <ms@kilabit.info> +// 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) +} @@ -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{}{} |
