Skip to content

Commit c9cd26f

Browse files
committed
Tidy up and flattening
1 parent 3b428e1 commit c9cd26f

File tree

8 files changed

+26
-331
lines changed

8 files changed

+26
-331
lines changed

httpclient/cookies.go

+2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import (
55
"net/url"
66
)
77

8+
// loadCustomCookies applies the custom cookies supplied in the config and applies them to the http session.
89
func (c *Client) loadCustomCookies() error {
910
cookieJar, err := cookiejar.New(nil)
1011
if err != nil {
1112
return err
1213
}
14+
1315
c.http.Jar = cookieJar
1416

1517
cookieUrl, err := url.Parse((*c.Integration).GetFQDN())

httpclient/headers.go

-14
This file was deleted.

httpclient/request.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,18 @@ func (c *Client) DoRequest(method, endpoint string, body, out interface{}) (*htt
113113
// - The retry mechanism employs exponential backoff with jitter to mitigate the impact of retries on the server.
114114
// endregion
115115
func (c *Client) executeRequestWithRetries(method, endpoint string, body interface{}) (*http.Response, error) {
116-
ctx := context.Background()
117-
totalRetryDeadline := time.Now().Add(c.config.TotalRetryDuration)
118-
119116
var resp *http.Response
120117
var err error
121118
var retryCount int
122119

120+
ctx := context.Background()
121+
123122
c.Sugar.Debug("Executing request with retries", zap.String("method", method), zap.String("endpoint", endpoint))
124123

125124
// TODO removed the blocked comments
125+
// Simplify this?
126126
// Timer
127+
totalRetryDeadline := time.Now().Add(c.config.TotalRetryDuration)
127128
for time.Now().Before(totalRetryDeadline) {
128129

129130
// Resp
@@ -133,21 +134,22 @@ func (c *Client) executeRequestWithRetries(method, endpoint string, body interfa
133134
}
134135

135136
// Success
136-
if resp.StatusCode >= 200 && resp.StatusCode < 400 {
137-
if resp.StatusCode >= 300 {
137+
if resp.StatusCode >= http.StatusOK && resp.StatusCode < http.StatusBadRequest {
138+
if resp.StatusCode == http.StatusPermanentRedirect || resp.StatusCode == http.StatusTemporaryRedirect {
138139
c.Sugar.Warn("Redirect response received", zap.Int("status_code", resp.StatusCode), zap.String("location", resp.Header.Get("Location")))
139140
}
140141
c.Sugar.Infof("%s request successful at %v", resp.Request.Method, resp.Request.URL)
141142
return resp, nil
142143
}
143144

145+
// Message
144146
statusMessage := http.StatusText(resp.StatusCode)
145147
if statusMessage == "" {
146148
statusMessage = "unknown response code"
147149
}
148150

149151
// Non Retry
150-
if IsNonRetryableStatusCode(resp) {
152+
if IsNonRetryableStatusCode(resp.StatusCode) {
151153
c.Sugar.Warn("Non-retryable error received", zap.Int("status_code", resp.StatusCode), zap.String("status_message", statusMessage))
152154
return resp, response.HandleAPIErrorResponse(resp, c.Sugar)
153155
}

httpclient/status.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package httpclient
33
import "net/http"
44

55
// IsNonRetryableStatusCode checks if the provided response indicates a non-retryable error.
6-
func IsNonRetryableStatusCode(resp *http.Response) bool {
6+
func IsNonRetryableStatusCode(statusCode int) bool {
77
nonRetryableStatusCodes := map[int]bool{
88
http.StatusBadRequest: true, // 400 - Bad Request
99
http.StatusUnauthorized: true, // 401 - Unauthorized
@@ -31,7 +31,7 @@ func IsNonRetryableStatusCode(resp *http.Response) bool {
3131
http.StatusUnavailableForLegalReasons: true, // 451 - Unavailable For Legal Reasons
3232
}
3333

34-
_, isNonRetryable := nonRetryableStatusCodes[resp.StatusCode]
34+
_, isNonRetryable := nonRetryableStatusCodes[statusCode]
3535
return isNonRetryable
3636
}
3737

httpclient/utility.go

+9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package httpclient
44
import (
55
"errors"
66
"fmt"
7+
"net/http"
78
"os"
89
"path/filepath"
910
"regexp"
@@ -152,3 +153,11 @@ func setDefaultDuration(field *time.Duration, defaultValue time.Duration) {
152153
*field = defaultValue
153154
}
154155
}
156+
157+
// CheckDeprecationHeader checks the response headers for the Deprecation header and logs a warning if present.
158+
func (c *Client) CheckDeprecationHeader(resp *http.Response) {
159+
deprecationHeader := resp.Header.Get("Deprecation")
160+
if deprecationHeader != "" {
161+
c.Sugar.Warn("API endpoint is deprecated", deprecationHeader, resp.Request.URL.String())
162+
}
163+
}

ratehandler/ratehandler.go renamed to ratehandler/main.go

+5-11
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,8 @@ const (
4040
// The jitterFactor adds randomness to the delay to avoid simultaneous retries (thundering herd problem).
4141
// The delay is capped at maxDelay to prevent excessive wait times.
4242
func CalculateBackoff(retry int) time.Duration {
43-
if retry < 0 {
44-
retry = 0 // Ensure non-negative retry count
45-
}
46-
4743
delay := float64(baseDelay) * math.Pow(2, float64(retry))
44+
4845
jitter := (rand.Float64() - 0.5) * jitterFactor * 2.0 // Random value between -jitterFactor and +jitterFactor
4946
delayWithJitter := delay * (1.0 + jitter)
5047

@@ -57,32 +54,29 @@ func CalculateBackoff(retry int) time.Duration {
5754
// ParseRateLimitHeaders parses common rate limit headers and adjusts behavior accordingly.
5855
// It handles both Retry-After (in seconds or HTTP-date format) and X-RateLimit-Reset headers.
5956
func ParseRateLimitHeaders(resp *http.Response, logger *zap.SugaredLogger) time.Duration {
60-
// Check for the Retry-After header in seconds
6157
if retryAfter := resp.Header.Get("Retry-After"); retryAfter != "" {
6258
if waitSeconds, err := strconv.Atoi(retryAfter); err == nil {
6359
return time.Duration(waitSeconds) * time.Second
60+
6461
} else if retryAfterDate, err := time.Parse(time.RFC1123, retryAfter); err == nil {
65-
// Handle HTTP-date format in Retry-After
6662
return time.Until(retryAfterDate)
63+
6764
} else {
6865
logger.Debug("Unable to parse Retry-After header", zap.String("value", retryAfter), zap.Error(err))
6966
}
7067
}
7168

72-
// Check for X-RateLimit-Remaining; if it's 0, use X-RateLimit-Reset to determine how long to wait
7369
if remaining := resp.Header.Get("X-RateLimit-Remaining"); remaining == "0" {
7470
if resetTimeStr := resp.Header.Get("X-RateLimit-Reset"); resetTimeStr != "" {
7571
if resetTimeEpoch, err := strconv.ParseInt(resetTimeStr, 10, 64); err == nil {
7672
resetTime := time.Unix(resetTimeEpoch, 0)
77-
// Add a buffer to account for potential clock skew
78-
const skewBuffer = 5 * time.Second
79-
return time.Until(resetTime) + skewBuffer
73+
return time.Until(resetTime) + (5 * time.Second)
74+
8075
} else {
8176
logger.Debug("Unable to parse X-RateLimit-Reset header", zap.String("value", resetTimeStr), zap.Error(err))
8277
}
8378
}
8479
}
8580

86-
// No relevant rate limiting headers found, return 0
8781
return 0
8882
}

ratehandler/ratehandler_test.go.bk

-101
This file was deleted.

0 commit comments

Comments
 (0)