Skip to content

Adjust scaling logic in ScaleDown and ScaleUp functions #170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions concurrency/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@ import "go.uber.org/zap"
// }
// }
func (ch *ConcurrencyHandler) ScaleDown() {
// Lock to ensure thread safety
ch.lock.Lock()
defer ch.lock.Unlock()

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

currentSize := ch.currentCapacity
if currentSize < MaxConcurrency {
// Scale up by 10% of the available margin, ensuring we do not exceed MaxConcurrency
newSize := currentSize + int64(float64(MaxConcurrency-currentSize)*0.1)
newSize = min(newSize, MaxConcurrency)
ch.logger.Info("Increasing request concurrency", zap.Int64("currentSize", currentSize), zap.Int64("newSize", newSize))
ch.ResizeSemaphore(newSize)
// Calculate the increase based on a percentage of the available margin
increase := int64(float64(MaxConcurrency-currentSize) * 0.1)
if increase < 1 {
increase = 1 // Ensure at least a minimum increase of 1
}
newSize := currentSize + increase
newSize = min(newSize, MaxConcurrency) // Ensure not exceeding max limit
if newSize > currentSize { // Check if there is an actual increase
ch.logger.Info("Increasing request concurrency", zap.Int64("currentSize", currentSize), zap.Int64("newSize", newSize))
ch.ResizeSemaphore(newSize)
} else {
ch.logger.Info("Attempted to increase concurrency but already at or near maximum limit", zap.Int64("currentSize", currentSize), zap.Int64("newSize", newSize))
}
} else {
ch.logger.Info("Concurrency already at maximum level; cannot increase further", zap.Int64("currentSize", currentSize))
}
Expand Down