diff options
| author | Paul Buetow <paul@buetow.org> | 2026-03-02 12:41:08 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-03-02 12:41:08 +0200 |
| commit | d0b9bc84aed1898a06a9d6fc3b82beee407d3cde (patch) | |
| tree | 7bb343960ee912d77dbc9f7720cd8cdd1f0172ea /internal/app | |
| parent | bbc91e8764bd83c4497f2ddac86bb8947a91765c (diff) | |
Refactor display iteration/state and harden collector runtime
Diffstat (limited to 'internal/app')
| -rw-r--r-- | internal/app/app.go | 41 |
1 files changed, 40 insertions, 1 deletions
diff --git a/internal/app/app.go b/internal/app/app.go index c18dc30..85c348a 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -2,7 +2,11 @@ package app import ( "context" + "fmt" + "os" + "strings" "sync" + "time" "codeberg.org/snonux/loadbars/internal/collector" "codeberg.org/snonux/loadbars/internal/config" @@ -23,7 +27,7 @@ func Run(cfg *config.Config) error { wg.Add(1) go func() { defer wg.Done() - _ = collector.Run(ctx, h, cfg, store) + runCollectorLoop(ctx, h, cfg, store) }() } @@ -32,3 +36,38 @@ func Run(cfg *config.Config) error { wg.Wait() return err } + +func runCollectorLoop(ctx context.Context, host string, cfg *config.Config, store *Store) { + backoff := time.Second + for { + err := collector.Run(ctx, host, cfg, store) + if err == nil || ctx.Err() != nil { + return + } + fmt.Fprintf(os.Stderr, "!!! collector %s failed: %v\n", host, err) + if !isRemoteHost(host) { + return + } + timer := time.NewTimer(backoff) + select { + case <-ctx.Done(): + timer.Stop() + return + case <-timer.C: + } + if backoff < 30*time.Second { + backoff *= 2 + if backoff > 30*time.Second { + backoff = 30 * time.Second + } + } + } +} + +func isRemoteHost(host string) bool { + host = strings.TrimSpace(host) + if i := strings.Index(host, ":"); i >= 0 { + host = strings.TrimSpace(host[:i]) + } + return host != "localhost" && host != "127.0.0.1" +} |
