Dump: add configurable timeout (#24101)

This commit is contained in:
premultiply 2025-10-05 08:22:05 +02:00 • committed by GitHub
parent f8ceb65f0e
commit 93e59d0c3f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 11 additions and 8 deletions

View file

@ -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)

View file

@ -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"

View file

@ -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: