Skip to content

Commit cce6e69

Browse files
committed
Refactor concurrency package to include new metrics and adjust concurrency based on feedback
1 parent 4daab07 commit cce6e69

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

concurrency/metrics.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,38 @@ import (
66
"net/http"
77
"strconv"
88
"time"
9+
10+
"go.uber.org/zap"
911
)
1012

11-
// EvaluateAndAdjustConcurrency evaluates the response from the server and adjusts the concurrency level accordingly.
13+
// EvaluateAndAdjustConcurrency evaluates the HTTP response from a server along with the request's response time
14+
// and adjusts the concurrency level of the system accordingly. It utilizes three monitoring functions:
15+
// MonitorRateLimitHeaders, MonitorServerResponseCodes, and MonitorResponseTimeVariability, each of which
16+
// provides feedback on different aspects of the response and system's current state. The function aggregates
17+
// feedback from these monitoring functions to make a decision on whether to scale up or scale down the concurrency.
18+
// The decision is based on a simple majority of suggestions: if more functions suggest scaling down (return -1),
19+
// it scales down; if more suggest scaling up (return 1), it scales up. This method centralizes concurrency control
20+
// decision-making, providing a systematic approach to managing request handling capacity based on real-time
21+
// operational metrics.
22+
//
23+
// Parameters:
24+
//
25+
// resp - The HTTP response received from the server.
26+
// responseTime - The time duration between sending the request and receiving the response.
27+
//
28+
// It logs the specific reason for scaling decisions, helping in traceability and fine-tuning system performance.
1229
func (ch *ConcurrencyHandler) EvaluateAndAdjustConcurrency(resp *http.Response, responseTime time.Duration) {
1330
// Call monitoring functions
1431
rateLimitFeedback := ch.MonitorRateLimitHeaders(resp)
1532
responseCodeFeedback := ch.MonitorServerResponseCodes(resp)
1633
responseTimeFeedback := ch.MonitorResponseTimeVariability(responseTime)
1734

35+
// Log the feedback from each monitoring function for debugging
36+
ch.logger.Debug("Concurrency Adjustment Feedback",
37+
zap.Int("RateLimitFeedback", rateLimitFeedback),
38+
zap.Int("ResponseCodeFeedback", responseCodeFeedback),
39+
zap.Int("ResponseTimeFeedback", responseTimeFeedback))
40+
1841
// Determine overall action based on feedback
1942
suggestions := []int{rateLimitFeedback, responseCodeFeedback, responseTimeFeedback}
2043
scaleDownCount := 0
@@ -29,11 +52,20 @@ func (ch *ConcurrencyHandler) EvaluateAndAdjustConcurrency(resp *http.Response,
2952
}
3053
}
3154

55+
// Log the counts for scale down and up suggestions
56+
ch.logger.Info("Scaling Decision Counts",
57+
zap.Int("ScaleDownCount", scaleDownCount),
58+
zap.Int("ScaleUpCount", scaleUpCount))
59+
3260
// Decide on scaling action
3361
if scaleDownCount > scaleUpCount {
62+
ch.logger.Info("Scaling down the concurrency", zap.String("Reason", "More signals suggested to decrease concurrency"))
3463
ch.ScaleDown()
3564
} else if scaleUpCount > scaleDownCount {
65+
ch.logger.Info("Scaling up the concurrency", zap.String("Reason", "More signals suggested to increase concurrency"))
3666
ch.ScaleUp()
67+
} else {
68+
ch.logger.Info("No change in concurrency", zap.String("Reason", "Equal signals for both scaling up and down"))
3769
}
3870
}
3971

0 commit comments

Comments
 (0)