-
Notifications
You must be signed in to change notification settings - Fork 499
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: PagerDuty Rate Limiting (#7218)
In my tests (toy account with 1000 incidents), this fixed the rate-limit errors that prevented cloudquery from getting data. Fetching '*' took 5m13s unfortunately. I used the `RateLimitHttpClient` instead of just calling `limiter.Wait` before every call, since some calls are paginated by the SDK (i.e. the `for` loop is in the `pagerduty-go` sdk). mention: #6981
- Loading branch information
Showing
6 changed files
with
63 additions
and
1 deletion.
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
34 changes: 34 additions & 0 deletions
34
plugins/source/pagerduty/client/rate_limited_http_client.go
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,34 @@ | ||
package client | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/PagerDuty/go-pagerduty" | ||
"golang.org/x/time/rate" | ||
) | ||
|
||
// Note that this is an implementatioin of 'pagerduty.HTTPClient', not of 'http.Client'. | ||
func newRateLimitedHttpClient(underlyingHttpClient pagerduty.HTTPClient, maxRequestsPerSecond int) *RateLimitedHttpClient { | ||
return &RateLimitedHttpClient{ | ||
underlyingHttpClient: underlyingHttpClient, | ||
limiter: rate.NewLimiter( | ||
/*r=*/ rate.Limit(maxRequestsPerSecond), | ||
/*b=*/ 1, | ||
), | ||
} | ||
} | ||
|
||
// Note that this is an implementatioin of 'pagerduty.HTTPClient', not of 'http.Client'. | ||
type RateLimitedHttpClient struct { | ||
underlyingHttpClient pagerduty.HTTPClient | ||
|
||
limiter *rate.Limiter | ||
} | ||
|
||
func (c *RateLimitedHttpClient) Do(req *http.Request) (*http.Response, error) { | ||
if err := c.limiter.Wait(req.Context()); err != nil { | ||
return nil, err | ||
} | ||
|
||
return c.underlyingHttpClient.Do(req) | ||
} |
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