kubeprobes/query.go

55 lines
969 B
Go
Raw Permalink Normal View History

2023-07-21 23:12:18 +02:00
package kubeprobes
import "sync"
type statusQuery struct {
ok bool
passed []statusEntry
failed []statusEntry
mux sync.Mutex
wg sync.WaitGroup
}
type statusEntry struct {
Probe string `json:"probe"`
Status error `json:"status,omitempty"`
2023-07-21 23:12:18 +02:00
}
func (sq *statusQuery) wait() {
2023-07-21 23:12:18 +02:00
sq.wg.Wait()
}
func newStatusQuery(probes []Probe) *statusQuery {
2023-07-21 23:12:18 +02:00
sq := &statusQuery{
ok: true,
passed: make([]statusEntry, 0, len(probes)),
failed: make([]statusEntry, 0, len(probes)),
mux: sync.Mutex{},
wg: sync.WaitGroup{},
2023-07-21 23:12:18 +02:00
}
sq.wg.Add(len(probes))
for _, probe := range probes {
probe := probe
go func() {
defer sq.wg.Done()
sq.mux.Lock()
if err := probe.status(); err != nil {
sq.ok = false
sq.failed = append(sq.failed, statusEntry{
Probe: probe.name(),
Status: err,
})
} else {
sq.passed = append(sq.passed, statusEntry{
Probe: probe.name(),
})
2023-07-21 23:12:18 +02:00
}
sq.mux.Unlock()
2023-07-21 23:12:18 +02:00
}()
}
return sq
}