1
1
package aapi
2
2
3
3
import (
4
- "context"
5
4
"encoding/json"
6
5
"fmt"
7
6
"io"
8
7
"io/ioutil"
9
- "log"
10
- "math/rand"
11
8
"net/http"
12
9
"net/url"
13
10
"sort"
14
- "strconv"
15
11
"strings"
16
- "time"
17
12
18
13
"github.com/google/go-querystring/query"
19
14
"github.com/hashicorp/go-retryablehttp"
20
- "golang.org/x/time/rate"
21
15
)
22
16
23
17
const (
@@ -37,13 +31,11 @@ type PaginatedResponse struct {
37
31
38
32
type Client struct {
39
33
// HTTP client used to communicate with the API.
40
- client * retryablehttp.Client
41
- token string
42
- baseURL * url.URL
43
- grafanaURL * url.URL
44
- disableRetries bool
45
- limiter * rate.Limiter
46
- UserAgent string
34
+ client * retryablehttp.Client
35
+ token string
36
+ baseURL * url.URL
37
+ grafanaURL * url.URL
38
+ UserAgent string
47
39
// List of Services. Keep in sync with func newClient
48
40
Alerts * AlertService
49
41
Integrations * IntegrationService
@@ -87,18 +79,8 @@ func New(base_url, token string) (*Client, error) {
87
79
func newClient (url , grafana_url string ) (* Client , error ) {
88
80
c := & Client {}
89
81
90
- // Configure the HTTP client.
91
- c .client = & retryablehttp.Client {
92
- Backoff : c .retryHTTPBackoff ,
93
- CheckRetry : c .retryHTTPCheck ,
94
- RetryWaitMin : 100 * time .Millisecond ,
95
- RetryWaitMax : 400 * time .Millisecond ,
96
- RetryMax : 5 ,
97
- }
98
- // https://grafana.com/docs/grafana-cloud/oncall/oncall-api-reference/#rate-limits
99
- baseLimit := 50.0 / 60
100
- limit := rate .Limit (baseLimit )
101
- c .limiter = rate .NewLimiter (limit , 50 )
82
+ // retryablehttp.Client will retry up to 4 times on recoverable errors (429, 5xx, and low-level network errors)
83
+ c .client = retryablehttp .NewClient ()
102
84
103
85
// Set the default base URL. _ suppress error handling
104
86
err := c .setBaseURL (url )
@@ -214,12 +196,6 @@ func (c *Client) NewRequest(method, path string, opt interface{}) (*retryablehtt
214
196
// JSON decoded and stored in the value pointed to by v, or returned as an
215
197
// error if an API error has occurred.
216
198
func (c * Client ) Do (req * retryablehttp.Request , v interface {}) (* http.Response , error ) {
217
- err := c .limiter .Wait (req .Context ())
218
- if err != nil {
219
- log .Println ("limiter" )
220
- return nil , err
221
- }
222
-
223
199
resp , err := c .client .Do (req )
224
200
if err != nil {
225
201
return nil , err
@@ -307,43 +283,6 @@ func (e *ErrorResponse) Error() string {
307
283
return fmt .Sprintf ("%s %s: %d %s" , e .Response .Request .Method , u , e .Response .StatusCode , e .Message )
308
284
}
309
285
310
- func (c * Client ) retryHTTPCheck (ctx context.Context , resp * http.Response , err error ) (bool , error ) {
311
- if ctx .Err () != nil {
312
- return false , ctx .Err ()
313
- }
314
- if err != nil {
315
- return false , err
316
- }
317
- if ! c .disableRetries && (resp .StatusCode == 429 || resp .StatusCode >= 500 ) {
318
- return true , nil
319
- }
320
- return false , nil
321
- }
322
-
323
- func (c * Client ) retryHTTPBackoff (min , max time.Duration , attemptNum int , resp * http.Response ) time.Duration {
324
- if resp != nil && resp .StatusCode == 429 {
325
- return rateLimitBackoff (min , max , attemptNum , resp )
326
- }
327
-
328
- return retryablehttp .LinearJitterBackoff (min , max , attemptNum , resp )
329
- }
330
-
331
- func rateLimitBackoff (min , max time.Duration , attemptNum int , resp * http.Response ) time.Duration {
332
- rnd := rand .New (rand .NewSource (time .Now ().UnixNano ()))
333
- jitter := time .Duration (rnd .Float64 () * float64 (max - min ))
334
- log .Printf ("[DEBUG] ratelimited call" )
335
- if resp != nil {
336
- if v := resp .Header .Get ("RateLimit-Reset" ); v != "" {
337
- if reset , _ := strconv .ParseInt (v , 10 , 64 ); reset > 0 {
338
- log .Printf ("[DEBUG] reset in '%d" , reset )
339
- min = time .Duration (reset ) * time .Second
340
- }
341
- }
342
- }
343
-
344
- return min + jitter
345
- }
346
-
347
286
func (c * Client ) BaseURL () * url.URL {
348
287
u := * c .baseURL
349
288
return & u
0 commit comments