@@ -199,11 +199,11 @@ var responseTimes []time.Duration
199
199
func (ch * ConcurrencyHandler ) MonitorResponseTimeVariability (responseTime time.Duration ) int {
200
200
// Append the latest response time
201
201
responseTimes = append (responseTimes , responseTime )
202
- if len (responseTimes ) > 5 { // Use the last 5 measurements
202
+ if len (responseTimes ) > 10 { // Use the last 10 measurements for a smoother average
203
203
responseTimes = responseTimes [1 :]
204
204
}
205
205
206
- // Calculate average response time from the slice
206
+ // Calculate moving average response time from the slice
207
207
var sum time.Duration
208
208
for _ , rt := range responseTimes {
209
209
sum += rt
@@ -218,11 +218,20 @@ func (ch *ConcurrencyHandler) MonitorResponseTimeVariability(responseTime time.D
218
218
variance := varianceSum / float64 (len (responseTimes ))
219
219
stdDev := math .Sqrt (variance )
220
220
221
- // Determine action based on standard deviation against a higher threshold
222
- if stdDev > (ch .Metrics .ResponseTimeVariability .StdDevThreshold * 1.5 ) { // Increased threshold
223
- return - 1 // Suggest decrease concurrency
224
- } else if stdDev <= ch .Metrics .ResponseTimeVariability .StdDevThreshold && len (ch .sem ) < MaxConcurrency {
225
- return 1 // Suggest increase concurrency if there is capacity
221
+ // Action determination with debounce effect
222
+ // Requires keeping track of how often the stdDev threshold has been exceeded
223
+ const debounceCount = 3 // e.g., threshold must be exceeded in 3 consecutive checks to act
224
+ if stdDev > (ch .Metrics .ResponseTimeVariability .StdDevThreshold * 1.5 ) {
225
+ ch .Metrics .ResponseTimeVariability .DebounceScaleDownCount ++
226
+ if ch .Metrics .ResponseTimeVariability .DebounceScaleDownCount >= debounceCount {
227
+ ch .Metrics .ResponseTimeVariability .DebounceScaleDownCount = 0 // reset counter after action
228
+ return - 1 // Suggest decrease concurrency
229
+ }
230
+ } else {
231
+ ch .Metrics .ResponseTimeVariability .DebounceScaleDownCount = 0 // reset counter if condition not met
232
+ if stdDev <= ch .Metrics .ResponseTimeVariability .StdDevThreshold && len (ch .sem ) < MaxConcurrency {
233
+ return 1 // Suggest increase concurrency if there is capacity
234
+ }
226
235
}
227
236
return 0
228
237
}
0 commit comments