Skip to content

Commit

Permalink
constellix: follow rate limiting headers (#2085)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Jan 23, 2024
1 parent 7fd1704 commit bb83067
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion providers/dns/constellix/constellix.go
Expand Up @@ -6,11 +6,13 @@ import (
"errors"
"fmt"
"net/http"
"strconv"
"time"

"github.com/go-acme/lego/v4/challenge/dns01"
"github.com/go-acme/lego/v4/platform/config/env"
"github.com/go-acme/lego/v4/providers/dns/constellix/internal"
"github.com/hashicorp/go-retryablehttp"
)

// Environment variables names.
Expand Down Expand Up @@ -85,7 +87,12 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
return nil, fmt.Errorf("constellix: %w", err)
}

client := internal.NewClient(tr.Wrap(config.HTTPClient))
retryClient := retryablehttp.NewClient()
retryClient.RetryMax = 5
retryClient.HTTPClient = tr.Wrap(config.HTTPClient)
retryClient.Backoff = backoff

client := internal.NewClient(retryClient.StandardClient())

return &DNSProvider{config: config, client: client}, nil
}
Expand Down Expand Up @@ -273,3 +280,18 @@ func containsValue(record *internal.Record, value string) bool {

return false
}

func backoff(min, max time.Duration, attemptNum int, resp *http.Response) time.Duration {
if resp != nil {
// https://api.dns.constellix.com/v4/docs#section/Using-the-API/Rate-Limiting
if resp.StatusCode == http.StatusTooManyRequests {
if s, ok := resp.Header["X-Ratelimit-Reset"]; ok {
if sleep, err := strconv.ParseInt(s[0], 10, 64); err == nil {
return time.Second * time.Duration(sleep)
}
}
}
}

return retryablehttp.DefaultBackoff(min, max, attemptNum, resp)
}

0 comments on commit bb83067

Please sign in to comment.