frostfs-qos/limiting/semaphore/semaphore.go
Aleksey Savchuk 59fb93fb23
[#4] limiting: Add limiter
Signed-off-by: Aleksey Savchuk <a.savchuk@yadro.com>
2025-02-13 15:38:27 +03:00

27 lines
363 B
Go

package semaphore
import (
"sync/atomic"
)
type Semaphore struct {
count atomic.Int64
limit int64
}
func NewSemaphore(size int64) *Semaphore {
return &Semaphore{limit: size}
}
func (s *Semaphore) Acquire() bool {
v := s.count.Add(1)
if v > s.limit {
s.count.Add(-1)
return false
}
return true
}
func (s *Semaphore) Release() {
s.count.Add(-1)
}