Newer
Older
package probes
import (
"fmt"
"sync"
"time"
)
type Result struct {
ID string
Spec SpecCommon
Ok bool
Start time.Time
Duration time.Duration
Logs string
}
// Nice displayable string.
func (r *Result) String() string {
return fmt.Sprintf("%s(%s)", r.Spec.Name, r.ID)
}
type ResultStore interface {
Push(*Result)
DoOk(func(*Result))
DoErrs(func(*Result))
Find(string) *Result
}
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Very simple overwriting circular buffer of results.
type resultBuffer struct {
buf []*Result
sz, head int
}
func newResultBuffer(sz int) *resultBuffer {
return &resultBuffer{
buf: make([]*Result, sz),
sz: sz,
}
}
func (b *resultBuffer) Push(result *Result) {
b.buf[b.head] = result
b.head++
if b.head >= b.sz {
b.head -= b.sz
}
}
func (b *resultBuffer) Each(f func(*Result)) {
for i := 0; i < b.sz; i++ {
pos := b.head + i
if pos >= b.sz {
pos -= b.sz
}
if b.buf[pos] != nil {
f(b.buf[pos])
}
}
}
// ResultStore that keeps recent results in memory.
last *resultBuffer
errs *resultBuffer
}
func NewResultStore(numOk, numErrs int) ResultStore {
return &memResultStore{
last: newResultBuffer(numOk),
errs: newResultBuffer(numErrs),
}
}
func (r *memResultStore) Push(result *Result) {
r.mx.Lock()
}
r.mx.Unlock()
}
func (r *memResultStore) DoOk(f func(*Result)) {
r.mx.Lock()
r.mx.Unlock()
}
func (r *memResultStore) DoErrs(f func(*Result)) {
r.mx.Lock()
r.mx.Unlock()
}
func (r *memResultStore) Find(id string) (out *Result) {
r.mx.Lock()
r.last.Each(func(r *Result) {
if r.ID == id {
out = r