Skip to content

Commit 3c81bed

Browse files
authored
Merge pull request #170 from deploymenttheory/dev
Adjust scaling logic in ScaleDown and ScaleUp functions
2 parents 4a19620 + ea07767 commit 3c81bed

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

concurrency/scale.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,19 @@ import "go.uber.org/zap"
2020
// }
2121
// }
2222
func (ch *ConcurrencyHandler) ScaleDown() {
23-
// Lock to ensure thread safety
2423
ch.lock.Lock()
2524
defer ch.lock.Unlock()
2625

27-
// We must consider the current capacity rather than the length of the semaphore
2826
currentSize := ch.currentCapacity
2927
if currentSize > MinConcurrency {
30-
newSize := currentSize - 1
31-
ch.logger.Info("Reducing request concurrency", zap.Int64("currentSize", currentSize), zap.Int64("newSize", newSize))
32-
ch.ResizeSemaphore(newSize)
28+
// Check if active permits allow for scaling down
29+
if ch.activePermits < currentSize {
30+
newSize := currentSize - 1
31+
ch.logger.Info("Reducing request concurrency", zap.Int64("currentSize", currentSize), zap.Int64("newSize", newSize))
32+
ch.ResizeSemaphore(newSize)
33+
} else {
34+
ch.logger.Info("Cannot scale down due to high number of active permits", zap.Int64("currentSize", currentSize), zap.Int64("activePermits", ch.activePermits))
35+
}
3336
} else {
3437
ch.logger.Info("Concurrency already at minimum level; cannot reduce further", zap.Int64("currentSize", currentSize))
3538
}
@@ -57,11 +60,19 @@ func (ch *ConcurrencyHandler) ScaleUp() {
5760

5861
currentSize := ch.currentCapacity
5962
if currentSize < MaxConcurrency {
60-
// Scale up by 10% of the available margin, ensuring we do not exceed MaxConcurrency
61-
newSize := currentSize + int64(float64(MaxConcurrency-currentSize)*0.1)
62-
newSize = min(newSize, MaxConcurrency)
63-
ch.logger.Info("Increasing request concurrency", zap.Int64("currentSize", currentSize), zap.Int64("newSize", newSize))
64-
ch.ResizeSemaphore(newSize)
63+
// Calculate the increase based on a percentage of the available margin
64+
increase := int64(float64(MaxConcurrency-currentSize) * 0.1)
65+
if increase < 1 {
66+
increase = 1 // Ensure at least a minimum increase of 1
67+
}
68+
newSize := currentSize + increase
69+
newSize = min(newSize, MaxConcurrency) // Ensure not exceeding max limit
70+
if newSize > currentSize { // Check if there is an actual increase
71+
ch.logger.Info("Increasing request concurrency", zap.Int64("currentSize", currentSize), zap.Int64("newSize", newSize))
72+
ch.ResizeSemaphore(newSize)
73+
} else {
74+
ch.logger.Info("Attempted to increase concurrency but already at or near maximum limit", zap.Int64("currentSize", currentSize), zap.Int64("newSize", newSize))
75+
}
6576
} else {
6677
ch.logger.Info("Concurrency already at maximum level; cannot increase further", zap.Int64("currentSize", currentSize))
6778
}

0 commit comments

Comments
 (0)