1
0
mirror of https://github.com/Luzifer/webcheck.git synced 2024-09-16 14:18:26 +00:00
webcheck/ring.go
Knut Ahlers 4bffea4f10
Fix: Limit historical performance data
in order not to consume an endless amount of memory

Signed-off-by: Knut Ahlers <knut@ahlers.me>
2018-07-24 13:36:12 +02:00

52 lines
806 B
Go

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]
}