From ce9c081ae640ebf9ba11652ddd1a2d1c40648ca6 Mon Sep 17 00:00:00 2001 From: David Janowski Date: Sat, 17 Jun 2023 21:18:01 +0200 Subject: [PATCH] added rate limiting with brust --- e621/client.go | 3 +++ e621/request.go | 4 ++-- go.mod | 1 + go.sum | 2 ++ 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/e621/client.go b/e621/client.go index 31bed82..1558eed 100644 --- a/e621/client.go +++ b/e621/client.go @@ -1,6 +1,7 @@ package e621 import ( + "golang.org/x/time/rate" "net/http" ) @@ -13,6 +14,7 @@ type Client struct { apiKey string username string client *http.Client + limiter *rate.Limiter } // NewClient creates a new e621 API client. @@ -21,5 +23,6 @@ func NewClient(apiKey string, username string) *Client { apiKey: apiKey, username: username, client: &http.Client{}, + limiter: rate.NewLimiter(1, 2), } } diff --git a/e621/request.go b/e621/request.go index 2ee1668..c43205b 100644 --- a/e621/request.go +++ b/e621/request.go @@ -1,16 +1,16 @@ package e621 import ( + "context" "encoding/json" "fmt" "io" "net/http" - "time" ) func ExecuteGetAPIRequest[dataType any](c *Client, URIPath string) (*dataType, error) { - time.Sleep(1 * time.Second) var err error + c.limiter.Wait(context.Background()) url := fmt.Sprintf("%s/%s", baseURL, URIPath) req, err := http.NewRequest("GET", url, nil) if err != nil { diff --git a/go.mod b/go.mod index 5ce0f67..3f4653e 100644 --- a/go.mod +++ b/go.mod @@ -10,5 +10,6 @@ require ( require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/stretchr/testify v1.5.1 // indirect + golang.org/x/time v0.3.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect ) diff --git a/go.sum b/go.sum index cb4c7a3..64d991e 100644 --- a/go.sum +++ b/go.sum @@ -11,6 +11,8 @@ github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4= +golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=