forked from TrueCloudLab/frostfs-node
70 lines
1.3 KiB
Go
70 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 {
|
|
m sync.RWMutex
|
|
objs map[oid.Address]struct{}
|
|
}
|
|
|
|
func (oiw *objectsInWork) inWork(addr oid.Address) bool {
|
|
oiw.m.RLock()
|
|
_, ok := oiw.objs[addr]
|
|
oiw.m.RUnlock()
|
|
|
|
return ok
|
|
}
|
|
|
|
func (oiw *objectsInWork) remove(addr oid.Address) {
|
|
oiw.m.Lock()
|
|
delete(oiw.objs, addr)
|
|
oiw.m.Unlock()
|
|
}
|
|
|
|
func (oiw *objectsInWork) add(addr oid.Address) {
|
|
oiw.m.Lock()
|
|
oiw.objs[addr] = struct{}{}
|
|
oiw.m.Unlock()
|
|
}
|
|
|
|
// 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 = &logger.Logger{Logger: c.log.With(zap.String("component", "Object Policer"))}
|
|
|
|
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.maxCapacity),
|
|
},
|
|
}
|
|
}
|