Skip to content

Commit

Permalink
impr: use ratelimiter standart libarary instead custom channels
Browse files Browse the repository at this point in the history
  • Loading branch information
mehanizm committed Dec 23, 2024
1 parent ba207f5 commit e0fd5aa
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
38 changes: 27 additions & 11 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import (
"io"
"net/http"
"net/url"
"time"

"golang.org/x/time/rate"
)

const (
Expand All @@ -25,7 +26,7 @@ const (
// Client client for airtable api.
type Client struct {
client *http.Client
rateLimiter <-chan time.Time
rateLimiter *rate.Limiter
baseURL string
apiKey string
}
Expand All @@ -36,7 +37,7 @@ type Client struct {
func NewClient(apiKey string) *Client {
return &Client{
client: http.DefaultClient,
rateLimiter: time.Tick(time.Second / time.Duration(rateLimit)),
rateLimiter: rate.NewLimiter(rate.Limit(rateLimit), 1),
apiKey: apiKey,
baseURL: airtableBaseURL,
}
Expand All @@ -51,7 +52,7 @@ func (at *Client) SetCustomClient(client *http.Client) {
// Airtable limit is 5 requests per second (we use 4)
// https://airtable.com/{yourDatabaseID}/api/docs#curl/ratelimits
func (at *Client) SetRateLimit(customRateLimit int) {
at.rateLimiter = time.Tick(time.Second / time.Duration(customRateLimit))
at.rateLimiter = rate.NewLimiter(rate.Limit(customRateLimit), 1)
}

func (at *Client) SetBaseURL(baseURL string) error {
Expand All @@ -73,12 +74,15 @@ func (at *Client) SetBaseURL(baseURL string) error {
return nil
}

func (at *Client) rateLimit() {
<-at.rateLimiter
func (at *Client) rateLimit(ctx context.Context) error {
return at.rateLimiter.Wait(ctx)
}

func (at *Client) get(ctx context.Context, db, table, recordID string, params url.Values, target any) error {
at.rateLimit()
err := at.rateLimit(ctx)
if err != nil {
return err
}

url := fmt.Sprintf("%s/%s/%s", at.baseURL, db, table)
if recordID != "" {
Expand All @@ -104,7 +108,10 @@ func (at *Client) get(ctx context.Context, db, table, recordID string, params ur
}

func (at *Client) post(ctx context.Context, db, table string, data, response any) error {
at.rateLimit()
err := at.rateLimit(ctx)
if err != nil {
return err
}

url := fmt.Sprintf("%s/%s/%s", at.baseURL, db, table)

Expand All @@ -125,7 +132,10 @@ func (at *Client) post(ctx context.Context, db, table string, data, response any
}

func (at *Client) delete(ctx context.Context, db, table string, recordIDs []string, target any) error {
at.rateLimit()
err := at.rateLimit(ctx)
if err != nil {
return err
}

rawURL := fmt.Sprintf("%s/%s/%s", at.baseURL, db, table)
params := url.Values{}
Expand Down Expand Up @@ -153,7 +163,10 @@ func (at *Client) delete(ctx context.Context, db, table string, recordIDs []stri
}

func (at *Client) patch(ctx context.Context, db, table, data, response any) error {
at.rateLimit()
err := at.rateLimit(ctx)
if err != nil {
return err
}

url := fmt.Sprintf("%s/%s/%s", at.baseURL, db, table)

Expand All @@ -174,7 +187,10 @@ func (at *Client) patch(ctx context.Context, db, table, data, response any) erro
}

func (at *Client) put(ctx context.Context, db, table, data, response any) error {
at.rateLimit()
err := at.rateLimit(ctx)
if err != nil {
return err
}

url := fmt.Sprintf("%s/%s/%s", at.baseURL, db, table)

Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/mehanizm/airtable

go 1.23

require golang.org/x/time v0.8.0
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
golang.org/x/time v0.8.0 h1:9i3RxcPv3PZnitoVGMPDKZSq1xW1gK1Xy3ArNOGZfEg=
golang.org/x/time v0.8.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=

0 comments on commit e0fd5aa

Please sign in to comment.