2022-04-19 17:59:13 +00:00
|
|
|
package tombstone
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/logger"
|
2022-12-30 09:54:48 +00:00
|
|
|
lru "github.com/hashicorp/golang-lru/v2"
|
2022-04-19 17:59:13 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
const defaultLRUCacheSize = 100
|
|
|
|
|
|
|
|
type cfg struct {
|
2022-09-28 07:41:01 +00:00
|
|
|
log *logger.Logger
|
2022-04-19 17:59:13 +00:00
|
|
|
|
|
|
|
cacheSize int
|
|
|
|
|
|
|
|
tsSource Source
|
|
|
|
}
|
|
|
|
|
|
|
|
// Option is an option of ExpirationChecker's constructor.
|
|
|
|
type Option func(*cfg)
|
|
|
|
|
|
|
|
func defaultCfg() *cfg {
|
|
|
|
return &cfg{
|
2024-10-21 07:22:54 +00:00
|
|
|
log: logger.NewLoggerWrapper(zap.NewNop()),
|
2022-04-19 17:59:13 +00:00
|
|
|
cacheSize: defaultLRUCacheSize,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewChecker creates, initializes and returns tombstone ExpirationChecker.
|
|
|
|
// The returned structure is ready to use.
|
|
|
|
//
|
|
|
|
// Panics if any of the provided options does not allow
|
|
|
|
// constructing a valid tombstone ExpirationChecker.
|
|
|
|
func NewChecker(oo ...Option) *ExpirationChecker {
|
|
|
|
cfg := defaultCfg()
|
|
|
|
|
|
|
|
for _, o := range oo {
|
|
|
|
o(cfg)
|
|
|
|
}
|
|
|
|
|
2023-02-21 11:42:45 +00:00
|
|
|
panicOnNil := func(v any, name string) {
|
2022-04-19 17:59:13 +00:00
|
|
|
if v == nil {
|
|
|
|
panic(fmt.Sprintf("tombstone getter constructor: %s is nil", name))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
panicOnNil(cfg.tsSource, "Tombstone source")
|
|
|
|
|
2022-12-30 09:54:48 +00:00
|
|
|
cache, err := lru.New[string, uint64](cfg.cacheSize)
|
2022-04-19 17:59:13 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("could not create LRU cache with %d size: %w", cfg.cacheSize, err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ExpirationChecker{
|
|
|
|
cache: cache,
|
|
|
|
log: cfg.log,
|
|
|
|
tsSource: cfg.tsSource,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithLogger returns an option to specify
|
|
|
|
// logger.
|
2022-09-28 07:41:01 +00:00
|
|
|
func WithLogger(v *logger.Logger) Option {
|
2022-04-19 17:59:13 +00:00
|
|
|
return func(c *cfg) {
|
|
|
|
c.log = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithCacheSize returns an option to specify
|
|
|
|
// LRU cache size.
|
|
|
|
func WithCacheSize(v int) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.cacheSize = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithTombstoneSource returns an option
|
|
|
|
// to specify tombstone source.
|
|
|
|
func WithTombstoneSource(v Source) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.tsSource = v
|
|
|
|
}
|
|
|
|
}
|