Skip to content

Commit 0c066b7

Browse files
committed
Remove unused code and update rate handling in httpclient package
1 parent 1a5564a commit 0c066b7

File tree

6 files changed

+20
-17
lines changed

6 files changed

+20
-17
lines changed

httpclient/httpclient_helpers.go renamed to helpers/helpers.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// http_helpers.go
2-
package httpclient
1+
// helpers/helpers_test.go
2+
package helpers
33

44
import (
55
"time"

httpclient/httpclient_helpers_test.go renamed to helpers/helpers_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// http_helpers.go
2-
package httpclient
1+
// helpers/helpers.go
2+
package helpers
33

44
import (
55
"testing"

httpclient/httpclient_ping.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"os"
99
"time"
1010

11+
"github.com/deploymenttheory/go-api-http-client/ratehandler"
12+
1113
"go.uber.org/zap"
1214
"golang.org/x/net/icmp"
1315
"golang.org/x/net/ipv4"
@@ -74,7 +76,7 @@ func (c *Client) DoPing(method, endpoint string, body, out interface{}) (*http.R
7476
log.Warn("Ping failed, retrying...", zap.String("method", method), zap.String("endpoint", endpoint), zap.Int("retryCount", retryCount))
7577

7678
// Calculate backoff duration and wait before retrying
77-
backoffDuration := calculateBackoff(retryCount)
79+
backoffDuration := ratehandler.CalculateBackoff(retryCount)
7880
time.Sleep(backoffDuration)
7981
}
8082

httpclient/httpclient_request.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/deploymenttheory/go-api-http-client/authenticationhandler"
1111
"github.com/deploymenttheory/go-api-http-client/headers"
1212
"github.com/deploymenttheory/go-api-http-client/logger"
13+
"github.com/deploymenttheory/go-api-http-client/ratehandler"
1314
"github.com/deploymenttheory/go-api-http-client/status"
1415
"github.com/google/uuid"
1516
"go.uber.org/zap"
@@ -207,7 +208,7 @@ func (c *Client) executeRequestWithRetries(method, endpoint string, body, out in
207208

208209
// Parsing rate limit headers if a rate-limit error is detected
209210
if status.IsRateLimitError(resp) {
210-
waitDuration := parseRateLimitHeaders(resp, log)
211+
waitDuration := ratehandler.ParseRateLimitHeaders(resp, log)
211212
if waitDuration > 0 {
212213
log.Warn("Rate limit encountered, waiting before retrying", zap.Duration("waitDuration", waitDuration))
213214
time.Sleep(waitDuration)
@@ -222,7 +223,7 @@ func (c *Client) executeRequestWithRetries(method, endpoint string, body, out in
222223
log.Warn("Max retry attempts reached", zap.String("method", method), zap.String("endpoint", endpoint))
223224
break // Stop retrying if max attempts are reached
224225
}
225-
waitDuration := calculateBackoff(retryCount)
226+
waitDuration := ratehandler.CalculateBackoff(retryCount)
226227
log.Warn("Retrying request due to transient error", zap.String("method", method), zap.String("endpoint", endpoint), zap.Int("retryCount", retryCount), zap.Duration("waitDuration", waitDuration), zap.Error(err))
227228
time.Sleep(waitDuration) // Wait before retrying
228229
continue // Continue to next iteration after waiting

httpclient/httpclient_rate_handler_test.go renamed to ratehandler/httpclient_rate_handler_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// http_rate_handler_test.go
2-
package httpclient
1+
// ratehandler/ratehandler.go
2+
package ratehandler
33

44
import (
55
"net/http"
@@ -26,7 +26,7 @@ func TestCalculateBackoff(t *testing.T) {
2626

2727
for _, tt := range tests {
2828
t.Run("RetryCount"+strconv.Itoa(tt.retry), func(t *testing.T) {
29-
delay := calculateBackoff(tt.retry)
29+
delay := CalculateBackoff(tt.retry)
3030

3131
// The delay should be within the expected range
3232
assert.GreaterOrEqual(t, delay, tt.expectedMin, "Delay should be greater than or equal to expected minimum after jitter adjustment")
@@ -86,7 +86,7 @@ func TestParseRateLimitHeaders(t *testing.T) {
8686
}
8787

8888
mockLog := mocklogger.NewMockLogger()
89-
wait := parseRateLimitHeaders(resp, mockLog)
89+
wait := ParseRateLimitHeaders(resp, mockLog)
9090

9191
// Adjust the delta based on the expected wait duration
9292
delta := time.Duration(1) * time.Second

httpclient/httpclient_rate_handler.go renamed to ratehandler/rate_handler.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// http_rate_handler.go
1+
// ratehandler/ratehandler.go
22

33
/*
44
Components:
@@ -16,7 +16,7 @@ behavior accordingly.
1616
1717
*/
1818

19-
package httpclient
19+
package ratehandler
2020

2121
import (
2222
"math"
@@ -36,11 +36,11 @@ const (
3636
jitterFactor = 0.5 // Random jitter factor
3737
)
3838

39-
// calculateBackoff calculates the next delay for retry with exponential backoff and jitter.
39+
// CalculateBackoff calculates the next delay for retry with exponential backoff and jitter.
4040
// The baseDelay is the initial delay duration, which is exponentially increased on each retry.
4141
// The jitterFactor adds randomness to the delay to avoid simultaneous retries (thundering herd problem).
4242
// The delay is capped at maxDelay to prevent excessive wait times.
43-
func calculateBackoff(retry int) time.Duration {
43+
func CalculateBackoff(retry int) time.Duration {
4444
if retry < 0 {
4545
retry = 0 // Ensure non-negative retry count
4646
}
@@ -55,9 +55,9 @@ func calculateBackoff(retry int) time.Duration {
5555
return time.Duration(delayWithJitter)
5656
}
5757

58-
// parseRateLimitHeaders parses common rate limit headers and adjusts behavior accordingly.
58+
// ParseRateLimitHeaders parses common rate limit headers and adjusts behavior accordingly.
5959
// It handles both Retry-After (in seconds or HTTP-date format) and X-RateLimit-Reset headers.
60-
func parseRateLimitHeaders(resp *http.Response, log logger.Logger) time.Duration {
60+
func ParseRateLimitHeaders(resp *http.Response, log logger.Logger) time.Duration {
6161
// Check for the Retry-After header in seconds
6262
if retryAfter := resp.Header.Get("Retry-After"); retryAfter != "" {
6363
if waitSeconds, err := strconv.Atoi(retryAfter); err == nil {

0 commit comments

Comments
 (0)