Skip to content

Commit

Permalink
sized down concurrency package
Browse files Browse the repository at this point in the history
  • Loading branch information
thejoeker12 committed Jul 9, 2024
1 parent 799e33c commit 718302b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 35 deletions.
File renamed without changes.
35 changes: 0 additions & 35 deletions concurrency/resize.go

This file was deleted.

33 changes: 33 additions & 0 deletions concurrency/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,36 @@ func (ch *ConcurrencyHandler) ScaleUp() {
ch.logger.Info("Concurrency already at maximum level; cannot increase further", zap.Int("currentSize", currentSize))
}
}

// ResizeSemaphore adjusts the size of the semaphore used to control concurrency. This method creates a new
// semaphore with the specified new size and closes the old semaphore to ensure that no further tokens can
// be acquired from it. This approach helps manage the transition from the old concurrency level to the new one
// without affecting ongoing operations significantly.
//
// Parameters:
// - newSize: The new size for the semaphore, representing the updated limit on concurrent requests.
//
// This function should be called from within synchronization contexts, such as AdjustConcurrency, to avoid
// race conditions and ensure that changes to the semaphore are consistent with the observed metrics.
func (ch *ConcurrencyHandler) ResizeSemaphore(newSize int) {
newSem := make(chan struct{}, newSize)

// Transfer tokens from the old semaphore to the new one.
for {
select {
case token := <-ch.sem:
select {
case newSem <- token:
// Token transferred to new semaphore.
default:
// New semaphore is full, put token back to the old one to allow ongoing operations to complete.
ch.sem <- token
}
default:
// No more tokens to transfer.
close(ch.sem)
ch.sem = newSem
return
}
}
}
File renamed without changes.

0 comments on commit 718302b

Please sign in to comment.