mirror of
https://github.com/henrygd/beszel.git
synced 2026-09-21 08:57:48 +02:00
feat: add network monitors (ICMP/TCP/HTTP/DNS) (#2266)
Co-authored-by: xiaomiku01 <xiaomiku01@outlook.com> Co-authored-by: henrygd <hank@henrygd.me>
This commit is contained in:
60
agent/network_monitor_schedule.go
Normal file
60
agent/network_monitor_schedule.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (pm *MonitorManager) startMonitor(task *monitorTask) {
|
||||
interval := time.Duration(task.config.Interval) * time.Second
|
||||
if interval < time.Second {
|
||||
interval = 30 * time.Second
|
||||
}
|
||||
delay := getStagger(interval.Milliseconds())
|
||||
slog.Debug("starting monitor task", "target", task.config.Target, "delay", delay, "interval", interval)
|
||||
go runMonitorSchedule(task.ctx, interval, delay, func() {
|
||||
if _, allowed := task.resumeGuard.snapshot(); allowed {
|
||||
task.runProbe(pm.probe)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// runMonitorSchedule owns only timing. Checks run serially, and slow checks
|
||||
// naturally drop missed ticks rather than building an execution backlog.
|
||||
func runMonitorSchedule(ctx context.Context, interval, delay time.Duration, run func()) {
|
||||
timer := time.NewTimer(delay)
|
||||
defer timer.Stop()
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-timer.C:
|
||||
}
|
||||
if ctx.Err() != nil {
|
||||
return
|
||||
}
|
||||
run()
|
||||
ticker := time.NewTicker(interval)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
if ctx.Err() != nil {
|
||||
return
|
||||
}
|
||||
run()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// getStagger returns an initial delay between half an interval and one interval.
|
||||
func getStagger(intervalMilli int64) time.Duration {
|
||||
delay := rand.Intn(int(intervalMilli))
|
||||
if delay < int(intervalMilli)/2 {
|
||||
delay += int(intervalMilli) / 2
|
||||
}
|
||||
return time.Duration(delay) * time.Millisecond
|
||||
}
|
||||
Reference in New Issue
Block a user