Skip to content

Commit

Permalink
feat: compute rate based on burst.
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed May 31, 2021
1 parent 44b0fcf commit e11ffe9
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions providers/dns/hurricane/internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const (
codeNotFqdn = "notfqdn"
)

const defaultBurst = 5

// Client the Hurricane Electric client.
type Client struct {
HTTPClient *http.Client
Expand Down Expand Up @@ -65,10 +67,7 @@ func (c *Client) UpdateTxtRecord(ctx context.Context, domain string, txt string)
data.Set("hostname", hostname)
data.Set("txt", txt)

// https://github.com/go-acme/lego/issues/1415
// 10 reqs / 2 minutes = freq 1/12 (burst = 1)
// 6 reqs / 2 minutes = freq 1/20 (burt = 5)
rl, _ := c.rateLimiters.LoadOrStore(hostname, rate.NewLimiter(rate.Every(20*time.Second), 5))
rl, _ := c.rateLimiters.LoadOrStore(hostname, rate.NewLimiter(limit(defaultBurst), defaultBurst))

err := rl.(*rate.Limiter).Wait(ctx)
if err != nil {
Expand Down Expand Up @@ -122,3 +121,14 @@ func evaluateBody(body string, hostname string) error {
return fmt.Errorf("attempt to change TXT record %s returned %s", hostname, body)
}
}

// limit computes the rate based on burst.
// The API rate limit per-record is 10 reqs / 2 minutes.
//
// 10 reqs / 2 minutes = freq 1/12 (burst = 1)
// 6 reqs / 2 minutes = freq 1/20 (burst = 5)
//
// https://github.com/go-acme/lego/issues/1415
func limit(burst int) rate.Limit {
return rate.Limit(1 / (120 / (10 - burst + 1)))
}

0 comments on commit e11ffe9

Please sign in to comment.