|
5 | 5 | "math"
|
6 | 6 | "net/http"
|
7 | 7 | "strconv"
|
| 8 | + "sync" |
8 | 9 | "time"
|
9 | 10 |
|
10 | 11 | "go.uber.org/zap"
|
@@ -194,39 +195,47 @@ func (ch *ConcurrencyHandler) MonitorServerResponseCodes(resp *http.Response) in
|
194 | 195 |
|
195 | 196 | // A slice to hold the last n response times for averaging
|
196 | 197 | var responseTimes []time.Duration
|
| 198 | +var responseTimesLock sync.Mutex |
197 | 199 |
|
| 200 | +// MonitorResponseTimeVariability monitors the response time variability and suggests a concurrency adjustment. |
198 | 201 | // MonitorResponseTimeVariability monitors the response time variability and suggests a concurrency adjustment.
|
199 | 202 | func (ch *ConcurrencyHandler) MonitorResponseTimeVariability(responseTime time.Duration) int {
|
200 |
| - ch.Metrics.Lock.Lock() // Ensure thread safety when accessing shared metrics |
201 |
| - defer ch.Metrics.Lock.Unlock() |
| 203 | + ch.Metrics.ResponseTimeVariability.Lock.Lock() |
| 204 | + defer ch.Metrics.ResponseTimeVariability.Lock.Unlock() |
202 | 205 |
|
203 |
| - // Append the latest response time |
| 206 | + responseTimesLock.Lock() // Ensure safe concurrent access |
204 | 207 | responseTimes = append(responseTimes, responseTime)
|
205 |
| - if len(responseTimes) > 10 { // Use the last 10 measurements for a smoother average |
206 |
| - responseTimes = responseTimes[1:] |
| 208 | + if len(responseTimes) > 10 { |
| 209 | + responseTimes = responseTimes[1:] // Maintain last 10 measurements |
207 | 210 | }
|
| 211 | + responseTimesLock.Unlock() |
208 | 212 |
|
209 | 213 | stdDev := calculateStdDev(responseTimes)
|
| 214 | + averageResponseTime := calculateAverage(responseTimes) |
210 | 215 |
|
211 |
| - // Action determination with debounce effect |
212 |
| - // Debounce mechanism for scaling down |
213 |
| - const debounceCount = 3 // Threshold must be exceeded in 3 consecutive checks to act |
214 |
| - if stdDev > (ch.Metrics.ResponseTimeVariability.StdDevThreshold * 2) { |
| 216 | + // Multi-factor check before scaling down |
| 217 | + if stdDev > ch.Metrics.ResponseTimeVariability.StdDevThreshold && averageResponseTime > AcceptableAverageResponseTime { |
215 | 218 | ch.Metrics.ResponseTimeVariability.DebounceScaleDownCount++
|
216 |
| - ch.logger.Info("Increased debounce counter", zap.Int("counter", ch.Metrics.ResponseTimeVariability.DebounceScaleDownCount)) |
217 |
| - if ch.Metrics.ResponseTimeVariability.DebounceScaleDownCount >= debounceCount { |
218 |
| - ch.Metrics.ResponseTimeVariability.DebounceScaleDownCount = 0 // reset counter after action |
219 |
| - ch.logger.Info("Concurrent requests scaling down due to high response time variability") |
| 219 | + if ch.Metrics.ResponseTimeVariability.DebounceScaleDownCount >= debounceScaleDownThreshold { |
| 220 | + ch.Metrics.ResponseTimeVariability.DebounceScaleDownCount = 0 |
220 | 221 | return -1 // Suggest decrease concurrency
|
221 | 222 | }
|
222 | 223 | } else {
|
223 |
| - ch.Metrics.ResponseTimeVariability.DebounceScaleDownCount = 0 // reset counter if condition not met |
224 |
| - if stdDev <= ch.Metrics.ResponseTimeVariability.StdDevThreshold && len(ch.sem) < cap(ch.sem) { |
225 |
| - ch.logger.Info("Concurrent requests scaling up as conditions are favorable") |
226 |
| - return 1 // Suggest increase concurrency if there is capacity |
| 224 | + ch.Metrics.ResponseTimeVariability.DebounceScaleDownCount = 0 // Reset counter if conditions are not met |
| 225 | + if stdDev <= ch.Metrics.ResponseTimeVariability.StdDevThreshold { |
| 226 | + return 1 // Suggest increase concurrency if conditions are favorable |
227 | 227 | }
|
228 | 228 | }
|
229 |
| - return 0 |
| 229 | + return 0 // Default to no change |
| 230 | +} |
| 231 | + |
| 232 | +// calculateAverage computes the average response time from a slice of response times. |
| 233 | +func calculateAverage(times []time.Duration) time.Duration { |
| 234 | + var total time.Duration |
| 235 | + for _, t := range times { |
| 236 | + total += t |
| 237 | + } |
| 238 | + return total / time.Duration(len(times)) |
230 | 239 | }
|
231 | 240 |
|
232 | 241 | // calculateStdDev computes the standard deviation of response times.
|
|
0 commit comments