From 718302b693d61cf018a38ef415c657ffd5bbead7 Mon Sep 17 00:00:00 2001 From: Joseph Little Date: Tue, 9 Jul 2024 15:08:59 +0100 Subject: [PATCH] sized down concurrency package --- concurrency/{const.go => constants.go} | 0 concurrency/resize.go | 35 -------------------------- concurrency/scale.go | 33 ++++++++++++++++++++++++ concurrency/{handler.go => struct.go} | 0 4 files changed, 33 insertions(+), 35 deletions(-) rename concurrency/{const.go => constants.go} (100%) delete mode 100644 concurrency/resize.go rename concurrency/{handler.go => struct.go} (100%) diff --git a/concurrency/const.go b/concurrency/constants.go similarity index 100% rename from concurrency/const.go rename to concurrency/constants.go diff --git a/concurrency/resize.go b/concurrency/resize.go deleted file mode 100644 index 9402e93..0000000 --- a/concurrency/resize.go +++ /dev/null @@ -1,35 +0,0 @@ -// concurrency/resize.go -package concurrency - -// 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 - } - } -} diff --git a/concurrency/scale.go b/concurrency/scale.go index 97fe811..ee8b05a 100644 --- a/concurrency/scale.go +++ b/concurrency/scale.go @@ -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 + } + } +} diff --git a/concurrency/handler.go b/concurrency/struct.go similarity index 100% rename from concurrency/handler.go rename to concurrency/struct.go