|
| 1 | +package concurrency |
| 2 | + |
| 3 | +import ( |
| 4 | + "math" |
| 5 | + "net/http" |
| 6 | + "time" |
| 7 | + |
| 8 | + "go.uber.org/zap" |
| 9 | +) |
| 10 | + |
| 11 | +// MonitorRateLimitHeaders monitors the rate limit headers (X-RateLimit-Remaining and Retry-After) |
| 12 | +// in the HTTP response and adjusts concurrency accordingly. |
| 13 | +func (ch *ConcurrencyHandler) MonitorRateLimitHeaders(resp *http.Response) { |
| 14 | + // Extract X-RateLimit-Remaining and Retry-After headers from the response |
| 15 | + remaining := resp.Header.Get("X-RateLimit-Remaining") |
| 16 | + retryAfter := resp.Header.Get("Retry-After") |
| 17 | + |
| 18 | + // Adjust concurrency based on the values of these headers |
| 19 | + // Implement your logic here to dynamically adjust concurrency |
| 20 | +} |
| 21 | + |
| 22 | +// MonitorServerResponseCodes monitors server response codes and adjusts concurrency accordingly. |
| 23 | +func (ch *ConcurrencyHandler) MonitorServerResponseCodes(resp *http.Response) { |
| 24 | + statusCode := resp.StatusCode |
| 25 | + // Check for 5xx errors (server errors) and 4xx errors (client errors) |
| 26 | + // Implement your logic here to track increases in error rates and adjust concurrency |
| 27 | +} |
| 28 | + |
| 29 | +// MonitorResponseTimeVariability calculates the standard deviation of response times |
| 30 | +// and uses moving averages to smooth out fluctuations, adjusting concurrency accordingly. |
| 31 | +func (ch *ConcurrencyHandler) MonitorResponseTimeVariability(responseTime time.Duration) { |
| 32 | + ch.Metrics.Lock.Lock() |
| 33 | + defer ch.Metrics.Lock.Unlock() |
| 34 | + |
| 35 | + // Update TotalResponseTime and ResponseCount for moving average calculation |
| 36 | + ch.Metrics.TotalResponseTime += responseTime |
| 37 | + ch.Metrics.ResponseCount++ |
| 38 | + |
| 39 | + // Calculate average response time |
| 40 | + averageResponseTime := ch.Metrics.TotalResponseTime / time.Duration(ch.Metrics.ResponseCount) |
| 41 | + |
| 42 | + // Calculate standard deviation of response times |
| 43 | + variance := ch.calculateVariance(averageResponseTime, responseTime) |
| 44 | + stdDev := math.Sqrt(variance) |
| 45 | + |
| 46 | + // Adjust concurrency based on response time variability |
| 47 | + if float64(stdDev) > MaxAcceptableResponseTimeVariability.Seconds() && len(ch.sem) > MinConcurrency { |
| 48 | + newSize := len(ch.sem) - 1 |
| 49 | + ch.logger.Info("Reducing concurrency due to high response time variability", zap.Int("NewSize", newSize)) |
| 50 | + ch.ResizeSemaphore(newSize) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// calculateVariance calculates the variance of response times. |
| 55 | +func (ch *ConcurrencyHandler) calculateVariance(averageResponseTime time.Duration, responseTime time.Duration) float64 { |
| 56 | + // Convert time.Duration values to seconds |
| 57 | + averageSeconds := averageResponseTime.Seconds() |
| 58 | + responseSeconds := responseTime.Seconds() |
| 59 | + |
| 60 | + // Calculate variance |
| 61 | + variance := (float64(ch.Metrics.ResponseCount-1)*math.Pow(averageSeconds-responseSeconds, 2) + ch.Metrics.Variance) / float64(ch.Metrics.ResponseCount) |
| 62 | + ch.Metrics.Variance = variance |
| 63 | + return variance |
| 64 | +} |
| 65 | + |
| 66 | +// MonitorNetworkLatency measures Time to First Byte (TTFB) and monitors network throughput, |
| 67 | +// adjusting concurrency based on changes in network latency and throughput. |
| 68 | +func (ch *ConcurrencyHandler) MonitorNetworkLatency(ttfb time.Duration, throughput float64) { |
| 69 | + ch.Metrics.Lock.Lock() |
| 70 | + defer ch.Metrics.Lock.Unlock() |
| 71 | + |
| 72 | + // Calculate the TTFB moving average |
| 73 | + ch.Metrics.TTFB.Lock.Lock() |
| 74 | + defer ch.Metrics.TTFB.Lock.Unlock() |
| 75 | + ch.Metrics.TTFB.Total += ttfb |
| 76 | + ch.Metrics.TTFB.Count++ |
| 77 | + ttfbMovingAverage := ch.Metrics.TTFB.Total / time.Duration(ch.Metrics.TTFB.Count) |
| 78 | + |
| 79 | + // Calculate the throughput moving average |
| 80 | + ch.Metrics.Throughput.Lock.Lock() |
| 81 | + defer ch.Metrics.Throughput.Lock.Unlock() |
| 82 | + ch.Metrics.Throughput.Total += throughput |
| 83 | + ch.Metrics.Throughput.Count++ |
| 84 | + throughputMovingAverage := ch.Metrics.Throughput.Total / float64(ch.Metrics.Throughput.Count) |
| 85 | + |
| 86 | + // Adjust concurrency based on TTFB and throughput moving averages |
| 87 | + if ttfbMovingAverage > MaxAcceptableTTFB && len(ch.sem) > MinConcurrency { |
| 88 | + newSize := len(ch.sem) - 1 |
| 89 | + ch.logger.Info("Reducing concurrency due to high TTFB", zap.Int("NewSize", newSize)) |
| 90 | + ch.ResizeSemaphore(newSize) |
| 91 | + } else if throughputMovingAverage > MaxAcceptableThroughput && len(ch.sem) < MaxConcurrency { |
| 92 | + newSize := len(ch.sem) + 1 |
| 93 | + ch.logger.Info("Increasing concurrency due to high throughput", zap.Int("NewSize", newSize)) |
| 94 | + ch.ResizeSemaphore(newSize) |
| 95 | + } |
| 96 | +} |
0 commit comments