feat: v0.4.0 simplify

This commit is contained in:
2025-09-23 23:14:13 +03:00
parent 214fda877e
commit fc3678f758
6 changed files with 198 additions and 174 deletions

View File

@@ -16,17 +16,16 @@ var (
// Watchdog keeps checks to run either periodically
// or on demand.
type Watchdog struct {
checks []*wdCheck
checks map[string]*wdCheck
mu sync.Mutex
monitoring bool // is monitoring currently in progress
events chan CheckResult // output channel
limiter chan struct{} // TODO: use proper limiter here
timeout time.Duration
timeout time.Duration // timeout for checks to complete
running int
monitoring bool // is monitoring currently in progress
running int // number of active checks monitored
}
type wdCheck struct {
@@ -37,29 +36,26 @@ type wdCheck struct {
// New creates instance of Watchdog with
// provided checks.
func New(checks ...Check) *Watchdog {
ch := make([]*wdCheck, len(checks))
w := Watchdog{
checks: make(map[string]*wdCheck),
}
for i := range checks {
ch[i] = &wdCheck{
check: checks[i],
for _, c := range checks {
nc := &wdCheck{
check: c,
}
w.checks[c.Name] = nc
}
w := &Watchdog{
checks: ch,
}
return w
return &w
}
func (w *Watchdog) ListChecks() []Check {
w.mu.Lock()
defer w.mu.Unlock()
out := make([]Check, len(w.checks))
for i := range w.checks {
out[i] = w.checks[i].check
}
out := w.copyChecks()
return out
}
@@ -81,20 +77,31 @@ func (w *Watchdog) SetTimeout(d time.Duration) {
// AddChecks adds checks to the group.
// If monitoring is in progress then monitoring it started for the newly added
// check as well.
// Check may have duplicate Name fields but note that RemoveChecks removes checks
// by their Name fields.
// Check may have not have duplicate Name fields. New check with the same
// hame overwrites the previous one.
func (w *Watchdog) AddChecks(checks ...Check) {
w.mu.Lock()
defer w.mu.Unlock()
for i := range checks {
if w.checks == nil {
w.checks = make(map[string]*wdCheck)
}
for _, c := range checks {
nc := &wdCheck{
check: checks[i],
check: c,
}
w.checks = append(w.checks, nc)
old, haveOld := w.checks[c.Name]
w.checks[c.Name] = nc
if w.monitoring {
w.startMonitoring(nc)
if haveOld {
w.stopMonitoring(old)
}
}
}
}
@@ -104,19 +111,18 @@ func (w *Watchdog) RemoveChecks(names ...string) {
w.mu.Lock()
defer w.mu.Unlock()
remaining := make([]*wdCheck, 0, len(w.checks)-len(names))
for _, c := range w.checks {
if slices.Contains(names, c.check.Name) {
if w.monitoring {
w.stopMonitoring(c)
}
for _, name := range names {
c, ok := w.checks[name]
if !ok {
continue
}
remaining = append(remaining, c)
}
if w.monitoring {
w.stopMonitoring(c)
}
w.checks = remaining
delete(w.checks, name)
}
}
// Start starts monitoring.
@@ -151,12 +157,10 @@ func (w *Watchdog) Start(concurrency int) (<-chan CheckResult, error) {
w.events = make(chan CheckResult, concurrency)
w.limiter = make(chan struct{}, concurrency)
for i := range w.checks {
w.startMonitoring(w.checks[i])
for _, c := range w.checks {
w.startMonitoring(c)
}
w.monitoring = true
return w.events, nil
}
@@ -170,8 +174,8 @@ func (w *Watchdog) Stop() error {
return ErrNotRunning
}
for i := range w.checks {
w.stopMonitoring(w.checks[i])
for _, c := range w.checks {
w.stopMonitoring(c)
}
return nil
@@ -182,13 +186,14 @@ func (w *Watchdog) Stop() error {
// Otherwise at most concurrency checks will be allowed to run simultaneously.
func (w *Watchdog) RunImmediately(ctx context.Context, concurrency int) ([]CheckResult, error) {
w.mu.Lock()
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)
@@ -209,43 +214,26 @@ func (w *Watchdog) RunImmediately(ctx context.Context, concurrency int) ([]Check
}
func (w *Watchdog) copyChecks() []Check {
cp := make([]Check, len(w.checks))
for i := range w.checks {
cp[i] = w.checks[i].check
cp := make([]Check, 0, len(w.checks))
for _, v := range w.checks {
cp = append(cp, v.check)
}
return cp
}
func (w *Watchdog) startMonitoring(wdc *wdCheck) {
wdc.stop = make(chan struct{})
c := wdc.check
// this method is called only with
// w.mu locked
if !w.monitoring {
w.monitoring = true
}
w.running++
go func() {
defer func() {
w.mu.Lock()
defer w.mu.Unlock()
w.running--
if w.running == 0 {
// last goroutine to exit will also
// close the output chan
close(w.events)
w.monitoring = false
}
}()
state := CheckResult{
// if first run return anything
// other that OK, we'll report it
// if first run is OK, then we do not need to report
Status: StatusOK,
}
var curr error = nil
ticker := time.Tick(wdc.check.Interval)
@@ -255,21 +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
s := CheckResult{
Name: c.Name,
Status: status,
Error: err,
r := CheckResult{
Name: c.Name,
Error: err,
}
if s.Status != state.Status || s.Error != nil {
w.events <- s
if (err != nil && curr == nil) ||
(curr != nil && err == nil) {
// status changed, let's report
w.events <- r
}
state = s
curr = err
select {
case <-ticker:
@@ -284,50 +273,52 @@ func (w *Watchdog) startMonitoring(wdc *wdCheck) {
func (w *Watchdog) stopMonitoring(wdc *wdCheck) {
close(wdc.stop)
w.running--
if w.running == 0 {
w.monitoring = false
close(w.events)
}
}
func runChecksConcurrently(ctx context.Context, ch []Check, concurrency int) []CheckResult {
statuses := make([]CheckResult, 0, len(ch))
m := sync.Mutex{} // for append operations
sema := make(chan struct{}, concurrency) // semaphore to limit concurrency
done := make(chan struct{}, len(ch))
count := len(ch)
done := make(chan CheckResult, len(ch))
wg := new(sync.WaitGroup)
wg.Add(len(ch))
for _, e := range ch {
sema <- struct{}{} // acquire
go func() error {
go func() {
sema <- struct{}{} // acquire
defer func() {
<-sema
done <- struct{}{}
<-sema // release
wg.Done()
}()
// 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,
}
m.Lock()
defer m.Unlock()
statuses = append(statuses, r)
return nil
done <- r
}()
}
// wait for all to finish
for range done {
count--
if count == 0 {
close(done)
}
go func() {
wg.Wait()
close(done)
}()
results := make([]CheckResult, 0, len(ch))
// collect results
for r := range done {
results = append(results, r)
}
return statuses
return results
}