Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RateLimiter #505

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions rest/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ func (c *Client) CallURL(ctx context.Context, method, uri string, response any,
req.SetHeaderMultiValues(options.Headers)
req.SetResult(response).SetError(&models.ErrorResponse{})

if options.Limiter != nil {
options.Limiter.Take()
}

res, err := req.Execute(method, uri)
if err != nil {
return fmt.Errorf("failed to execute request: %w", err)
Expand Down
15 changes: 15 additions & 0 deletions rest/models/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package models
import (
"net/http"
"net/url"
"time"
)

// RequestOptions are used to configure client calls.
Expand All @@ -18,6 +19,9 @@ type RequestOptions struct {

// Trace enables request tracing
Trace bool

// Limiter used to rate limit requests
Limiter RateLimiter
}

// RequestOption changes the configuration of RequestOptions.
Expand Down Expand Up @@ -85,3 +89,14 @@ func WithTrace(trace bool) RequestOption {
o.Trace = trace
}
}

type RateLimiter interface {
Take() time.Time
}

// WithRateLimiter sets Ratelimiter to limit request rate.
func WithRateLimiter(limiter RateLimiter) RequestOption {
return func(o *RequestOptions) {
o.Limiter = limiter
}
}