-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] Implement client-side rate limiting of API requests (#31)
* [feat] Implement client-side rate limiting of API requests * Update internal/provider/utils/throttled_transport.go Co-authored-by: Albert Chang <albertchang@users.noreply.github.com> --------- Co-authored-by: Albert Chang <albertchang@users.noreply.github.com>
- Loading branch information
1 parent
1447e33
commit 706ad04
Showing
8 changed files
with
114 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
provider "retool" { | ||
requests_per_minute = 100 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
provider "retool" { | ||
requests_per_minute = -1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package utils | ||
|
||
import ( | ||
"net/http" | ||
"time" | ||
|
||
"golang.org/x/time/rate" | ||
) | ||
|
||
// ThrottledTransport Rate Limited HTTP Client | ||
// Copied as-is from https://gist.github.com/zdebra/10f0e284c4672e99f0cb767298f20c11 | ||
type ThrottledTransport struct { | ||
roundTripperWrap http.RoundTripper | ||
ratelimiter *rate.Limiter | ||
} | ||
|
||
// Implements the RoundTripper interface. | ||
func (c *ThrottledTransport) RoundTrip(r *http.Request) (*http.Response, error) { | ||
err := c.ratelimiter.Wait(r.Context()) // This is a blocking call. Honors the rate limit. | ||
if err != nil { | ||
return nil, err | ||
} | ||
return c.roundTripperWrap.RoundTrip(r) | ||
} | ||
|
||
// NewThrottledTransport wraps transportWrap with a rate limitter | ||
// example usage: | ||
// client := http.DefaultClient | ||
// client.Transport = NewThrottledTransport(10*time.Seconds, 60, http.DefaultTransport) allows 60 requests every 10 seconds. | ||
func NewThrottledTransport(limitPeriod time.Duration, requestCount int, transportWrap http.RoundTripper) http.RoundTripper { | ||
return &ThrottledTransport{ | ||
roundTripperWrap: transportWrap, | ||
ratelimiter: rate.NewLimiter(rate.Every(limitPeriod), requestCount), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters