generated from deploymenttheory/Template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
resize.go
35 lines (33 loc) · 1.25 KB
/
resize.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// 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
}
}
}