syncs: add Semaphore.Len (#16981)

The Len reports the number of acquired tokens for metrics.

Updates tailscale/corp#31252

Signed-off-by: Joe Tsai <joetsai@digital-static.net>
This commit is contained in:
Joe Tsai
2025-08-29 10:33:14 -07:00
committed by GitHub
parent 1a98943204
commit 7cbcc10eb1
2 changed files with 21 additions and 0 deletions
+7
View File
@@ -201,6 +201,13 @@ func NewSemaphore(n int) Semaphore {
return Semaphore{c: make(chan struct{}, n)}
}
// Len reports the number of in-flight acquisitions.
// It is incremented whenever the semaphore is acquired.
// It is decremented whenever the semaphore is released.
func (s Semaphore) Len() int {
return len(s.c)
}
// Acquire blocks until a resource is acquired.
func (s Semaphore) Acquire() {
s.c <- struct{}{}