added rate limiting with brust

This commit is contained in:
David Janowski 2023-06-17 21:18:01 +02:00
parent 3a964f598f
commit ce9c081ae6
4 changed files with 8 additions and 2 deletions

View File

@ -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),
}
}

View File

@ -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 {

1
go.mod
View File

@ -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
)

2
go.sum
View File

@ -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=