Skip to content

Adjust concurrency logic and metrics in the concurrency package #174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions concurrency/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,33 +145,28 @@ func (ch *ConcurrencyHandler) MonitorRateLimitHeaders(resp *http.Response) int {
func (ch *ConcurrencyHandler) MonitorServerResponseCodes(resp *http.Response) int {
statusCode := resp.StatusCode

// Lock the metrics to ensure thread safety
ch.Metrics.Lock.Lock()
defer ch.Metrics.Lock.Unlock()

// Update the appropriate error count based on the response status code
switch {
case statusCode >= 500 && statusCode < 600:
// Reset error rates on successful response
if statusCode >= 200 && statusCode < 300 {
ch.Metrics.TotalRateLimitErrors = 0
ch.Metrics.TotalRetries = 0
} else if statusCode >= 500 && statusCode < 600 {
ch.Metrics.TotalRateLimitErrors++
case statusCode >= 400 && statusCode < 500:
// Assuming 4xx errors as client errors
} else if statusCode >= 400 && statusCode < 500 {
ch.Metrics.TotalRetries++
}

// Calculate error rate
totalRequests := float64(ch.Metrics.TotalRequests)
totalErrors := float64(ch.Metrics.TotalRateLimitErrors + ch.Metrics.TotalRetries)
errorRate := totalErrors / totalRequests

// Set the new error rate in the metrics
ch.Metrics.ResponseCodeMetrics.ErrorRate = errorRate

// Determine action based on the error rate
if errorRate > ErrorRateThreshold {
// Suggest decrease concurrency
return -1
} else if errorRate <= ErrorRateThreshold && len(ch.sem) < MaxConcurrency {
// Suggest increase concurrency if there is capacity
return 1
}
return 0
Expand Down