@@ -145,33 +145,28 @@ func (ch *ConcurrencyHandler) MonitorRateLimitHeaders(resp *http.Response) int {
145
145
func (ch * ConcurrencyHandler ) MonitorServerResponseCodes (resp * http.Response ) int {
146
146
statusCode := resp .StatusCode
147
147
148
- // Lock the metrics to ensure thread safety
149
148
ch .Metrics .Lock .Lock ()
150
149
defer ch .Metrics .Lock .Unlock ()
151
150
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 {
155
156
ch .Metrics .TotalRateLimitErrors ++
156
- case statusCode >= 400 && statusCode < 500 :
157
- // Assuming 4xx errors as client errors
157
+ } else if statusCode >= 400 && statusCode < 500 {
158
158
ch .Metrics .TotalRetries ++
159
159
}
160
160
161
- // Calculate error rate
162
161
totalRequests := float64 (ch .Metrics .TotalRequests )
163
162
totalErrors := float64 (ch .Metrics .TotalRateLimitErrors + ch .Metrics .TotalRetries )
164
163
errorRate := totalErrors / totalRequests
165
164
166
- // Set the new error rate in the metrics
167
165
ch .Metrics .ResponseCodeMetrics .ErrorRate = errorRate
168
166
169
- // Determine action based on the error rate
170
167
if errorRate > ErrorRateThreshold {
171
- // Suggest decrease concurrency
172
168
return - 1
173
169
} else if errorRate <= ErrorRateThreshold && len (ch .sem ) < MaxConcurrency {
174
- // Suggest increase concurrency if there is capacity
175
170
return 1
176
171
}
177
172
return 0
0 commit comments