All checks were successful
DCO action / DCO (pull_request) Successful in 42s
Vulncheck / Vulncheck (pull_request) Successful in 52s
Pre-commit hooks / Pre-commit (pull_request) Successful in 1m25s
Build / Build Components (pull_request) Successful in 1m45s
Tests and linters / Run gofumpt (pull_request) Successful in 2m52s
Tests and linters / Lint (pull_request) Successful in 3m33s
Tests and linters / Staticcheck (pull_request) Successful in 3m30s
Tests and linters / gopls check (pull_request) Successful in 3m37s
Tests and linters / Tests with -race (pull_request) Successful in 3m47s
Tests and linters / Tests (pull_request) Successful in 3m56s
Signed-off-by: Anton Nikiforov <an.nikiforov@yadro.com>
72 lines
1.3 KiB
Go
72 lines
1.3 KiB
Go
package policer
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/logger"
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
lru "github.com/hashicorp/golang-lru/v2"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
type objectsInWork struct {
|
|
sync.RWMutex
|
|
objs map[oid.Address]struct{}
|
|
}
|
|
|
|
func (oiw *objectsInWork) inWork(addr oid.Address) bool {
|
|
oiw.RLock()
|
|
_, ok := oiw.objs[addr]
|
|
oiw.RUnlock()
|
|
|
|
return ok
|
|
}
|
|
|
|
func (oiw *objectsInWork) remove(addr oid.Address) {
|
|
oiw.Lock()
|
|
delete(oiw.objs, addr)
|
|
oiw.Unlock()
|
|
}
|
|
|
|
func (oiw *objectsInWork) add(addr oid.Address) bool {
|
|
oiw.Lock()
|
|
_, exists := oiw.objs[addr]
|
|
oiw.objs[addr] = struct{}{}
|
|
oiw.Unlock()
|
|
return !exists
|
|
}
|
|
|
|
// Policer represents the utility that verifies
|
|
// compliance with the object storage policy.
|
|
type Policer struct {
|
|
*cfg
|
|
|
|
cache *lru.Cache[oid.Address, time.Time]
|
|
|
|
objsInWork *objectsInWork
|
|
}
|
|
|
|
// New creates, initializes and returns Policer instance.
|
|
func New(opts ...Option) *Policer {
|
|
c := defaultCfg()
|
|
|
|
for i := range opts {
|
|
opts[i](c)
|
|
}
|
|
|
|
c.log = c.log.With(zap.String("component", "Object Policer")).WithTag(logger.TagPolicer)
|
|
|
|
cache, err := lru.New[oid.Address, time.Time](int(c.cacheSize))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return &Policer{
|
|
cfg: c,
|
|
cache: cache,
|
|
objsInWork: &objectsInWork{
|
|
objs: make(map[oid.Address]struct{}, c.taskPool.Cap()),
|
|
},
|
|
}
|
|
}
|