frostfs-node/internal/qos/semaphore.go
Dmitrii Stepanov 5e60db6705
Some checks failed
DCO action / DCO (pull_request) Successful in 37s
Tests and linters / Run gofumpt (pull_request) Successful in 36s
Vulncheck / Vulncheck (pull_request) Successful in 55s
Tests and linters / Staticcheck (pull_request) Failing after 1m21s
Build / Build Components (pull_request) Successful in 1m41s
Tests and linters / Lint (pull_request) Failing after 1m50s
Pre-commit hooks / Pre-commit (pull_request) Failing after 1m55s
Tests and linters / Tests (pull_request) Successful in 2m14s
Tests and linters / gopls check (pull_request) Successful in 2m55s
Tests and linters / Tests with -race (pull_request) Successful in 3m15s
[#9999] 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>
2025-02-14 10:05:06 +03:00

39 lines
670 B
Go

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, tag 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
}