[#1636] qos: Add semaphore limiter
If no tags specified, then limiter could be optimized to use atomic semaphore. Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
This commit is contained in:
parent
5c7a0e1cac
commit
1696d09c4d
2 changed files with 65 additions and 24 deletions
39
internal/qos/semaphore.go
Normal file
39
internal/qos/semaphore.go
Normal file
|
@ -0,0 +1,39 @@
|
|||
package qos
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sync/atomic"
|
||||
|
||||
"git.frostfs.info/TrueCloudLab/frostfs-qos/scheduling"
|
||||
)
|
||||
|
||||
var (
|
||||
_ scheduler = (*semaphore)(nil)
|
||||
errSemaphoreLimitExceeded = errors.New("semaphore limit exceeded")
|
||||
)
|
||||
|
||||
type semaphore struct {
|
||||
count atomic.Int64
|
||||
limit int64
|
||||
}
|
||||
|
||||
func (s *semaphore) Close() {}
|
||||
|
||||
func (s *semaphore) RequestArrival(ctx context.Context, _ string) (scheduling.ReleaseFunc, error) {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil, ctx.Err()
|
||||
default:
|
||||
}
|
||||
|
||||
v := s.count.Add(1)
|
||||
if v > s.limit {
|
||||
s.count.Add(-1)
|
||||
return nil, errSemaphoreLimitExceeded
|
||||
}
|
||||
|
||||
return func() {
|
||||
s.count.Add(-1)
|
||||
}, nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue