Skip to content

Commit bdb2091

Browse files
authored
Merge pull request #174 from deploymenttheory/dev
Adjust concurrency logic and metrics in the concurrency package
2 parents 5827a44 + dd5157c commit bdb2091

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

concurrency/metrics.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -145,33 +145,28 @@ func (ch *ConcurrencyHandler) MonitorRateLimitHeaders(resp *http.Response) int {
145145
func (ch *ConcurrencyHandler) MonitorServerResponseCodes(resp *http.Response) int {
146146
statusCode := resp.StatusCode
147147

148-
// Lock the metrics to ensure thread safety
149148
ch.Metrics.Lock.Lock()
150149
defer ch.Metrics.Lock.Unlock()
151150

152-
// Update the appropriate error count based on the response status code
153-
switch {
154-
case statusCode >= 500 && statusCode < 600:
151+
// Reset error rates on successful response
152+
if statusCode >= 200 && statusCode < 300 {
153+
ch.Metrics.TotalRateLimitErrors = 0
154+
ch.Metrics.TotalRetries = 0
155+
} else if statusCode >= 500 && statusCode < 600 {
155156
ch.Metrics.TotalRateLimitErrors++
156-
case statusCode >= 400 && statusCode < 500:
157-
// Assuming 4xx errors as client errors
157+
} else if statusCode >= 400 && statusCode < 500 {
158158
ch.Metrics.TotalRetries++
159159
}
160160

161-
// Calculate error rate
162161
totalRequests := float64(ch.Metrics.TotalRequests)
163162
totalErrors := float64(ch.Metrics.TotalRateLimitErrors + ch.Metrics.TotalRetries)
164163
errorRate := totalErrors / totalRequests
165164

166-
// Set the new error rate in the metrics
167165
ch.Metrics.ResponseCodeMetrics.ErrorRate = errorRate
168166

169-
// Determine action based on the error rate
170167
if errorRate > ErrorRateThreshold {
171-
// Suggest decrease concurrency
172168
return -1
173169
} else if errorRate <= ErrorRateThreshold && len(ch.sem) < MaxConcurrency {
174-
// Suggest increase concurrency if there is capacity
175170
return 1
176171
}
177172
return 0

0 commit comments

Comments
 (0)