@@ -6,15 +6,38 @@ import (
6
6
"net/http"
7
7
"strconv"
8
8
"time"
9
+
10
+ "go.uber.org/zap"
9
11
)
10
12
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.
12
29
func (ch * ConcurrencyHandler ) EvaluateAndAdjustConcurrency (resp * http.Response , responseTime time.Duration ) {
13
30
// Call monitoring functions
14
31
rateLimitFeedback := ch .MonitorRateLimitHeaders (resp )
15
32
responseCodeFeedback := ch .MonitorServerResponseCodes (resp )
16
33
responseTimeFeedback := ch .MonitorResponseTimeVariability (responseTime )
17
34
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
+
18
41
// Determine overall action based on feedback
19
42
suggestions := []int {rateLimitFeedback , responseCodeFeedback , responseTimeFeedback }
20
43
scaleDownCount := 0
@@ -29,11 +52,20 @@ func (ch *ConcurrencyHandler) EvaluateAndAdjustConcurrency(resp *http.Response,
29
52
}
30
53
}
31
54
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
+
32
60
// Decide on scaling action
33
61
if scaleDownCount > scaleUpCount {
62
+ ch .logger .Info ("Scaling down the concurrency" , zap .String ("Reason" , "More signals suggested to decrease concurrency" ))
34
63
ch .ScaleDown ()
35
64
} else if scaleUpCount > scaleDownCount {
65
+ ch .logger .Info ("Scaling up the concurrency" , zap .String ("Reason" , "More signals suggested to increase concurrency" ))
36
66
ch .ScaleUp ()
67
+ } else {
68
+ ch .logger .Info ("No change in concurrency" , zap .String ("Reason" , "Equal signals for both scaling up and down" ))
37
69
}
38
70
}
39
71
0 commit comments