mirror of
https://github.com/Luzifer/webcheck.git
synced 2025-01-06 18:46:08 +00:00
Fix: Limit historical performance data
in order not to consume an endless amount of memory Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
0a3219ce4a
commit
4bffea4f10
3 changed files with 64 additions and 6 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1 +1,2 @@
|
||||||
request-log
|
request-log
|
||||||
|
webcheck
|
||||||
|
|
18
main.go
18
main.go
|
@ -19,7 +19,10 @@ import (
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
const dateFormat = time.RFC1123
|
const (
|
||||||
|
dateFormat = time.RFC1123
|
||||||
|
numHistoricalDurations = 300
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
cfg = struct {
|
cfg = struct {
|
||||||
|
@ -54,7 +57,7 @@ const (
|
||||||
|
|
||||||
type checkResult struct {
|
type checkResult struct {
|
||||||
DumpFile string
|
DumpFile string
|
||||||
Durations []time.Duration
|
Durations *ringDuration
|
||||||
Message string
|
Message string
|
||||||
Start time.Time
|
Start time.Time
|
||||||
Status checkStatus
|
Status checkStatus
|
||||||
|
@ -64,8 +67,11 @@ type checkResult struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func newCheckResult(status checkStatus, message string, duration time.Duration) *checkResult {
|
func newCheckResult(status checkStatus, message string, duration time.Duration) *checkResult {
|
||||||
|
r := newRingDuration(numHistoricalDurations)
|
||||||
|
r.SetNext(duration)
|
||||||
|
|
||||||
return &checkResult{
|
return &checkResult{
|
||||||
Durations: []time.Duration{duration},
|
Durations: r,
|
||||||
Message: message,
|
Message: message,
|
||||||
Start: time.Now(),
|
Start: time.Now(),
|
||||||
Status: status,
|
Status: status,
|
||||||
|
@ -76,7 +82,7 @@ func (c *checkResult) AddDuration(d time.Duration) {
|
||||||
c.lock.Lock()
|
c.lock.Lock()
|
||||||
defer c.lock.Unlock()
|
defer c.lock.Unlock()
|
||||||
|
|
||||||
c.Durations = append(c.Durations, d)
|
c.Durations.SetNext(d)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *checkResult) DurationStats() string {
|
func (c *checkResult) DurationStats() string {
|
||||||
|
@ -84,7 +90,7 @@ func (c *checkResult) DurationStats() string {
|
||||||
defer c.lock.RUnlock()
|
defer c.lock.RUnlock()
|
||||||
|
|
||||||
var (
|
var (
|
||||||
s = stats.LoadRawData(c.Durations)
|
s = stats.LoadRawData(c.Durations.GetAll())
|
||||||
min, avg, max float64
|
min, avg, max float64
|
||||||
err error
|
err error
|
||||||
)
|
)
|
||||||
|
@ -176,7 +182,7 @@ func main() {
|
||||||
lastResult.DumpFile = fn
|
lastResult.DumpFile = fn
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
lastResult.AddDuration(result.Durations[0])
|
lastResult.AddDuration(result.Durations.GetCurrent())
|
||||||
}
|
}
|
||||||
|
|
||||||
lastResult.Print()
|
lastResult.Print()
|
||||||
|
|
51
ring.go
Normal file
51
ring.go
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ringDuration struct {
|
||||||
|
store []time.Duration
|
||||||
|
current int
|
||||||
|
|
||||||
|
lock sync.RWMutex
|
||||||
|
}
|
||||||
|
|
||||||
|
func newRingDuration(maxLen int) *ringDuration {
|
||||||
|
return &ringDuration{
|
||||||
|
store: make([]time.Duration, 0, maxLen),
|
||||||
|
current: -1, // Initial value to start the ring with element 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *ringDuration) SetNext(i time.Duration) {
|
||||||
|
r.lock.Lock()
|
||||||
|
defer r.lock.Unlock()
|
||||||
|
|
||||||
|
next := r.current + 1
|
||||||
|
if next == cap(r.store) {
|
||||||
|
next = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
if next == len(r.store) {
|
||||||
|
r.store = append(r.store, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
r.store[next] = i
|
||||||
|
r.current = next
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *ringDuration) GetAll() []time.Duration {
|
||||||
|
r.lock.RLock()
|
||||||
|
defer r.lock.RUnlock()
|
||||||
|
|
||||||
|
return r.store
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *ringDuration) GetCurrent() time.Duration {
|
||||||
|
r.lock.RLock()
|
||||||
|
defer r.lock.RUnlock()
|
||||||
|
|
||||||
|
return r.store[r.current]
|
||||||
|
}
|
Loading…
Reference in a new issue