From d0b9bc84aed1898a06a9d6fc3b82beee407d3cde Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Mon, 2 Mar 2026 12:41:08 +0200 Subject: Refactor display iteration/state and harden collector runtime --- internal/app/app.go | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) (limited to 'internal/app') 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" +} -- cgit v1.2.3