feat: added limitting to the request rate.
This commit is contained in:
parent
f970498504
commit
4edffe94f3
4 changed files with 54 additions and 1 deletions
|
@ -26,3 +26,4 @@ func NewClient(secretPath string) (*Client, error) {
|
|||
}, nil
|
||||
}
|
||||
|
||||
|
||||
|
|
48
api/api.go
48
api/api.go
|
@ -11,6 +11,7 @@ const (
|
|||
DefaultContentType = "application/json"
|
||||
DefaultAccept = DefaultContentType
|
||||
DefaultCacheControl = "no-cache"
|
||||
MaxEntitiesPerRequest = 250
|
||||
)
|
||||
|
||||
type ClientOptions struct {
|
||||
|
@ -34,6 +35,14 @@ type Client struct {
|
|||
BaseURL *url.URL
|
||||
secretStoreFilePath string
|
||||
Debug bool
|
||||
|
||||
availableRequests int
|
||||
mrps int
|
||||
|
||||
ticker *time.Ticker
|
||||
req chan struct{}
|
||||
|
||||
requestsMade int64
|
||||
}
|
||||
|
||||
type OAuthTokenResponse struct {
|
||||
|
@ -112,3 +121,42 @@ func NewAPI(secretPath string) (*Client, error) {
|
|||
return client, nil
|
||||
}
|
||||
|
||||
// Set maximum requests per second.
|
||||
func (client *Client) SetMRPS(rps int) *Client {
|
||||
client.mrps = rps
|
||||
client.req = make(chan struct{})
|
||||
client.ticker = time.NewTicker(time.Second)
|
||||
go func() {
|
||||
for {
|
||||
for _ = range make(
|
||||
[]struct{},
|
||||
client.mrps - client.availableRequests,
|
||||
) {
|
||||
client.req <- struct{}{}
|
||||
}
|
||||
client.availableRequests = client.mrps
|
||||
<-client.ticker.C
|
||||
}
|
||||
}()
|
||||
return client
|
||||
}
|
||||
|
||||
func (client *Client) waitInQueue() bool {
|
||||
// Just leaving if MRPS is not set.
|
||||
if client.mrps == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
<- client.req
|
||||
client.availableRequests--
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (client *Client) finishRequest() {
|
||||
}
|
||||
|
||||
func (client *Client) RequestsMade() int64 {
|
||||
return client.requestsMade
|
||||
}
|
||||
|
||||
|
|
|
@ -22,6 +22,7 @@ func (client *Client) doRawRequest(
|
|||
var (
|
||||
err error
|
||||
)
|
||||
client.requestsMade++
|
||||
|
||||
reqBody := new(bytes.Buffer)
|
||||
if params.Body != nil {
|
||||
|
@ -113,6 +114,9 @@ func (client *Client) doRequest(
|
|||
err error
|
||||
parsedURL *url.URL
|
||||
)
|
||||
if client.waitInQueue() {
|
||||
defer client.finishRequest()
|
||||
}
|
||||
|
||||
parsedURL, err = url.Parse(u)
|
||||
if err != nil {
|
||||
|
|
|
@ -7,7 +7,6 @@ import "encoding/json"
|
|||
import "strconv"
|
||||
import "log"
|
||||
import "fmt"
|
||||
//import "os"
|
||||
|
||||
var getLead = mtool.T("get-leads").Func(func(flags *mtool.Flags){
|
||||
var (
|
||||
|
@ -36,6 +35,7 @@ var getLead = mtool.T("get-leads").Func(func(flags *mtool.Flags){
|
|||
if err != nil {
|
||||
log.Fatalf("NewAmoClient(...): %s\n", err)
|
||||
}
|
||||
c.API.SetMRPS(8)
|
||||
|
||||
leads, err := c.GetLeads(
|
||||
urlenc.Array[int]{
|
||||
|
|
Loading…
Reference in a new issue