From 93e59d0c3fbf67caa317690e6f198291eaa1371a Mon Sep 17 00:00:00 2001 From: premultiply <4681172+premultiply@users.noreply.github.com> Date: Sun, 5 Oct 2025 08:22:05 +0200 Subject: [PATCH] Dump: add configurable timeout (#24101) --- cmd/dumper.go | 12 +++++------- cmd/flags.go | 3 +++ cmd/meter.go | 4 +++- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/cmd/dumper.go b/cmd/dumper.go index 5f1545f06..e3275f3b8 100644 --- a/cmd/dumper.go +++ b/cmd/dumper.go @@ -15,7 +15,8 @@ import ( ) type dumper struct { - len int + len int + timeout time.Duration } func (d *dumper) Header(name, underline string) { @@ -36,8 +37,8 @@ func (d *dumper) DumpWithHeader(name string, device interface{}) { } // bo returns an exponential backoff for reading meter power quickly -func bo() *backoff.ExponentialBackOff { - return backoff.NewExponentialBackOff(backoff.WithInitialInterval(20*time.Millisecond), backoff.WithMaxElapsedTime(time.Second)) +func (d *dumper) bo() *backoff.ExponentialBackOff { + return backoff.NewExponentialBackOff(backoff.WithInitialInterval(20*time.Millisecond), backoff.WithMaxElapsedTime(d.timeout)) } func (d *dumper) Dump(name string, v interface{}) { @@ -53,11 +54,8 @@ func (d *dumper) Dump(name string, v interface{}) { if v, ok := v.(api.Meter); ok { power, err := backoff.RetryWithData(func() (float64, error) { f, err := v.CurrentPower() - if err != nil { - fmt.Println(err) - } return f, err - }, bo()) + }, d.bo()) if err != nil { fmt.Fprintf(w, "Power:\t%v\n", err) diff --git a/cmd/flags.go b/cmd/flags.go index c4a2736fc..350832caf 100644 --- a/cmd/flags.go +++ b/cmd/flags.go @@ -68,6 +68,9 @@ const ( flagHeartbeat = "heartbeat" flagHeartbeatDescription = "After command, continue running device heartbeats (if any) until interrupted" + flagTimeout = "timeout" + flagTimeoutDescription = "Timeout" + flagDigits = "digits" flagDelay = "delay" flagForce = "force" diff --git a/cmd/meter.go b/cmd/meter.go index 73beb434c..6d7a133f5 100644 --- a/cmd/meter.go +++ b/cmd/meter.go @@ -24,6 +24,7 @@ func init() { meterCmd.Flags().BoolP(flagRepeat, "r", false, flagRepeatDescription) meterCmd.Flags().Duration(flagRepeatInterval, 0, flagRepeatIntervalDescription) meterCmd.Flags().Bool(flagHeartbeat, false, flagHeartbeatDescription) + meterCmd.Flags().Duration(flagTimeout, time.Second, flagTimeoutDescription) } func runMeter(cmd *cobra.Command, args []string) { @@ -71,7 +72,8 @@ func runMeter(cmd *cobra.Command, args []string) { } if !flagUsed { - d := dumper{len: len(meters)} + timeout, _ := cmd.Flags().GetDuration(flagTimeout) + d := dumper{len: len(meters), timeout: timeout} flag := cmd.Flag(flagDiagnose).Changed REPEAT: