Skip to content

Commit

Permalink
lib: use permanent error instead of context cancellation (#1429)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez authored Jun 11, 2021
1 parent 9b44e4a commit 1dbebce
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 21 deletions.
8 changes: 2 additions & 6 deletions acme/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package api

import (
"bytes"
"context"
"crypto"
"encoding/json"
"errors"
Expand Down Expand Up @@ -83,8 +82,6 @@ func (a *Core) retrievablePost(uri string, content []byte, response interface{})
bo.MaxInterval = 5 * time.Second
bo.MaxElapsedTime = 20 * time.Second

ctx, cancel := context.WithCancel(context.Background())

var resp *http.Response
operation := func() error {
var err error
Expand All @@ -96,8 +93,7 @@ func (a *Core) retrievablePost(uri string, content []byte, response interface{})
return err
}

cancel()
return err
return backoff.Permanent(err)
}

return nil
Expand All @@ -107,7 +103,7 @@ func (a *Core) retrievablePost(uri string, content []byte, response interface{})
log.Infof("retry due to: %v", err)
}

err := backoff.RetryNotify(operation, backoff.WithContext(bo, ctx), notify)
err := backoff.RetryNotify(operation, bo, notify)
if err != nil {
return resp, err
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/akamai/AkamaiOPEN-edgegrid-golang v1.1.0
github.com/aliyun/alibaba-cloud-sdk-go v1.61.976
github.com/aws/aws-sdk-go v1.37.27
github.com/cenkalti/backoff/v4 v4.1.0
github.com/cenkalti/backoff/v4 v4.1.1
github.com/cloudflare/cloudflare-go v0.14.0
github.com/cpu/goacmedns v0.1.1
github.com/dnsimple/dnsimple-go v0.63.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJm
github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc h1:biVzkmvwrH8WK8raXaxBx6fRVTlJILwEwQGL1I/ByEI=
github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
github.com/c-bata/go-prompt v0.2.5/go.mod h1:vFnjEGDIIA/Lib7giyE4E9c50Lvl8j0S+7FVlAwDAVw=
github.com/cenkalti/backoff/v4 v4.1.0 h1:c8LkOFQTzuO0WBM/ae5HdGQuZPfPxp7lqBRwQRm4fSc=
github.com/cenkalti/backoff/v4 v4.1.0/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
github.com/cenkalti/backoff/v4 v4.1.1 h1:G2HAfAmvm/GcKan2oOQpBXOd2tT2G57ZnZGWa1PxPBQ=
github.com/cenkalti/backoff/v4 v4.1.1/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
github.com/census-instrumentation/opencensus-proto v0.2.0/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
Expand Down
8 changes: 2 additions & 6 deletions providers/dns/dynu/internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package internal

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -125,8 +124,6 @@ func (c Client) GetRootDomain(hostname string) (*DNSHostname, error) {
func (c Client) doRetry(method, uri string, body []byte, data interface{}) error {
var resp *http.Response

ctx, cancel := context.WithCancel(context.Background())

operation := func() error {
var reqBody io.Reader
if len(body) > 0 {
Expand All @@ -147,8 +144,7 @@ func (c Client) doRetry(method, uri string, body []byte, data interface{}) error
}

if err != nil {
cancel()
return fmt.Errorf("client error: %w", err)
return backoff.Permanent(fmt.Errorf("client error: %w", err))
}

return nil
Expand All @@ -161,7 +157,7 @@ func (c Client) doRetry(method, uri string, body []byte, data interface{}) error
bo := backoff.NewExponentialBackOff()
bo.InitialInterval = 1 * time.Second

err := backoff.RetryNotify(operation, backoff.WithContext(bo, ctx), notify)
err := backoff.RetryNotify(operation, bo, notify)
if err != nil {
return err
}
Expand Down
8 changes: 2 additions & 6 deletions providers/dns/hostingde/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package hostingde

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -57,15 +56,12 @@ func (d *DNSProvider) updateZone(updateRequest ZoneUpdateRequest) (*ZoneUpdateRe
}

func (d *DNSProvider) getZone(findRequest ZoneConfigsFindRequest) (*ZoneConfig, error) {
ctx, cancel := context.WithCancel(context.Background())

var zoneConfig *ZoneConfig

operation := func() error {
findResponse, err := d.listZoneConfigs(findRequest)
if err != nil {
cancel()
return err
return backoff.Permanent(err)
}

if findResponse.Response.Data[0].Status != "active" {
Expand All @@ -83,7 +79,7 @@ func (d *DNSProvider) getZone(findRequest ZoneConfigsFindRequest) (*ZoneConfig,
bo.MaxElapsedTime = 100 * bo.InitialInterval

// retry in case the zone was edited recently and is not yet active
err := backoff.Retry(operation, backoff.WithContext(bo, ctx))
err := backoff.Retry(operation, bo)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 1dbebce

Please sign in to comment.