simplify API

This commit is contained in:
2025-09-23 08:59:10 +03:00
parent 0f2f9144ed
commit 1d381420bf
5 changed files with 72 additions and 146 deletions

View File

@@ -16,7 +16,7 @@ var (
// Watchdog keeps checks to run either periodically
// or on demand.
type Watchdog struct {
checks checksMap
checks map[string]*wdCheck
mu sync.Mutex
events chan CheckResult // output channel
@@ -28,47 +28,6 @@ type Watchdog struct {
running int // number of active checks monitored
}
type checksMap struct {
m map[string]*wdCheck
}
func (c *checksMap) build() {
if c.m == nil {
c.m = make(map[string]*wdCheck)
}
}
func (c *checksMap) Map() map[string]*wdCheck {
c.build()
return c.m
}
func (c *checksMap) Set(key string, v *wdCheck) {
c.build()
c.m[key] = v
}
func (c *checksMap) Lookup(key string) (*wdCheck, bool) {
c.build()
v, ok := c.m[key]
return v, ok
}
func (c *checksMap) Delete(key string) {
c.build()
delete(c.m, key)
}
func (c *checksMap) Len() int {
c.build()
return len(c.m)
}
type wdCheck struct {
check Check
stop chan struct{}
@@ -77,13 +36,16 @@ type wdCheck struct {
// New creates instance of Watchdog with
// provided checks.
func New(checks ...Check) *Watchdog {
w := Watchdog{}
w := Watchdog{
checks: make(map[string]*wdCheck),
}
for _, c := range checks {
nc := &wdCheck{
check: c,
}
w.checks.Set(c.Name, nc)
w.checks[c.Name] = nc
}
return &w
@@ -121,14 +83,18 @@ func (w *Watchdog) AddChecks(checks ...Check) {
w.mu.Lock()
defer w.mu.Unlock()
if w.checks == nil {
w.checks = make(map[string]*wdCheck)
}
for _, c := range checks {
nc := &wdCheck{
check: c,
}
old, haveOld := w.checks.Lookup(c.Name)
old, haveOld := w.checks[c.Name]
w.checks.Set(c.Name, nc)
w.checks[c.Name] = nc
if w.monitoring {
w.startMonitoring(nc)
@@ -146,7 +112,7 @@ func (w *Watchdog) RemoveChecks(names ...string) {
defer w.mu.Unlock()
for _, name := range names {
c, ok := w.checks.Lookup(name)
c, ok := w.checks[name]
if !ok {
continue
}
@@ -155,7 +121,7 @@ func (w *Watchdog) RemoveChecks(names ...string) {
w.stopMonitoring(c)
}
w.checks.Delete(name)
delete(w.checks, name)
}
}
@@ -172,7 +138,7 @@ func (w *Watchdog) Start(concurrency int) (<-chan CheckResult, error) {
w.mu.Lock()
defer w.mu.Unlock()
if w.checks.Len() == 0 {
if len(w.checks) == 0 {
return nil, ErrNotConfigured
}
@@ -181,7 +147,7 @@ func (w *Watchdog) Start(concurrency int) (<-chan CheckResult, error) {
}
if concurrency == 0 {
concurrency = w.checks.Len()
concurrency = len(w.checks)
}
if w.timeout == 0 {
@@ -191,7 +157,7 @@ func (w *Watchdog) Start(concurrency int) (<-chan CheckResult, error) {
w.events = make(chan CheckResult, concurrency)
w.limiter = make(chan struct{}, concurrency)
for _, c := range w.checks.Map() {
for _, c := range w.checks {
w.startMonitoring(c)
}
@@ -208,7 +174,7 @@ func (w *Watchdog) Stop() error {
return ErrNotRunning
}
for _, c := range w.checks.Map() {
for _, c := range w.checks {
w.stopMonitoring(c)
}
@@ -221,13 +187,13 @@ func (w *Watchdog) Stop() error {
func (w *Watchdog) RunImmediately(ctx context.Context, concurrency int) ([]CheckResult, error) {
w.mu.Lock()
if w.checks.Len() == 0 {
if len(w.checks) == 0 {
w.mu.Unlock()
return nil, ErrNotConfigured
}
cp := w.copyChecks()
w.mu.Unlock() // release
w.mu.Unlock()
if concurrency == 0 {
concurrency = len(cp)
@@ -248,8 +214,8 @@ func (w *Watchdog) RunImmediately(ctx context.Context, concurrency int) ([]Check
}
func (w *Watchdog) copyChecks() []Check {
cp := make([]Check, 0, w.checks.Len())
for _, v := range w.checks.Map() {
cp := make([]Check, 0, len(w.checks))
for _, v := range w.checks {
cp = append(cp, v.check)
}
@@ -267,11 +233,7 @@ func (w *Watchdog) startMonitoring(wdc *wdCheck) {
w.running++
go func() {
state := CheckResult{
// on first run return anything
// other that OK
Status: StatusOK,
}
var curr error = nil
ticker := time.Tick(wdc.check.Interval)
@@ -281,23 +243,22 @@ func (w *Watchdog) startMonitoring(wdc *wdCheck) {
ctx, cancel := context.WithTimeout(context.Background(), w.timeout)
defer cancel()
status, err := c.Check(ctx)
err := c.Check(ctx)
<-w.limiter
r := CheckResult{
Name: c.Name,
Status: status,
Error: err,
Name: c.Name,
Error: err,
}
// if status changed or we've got an error
// then report this
if r.Status != state.Status || r.Error != nil {
if !errors.Is(r.Error, curr) {
w.events <- r
}
state = r
curr = r.Error
select {
case <-ticker:
@@ -336,12 +297,11 @@ func runChecksConcurrently(ctx context.Context, ch []Check, concurrency int) []C
// relying on assumption that CheckFunc obeys context
// cancellation
status, err := e.Check(ctx)
err := e.Check(ctx)
r := CheckResult{
Name: e.Name,
Status: status,
Error: err,
Name: e.Name,
Error: err,
}
done <- r