Skip to content

Commit

Permalink
PORKBUN: fix JSON unmarshal and add retry on rate limit (#3019)
Browse files Browse the repository at this point in the history
Co-authored-by: Tom Limoncelli <[email protected]>
  • Loading branch information
imlonghao and tlimoncelli authored Jul 1, 2024
1 parent f3acdc4 commit 2f155ce
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
28 changes: 15 additions & 13 deletions providers/porkbun/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/StackExchange/dnscontrol/v4/pkg/printer"
"io"
"net/http"
"sort"
Expand Down Expand Up @@ -34,33 +35,21 @@ type domainRecord struct {
Content string `json:"content"`
TTL string `json:"ttl"`
Prio string `json:"prio"`
Notes string `json:"notes"`
}

type recordResponse struct {
Status string `json:"status"`
Records []domainRecord `json:"records"`
}

type domainListRecord struct {
Domain string `json:"domain"`
Status string `json:"status"`
TLD string `json:"tld"`
CreateDate string `json:"createDate"`
ExpireDate string `json:"expireDate"`
SecurityLock string `json:"securityLock"`
WhoisPrivacy string `json:"whoisPrivacy"`
AutoRenew string `json:"autoRenew"`
NotLocal string `json:"notLocal"`
Domain string `json:"domain"`
}

type domainListResponse struct {
Status string `json:"status"`
Domains []domainListRecord `json:"domains"`
}

type nsResponse struct {
Status string `json:"status"`
Nameservers []string `json:"ns"`
}

Expand All @@ -76,8 +65,11 @@ func (c *porkbunProvider) post(endpoint string, params requestParams) ([]byte, e
client := &http.Client{}
req, _ := http.NewRequest("POST", baseURL+endpoint, bytes.NewBuffer(personJSON))

retrycnt := 0

// If request sending too fast, the server will fail with the following error:
// porkbun API error: Create error: We were unable to create the DNS record.
retry:
time.Sleep(500 * time.Millisecond)
resp, err := client.Do(req)
if err != nil {
Expand All @@ -86,6 +78,16 @@ func (c *porkbunProvider) post(endpoint string, params requestParams) ([]byte, e

bodyString, _ := io.ReadAll(resp.Body)

if resp.StatusCode == 202 {
retrycnt += 1
if retrycnt == 5 {
return bodyString, fmt.Errorf("rate limiting exceeded")
}
printer.Warnf("Rate limiting.. waiting for %d minute(s)\n", retrycnt)
time.Sleep(time.Minute * time.Duration(retrycnt))
goto retry
}

// Got error from API ?
var errResp errorResponse
err = json.Unmarshal(bodyString, &errResp)
Expand Down
5 changes: 0 additions & 5 deletions providers/porkbun/porkbunProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ func newPorkbun(m map[string]string, _ json.RawMessage) (*porkbunProvider, error
return nil, fmt.Errorf("missing porkbun api_key or secret_key")
}

// Validate authentication
if err := c.ping(); err != nil {
return nil, err
}

return c, nil
}

Expand Down

0 comments on commit 2f155ce

Please sign in to comment.