frostfs-qos/limiting/semaphore/semaphore.go
Aleksey Savchuk 3a15523b78
[#4] limiting: Add limiter
Signed-off-by: Aleksey Savchuk <a.savchuk@yadro.com>
2025-02-13 14:34:02 +03:00

27 lines
429 B
Go

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