diff --git a/charger/easee.go b/charger/easee.go index d710a2535..063c67073 100644 --- a/charger/easee.go +++ b/charger/easee.go @@ -208,8 +208,7 @@ func (c *Easee) chargerSite(charger string) (easee.Site, error) { // connect creates an HTTP connection to the signalR hub func (c *Easee) connect(ts oauth2.TokenSource) func() (signalr.Connection, error) { - bo := backoff.NewExponentialBackOff() - bo.MaxInterval = time.Minute + bo := backoff.NewExponentialBackOff(backoff.WithMaxInterval(time.Minute)) return func() (conn signalr.Connection, err error) { defer func() { diff --git a/charger/hardybarth-salia.go b/charger/hardybarth-salia.go index 9a8ecb389..2dc8adbe1 100644 --- a/charger/hardybarth-salia.go +++ b/charger/hardybarth-salia.go @@ -132,9 +132,9 @@ func NewSalia(uri string, cache time.Duration) (api.Charger, error) { } func (wb *Salia) heartbeat() { - bo := backoff.NewExponentialBackOff() - bo.InitialInterval = 5 * time.Second - bo.MaxInterval = time.Minute + bo := backoff.NewExponentialBackOff( + backoff.WithInitialInterval(5*time.Second), + backoff.WithMaxInterval(time.Minute)) for range time.Tick(30 * time.Second) { if err := backoff.Retry(func() error { diff --git a/charger/pulsatrix.go b/charger/pulsatrix.go index 4dae62ec0..87c69a548 100644 --- a/charger/pulsatrix.go +++ b/charger/pulsatrix.go @@ -124,10 +124,10 @@ func (c *Pulsatrix) connectWs() error { // ReconnectWs reconnects to a pulsatrix SECC websocket func (c *Pulsatrix) reconnectWs() { - bo := backoff.NewExponentialBackOff() - bo.InitialInterval = time.Second - bo.MaxInterval = 1 * time.Minute - bo.MaxElapsedTime = 0 * time.Second // retry forever; default is 15 min + bo := backoff.NewExponentialBackOff( + backoff.WithInitialInterval(time.Second), + backoff.WithMaxInterval(time.Minute), + backoff.WithMaxElapsedTime(0)) // retry forever; default is 15 min if err := backoff.Retry(c.connectWs, bo); err != nil { c.log.ERROR.Println(err) } diff --git a/core/helper.go b/core/helper.go index 846287e24..5986c28e8 100644 --- a/core/helper.go +++ b/core/helper.go @@ -18,9 +18,7 @@ var ( // bo returns an exponential backoff for reading meter power quickly func bo() *backoff.ExponentialBackOff { - bo := backoff.NewExponentialBackOff() - bo.MaxElapsedTime = time.Second - return bo + return backoff.NewExponentialBackOff(backoff.WithMaxElapsedTime(time.Second)) } // powerToCurrent is a helper function to convert power to per-phase current diff --git a/core/loadpoint.go b/core/loadpoint.go index be6bb03e0..643f0e38f 100644 --- a/core/loadpoint.go +++ b/core/loadpoint.go @@ -1369,10 +1369,7 @@ func (lp *Loadpoint) pvMaxCurrent(mode api.ChargeMode, sitePower float64, batter // UpdateChargePowerAndCurrents updates charge meter power and currents for load management func (lp *Loadpoint) UpdateChargePowerAndCurrents() { - bo := backoff.NewExponentialBackOff() - bo.MaxElapsedTime = time.Second - - if power, err := backoff.RetryWithData(lp.chargeMeter.CurrentPower, bo); err == nil { + if power, err := backoff.RetryWithData(lp.chargeMeter.CurrentPower, bo()); err == nil { lp.Lock() lp.chargePower = power // update value if no error lp.Unlock() @@ -1409,7 +1406,7 @@ func (lp *Loadpoint) UpdateChargePowerAndCurrents() { lp.publish(keys.ChargeCurrents, lp.chargeCurrents) return nil - }, bo); err != nil { + }, bo()); err != nil { lp.log.ERROR.Printf("charge currents: %v", err) } } diff --git a/meter/dsmr.go b/meter/dsmr.go index 561d7b732..fb730958a 100644 --- a/meter/dsmr.go +++ b/meter/dsmr.go @@ -139,8 +139,7 @@ func NewDsmr(uri, energy string, timeout time.Duration) (api.Meter, error) { // based on https://github.com/basvdlei/gotsmart/blob/master/gotsmart.go func (m *Dsmr) run(conn net.Conn, done chan struct{}) { log := util.NewLogger("dsmr") - bo := backoff.NewExponentialBackOff() - bo.MaxInterval = 5 * time.Minute + bo := backoff.NewExponentialBackOff(backoff.WithMaxInterval(5 * time.Minute)) handle := func(op string, err error) { log.ERROR.Printf("%s: %v", op, err) diff --git a/meter/goodwe/server.go b/meter/goodwe/server.go index 80c0ce8cf..d2f1d1665 100644 --- a/meter/goodwe/server.go +++ b/meter/goodwe/server.go @@ -60,9 +60,9 @@ func (m *Server) GetInverter(ip string) *util.Monitor[Inverter] { } func (m *Server) readData() { - bo := backoff.NewExponentialBackOff() - bo.MaxInterval = time.Second - bo.MaxElapsedTime = 10 * time.Second + bo := backoff.NewExponentialBackOff( + backoff.WithMaxInterval(time.Second), + backoff.WithMaxElapsedTime(10*time.Second)) for { mu.RLock() diff --git a/meter/rct.go b/meter/rct.go index 203078545..5fe094bfe 100644 --- a/meter/rct.go +++ b/meter/rct.go @@ -87,9 +87,9 @@ func NewRCT(uri, usage string, cache time.Duration, capacity func() float64) (ap return nil, err } - bo := backoff.NewExponentialBackOff() - bo.InitialInterval = 10 * time.Millisecond - bo.MaxElapsedTime = time.Second + bo := backoff.NewExponentialBackOff( + backoff.WithInitialInterval(10*time.Millisecond), + backoff.WithMaxElapsedTime(time.Second)) m := &RCT{ usage: strings.ToLower(usage), diff --git a/tariff/amber.go b/tariff/amber.go index b85a83e6f..416e6df73 100644 --- a/tariff/amber.go +++ b/tariff/amber.go @@ -82,7 +82,6 @@ func NewAmberFromConfig(other map[string]interface{}) (api.Tariff, error) { func (t *Amber) run(done chan error) { var once sync.Once - bo := newBackoff() tick := time.NewTicker(time.Minute) for ; true; <-tick.C { @@ -91,7 +90,7 @@ func (t *Amber) run(done chan error) { if err := backoff.Retry(func() error { return backoffPermanentError(t.GetJSON(uri, &res)) - }, bo); err != nil { + }, bo()); err != nil { once.Do(func() { done <- err }) t.log.ERROR.Println(err) diff --git a/tariff/awattar.go b/tariff/awattar.go index eeae54cfc..9acef9d67 100644 --- a/tariff/awattar.go +++ b/tariff/awattar.go @@ -55,7 +55,7 @@ func NewAwattarFromConfig(other map[string]interface{}) (api.Tariff, error) { func (t *Awattar) run(done chan error) { var once sync.Once - bo := newBackoff() + client := request.NewHelper(t.log) tick := time.NewTicker(time.Hour) @@ -64,7 +64,7 @@ func (t *Awattar) run(done chan error) { if err := backoff.Retry(func() error { return backoffPermanentError(client.GetJSON(t.uri, &res)) - }, bo); err != nil { + }, bo()); err != nil { once.Do(func() { done <- err }) t.log.ERROR.Println(err) diff --git a/tariff/edf-tempo.go b/tariff/edf-tempo.go index 88d3d3a2c..c31465d3f 100644 --- a/tariff/edf-tempo.go +++ b/tariff/edf-tempo.go @@ -102,7 +102,6 @@ func (t *EdfTempo) RefreshToken(_ *oauth2.Token) (*oauth2.Token, error) { func (t *EdfTempo) run(done chan error) { var once sync.Once - bo := newBackoff() tick := time.NewTicker(time.Hour) for ; true; <-tick.C { @@ -125,7 +124,7 @@ func (t *EdfTempo) run(done chan error) { if err := backoff.Retry(func() error { return backoffPermanentError(t.GetJSON(uri, &res)) - }, bo); err != nil { + }, bo()); err != nil { once.Do(func() { done <- err }) t.log.ERROR.Println(err) diff --git a/tariff/electricitymaps.go b/tariff/electricitymaps.go index de5d7a860..6483bef41 100644 --- a/tariff/electricitymaps.go +++ b/tariff/electricitymaps.go @@ -79,7 +79,7 @@ func NewElectricityMapsFromConfig(other map[string]interface{}) (api.Tariff, err func (t *ElectricityMaps) run(done chan error) { var once sync.Once - bo := newBackoff() + uri := fmt.Sprintf("%s/carbon-intensity/forecast?zone=%s", t.uri, t.zone) tick := time.NewTicker(time.Hour) @@ -88,7 +88,7 @@ func (t *ElectricityMaps) run(done chan error) { if err := backoff.Retry(func() error { return backoffPermanentError(t.GetJSON(uri, &res)) - }, bo); err != nil { + }, bo()); err != nil { if res.Error != "" { err = errors.New(res.Error) } diff --git a/tariff/elering.go b/tariff/elering.go index 0c0768b9c..e49e15407 100644 --- a/tariff/elering.go +++ b/tariff/elering.go @@ -60,7 +60,6 @@ func NewEleringFromConfig(other map[string]interface{}) (api.Tariff, error) { func (t *Elering) run(done chan error) { var once sync.Once client := request.NewHelper(t.log) - bo := newBackoff() tick := time.NewTicker(time.Hour) for ; true; <-tick.C { @@ -73,7 +72,7 @@ func (t *Elering) run(done chan error) { if err := backoff.Retry(func() error { return backoffPermanentError(client.GetJSON(uri, &res)) - }, bo); err != nil { + }, bo()); err != nil { once.Do(func() { done <- err }) t.log.ERROR.Println(err) diff --git a/tariff/energinet.go b/tariff/energinet.go index 516fe3272..a5170f1da 100644 --- a/tariff/energinet.go +++ b/tariff/energinet.go @@ -59,7 +59,6 @@ func NewEnerginetFromConfig(other map[string]interface{}) (api.Tariff, error) { func (t *Energinet) run(done chan error) { var once sync.Once client := request.NewHelper(t.log) - bo := newBackoff() tick := time.NewTicker(time.Hour) for ; true; <-tick.C { @@ -73,7 +72,7 @@ func (t *Energinet) run(done chan error) { if err := backoff.Retry(func() error { return backoffPermanentError(client.GetJSON(uri, &res)) - }, bo); err != nil { + }, bo()); err != nil { once.Do(func() { done <- err }) t.log.ERROR.Println(err) diff --git a/tariff/entsoe.go b/tariff/entsoe.go index 7b08687ea..714117a3b 100644 --- a/tariff/entsoe.go +++ b/tariff/entsoe.go @@ -85,8 +85,6 @@ func NewEntsoeFromConfig(other map[string]interface{}) (api.Tariff, error) { func (t *Entsoe) run(done chan error) { var once sync.Once - bo := newBackoff() - // Data updated by ESO every half hour, but we only need data every hour to stay current. tick := time.NewTicker(time.Hour) for ; true; <-tick.C { @@ -127,7 +125,7 @@ func (t *Entsoe) run(done chan error) { default: return backoff.Permanent(errors.New("invalid document name: " + doc.XMLName.Local)) } - }, bo); err != nil { + }, bo()); err != nil { once.Do(func() { done <- err }) t.log.ERROR.Println(err) diff --git a/tariff/groupe-e.go b/tariff/groupe-e.go index d29222b8f..c708e3801 100644 --- a/tariff/groupe-e.go +++ b/tariff/groupe-e.go @@ -39,7 +39,7 @@ func NewGroupeEFromConfig(other map[string]interface{}) (api.Tariff, error) { func (t *GroupeE) run(done chan error) { var once sync.Once - bo := newBackoff() + client := request.NewHelper(t.log) tick := time.NewTicker(time.Hour) @@ -55,7 +55,7 @@ func (t *GroupeE) run(done chan error) { if err := backoff.Retry(func() error { return backoffPermanentError(client.GetJSON(uri, &res)) - }, bo); err != nil { + }, bo()); err != nil { once.Do(func() { done <- err }) t.log.ERROR.Println(err) diff --git a/tariff/gruenstromindex.go b/tariff/gruenstromindex.go index 13c1de193..286c6343c 100644 --- a/tariff/gruenstromindex.go +++ b/tariff/gruenstromindex.go @@ -89,7 +89,7 @@ func NewGrünStromIndexFromConfig(other map[string]interface{}) (api.Tariff, err func (t *GrünStromIndex) run(done chan error) { var once sync.Once client := request.NewHelper(t.log) - bo := newBackoff() + uri := fmt.Sprintf("https://api.corrently.io/v2.0/gsi/prediction?zip=%s", t.zip) tick := time.NewTicker(time.Hour) diff --git a/tariff/helper.go b/tariff/helper.go index 4b7332b18..1da0ee419 100644 --- a/tariff/helper.go +++ b/tariff/helper.go @@ -11,11 +11,11 @@ import ( "github.com/evcc-io/evcc/util/request" ) -func newBackoff() backoff.BackOff { - bo := backoff.NewExponentialBackOff() - bo.InitialInterval = time.Second - bo.MaxElapsedTime = time.Minute - return bo +func bo() backoff.BackOff { + return backoff.NewExponentialBackOff( + backoff.WithInitialInterval(time.Second), + backoff.WithMaxElapsedTime(time.Minute), + ) } // backoffPermanentError returns a permanent error in case of HTTP 400 diff --git a/tariff/ngeso.go b/tariff/ngeso.go index 93b751b14..c1718ffd6 100644 --- a/tariff/ngeso.go +++ b/tariff/ngeso.go @@ -57,7 +57,6 @@ func NewNgesoFromConfig(other map[string]interface{}) (api.Tariff, error) { func (t *Ngeso) run(done chan error) { var once sync.Once client := request.NewHelper(t.log) - bo := newBackoff() // Use national results by default. var tReq ngeso.CarbonForecastRequest diff --git a/tariff/octopus.go b/tariff/octopus.go index 2620c5e89..0525ea666 100644 --- a/tariff/octopus.go +++ b/tariff/octopus.go @@ -84,7 +84,6 @@ func NewOctopusFromConfig(other map[string]interface{}) (api.Tariff, error) { func (t *Octopus) run(done chan error) { var once sync.Once client := request.NewHelper(t.log) - bo := newBackoff() var restQueryUri string @@ -115,7 +114,7 @@ func (t *Octopus) run(done chan error) { if err := backoff.Retry(func() error { return backoffPermanentError(client.GetJSON(restQueryUri, &res)) - }, bo); err != nil { + }, bo()); err != nil { once.Do(func() { done <- err }) t.log.ERROR.Println(err) diff --git a/tariff/pun.go b/tariff/pun.go index 8d73ba66e..930fe0802 100644 --- a/tariff/pun.go +++ b/tariff/pun.go @@ -70,7 +70,6 @@ func NewPunFromConfig(other map[string]interface{}) (api.Tariff, error) { func (t *Pun) run(done chan error) { var once sync.Once - bo := newBackoff() tick := time.NewTicker(time.Hour) for ; true; <-tick.C { @@ -81,7 +80,7 @@ func (t *Pun) run(done chan error) { today, err = t.getData(time.Now()) return err - }, bo); err != nil { + }, bo()); err != nil { once.Do(func() { done <- err }) t.log.ERROR.Println(err) diff --git a/tariff/smartenergy.go b/tariff/smartenergy.go index 033b0e296..17b0116f7 100644 --- a/tariff/smartenergy.go +++ b/tariff/smartenergy.go @@ -49,7 +49,6 @@ func NewSmartEnergyFromConfig(other map[string]interface{}) (api.Tariff, error) func (t *SmartEnergy) run(done chan error) { var once sync.Once client := request.NewHelper(t.log) - bo := newBackoff() tick := time.NewTicker(time.Hour) for ; true; <-tick.C { @@ -57,7 +56,7 @@ func (t *SmartEnergy) run(done chan error) { if err := backoff.Retry(func() error { return backoffPermanentError(client.GetJSON(smartenergy.URI, &res)) - }, bo); err != nil { + }, bo()); err != nil { once.Do(func() { done <- err }) t.log.ERROR.Println(err) diff --git a/tariff/tariff.go b/tariff/tariff.go index 8986614ab..1a1d77c18 100644 --- a/tariff/tariff.go +++ b/tariff/tariff.go @@ -80,7 +80,6 @@ func NewConfigurableFromConfig(other map[string]interface{}) (api.Tariff, error) func (t *Tariff) run(forecastG func() (string, error), done chan error) { var once sync.Once - bo := newBackoff() tick := time.NewTicker(time.Hour) for ; true; <-tick.C { @@ -97,7 +96,7 @@ func (t *Tariff) run(forecastG func() (string, error), done chan error) { data[i].Price = t.totalPrice(r.Price) } return nil - }, bo); err != nil { + }, bo()); err != nil { once.Do(func() { done <- err }) t.log.ERROR.Println(err) diff --git a/tariff/tibber.go b/tariff/tibber.go index 9f9348d40..e339cad47 100644 --- a/tariff/tibber.go +++ b/tariff/tibber.go @@ -72,7 +72,6 @@ func NewTibberFromConfig(other map[string]interface{}) (api.Tariff, error) { func (t *Tibber) run(done chan error) { var once sync.Once - bo := newBackoff() v := map[string]interface{}{ "id": graphql.ID(t.homeID), @@ -94,7 +93,7 @@ func (t *Tibber) run(done chan error) { ctx, cancel := context.WithTimeout(context.Background(), request.Timeout) defer cancel() return t.client.Query(ctx, &res, v) - }, bo); err != nil { + }, bo()); err != nil { once.Do(func() { done <- err }) t.log.ERROR.Println(err)