zotregistry.dev/zot@v1.4.4-0.20240314164342-eec277e14d20/pkg/extensions/sync/features/features.go (about)

     1  package features
     2  
     3  import (
     4  	"sync"
     5  	"time"
     6  )
     7  
     8  const defaultExpireMinutes = 10
     9  
    10  type featureKey struct {
    11  	kind string
    12  	repo string
    13  }
    14  
    15  type featureVal struct {
    16  	enabled bool
    17  	expire  time.Time
    18  }
    19  
    20  type Map struct {
    21  	store       map[featureKey]*featureVal
    22  	expireAfter time.Duration
    23  	mu          *sync.Mutex
    24  }
    25  
    26  func New() *Map {
    27  	return &Map{
    28  		store:       make(map[featureKey]*featureVal),
    29  		expireAfter: defaultExpireMinutes * time.Minute,
    30  		mu:          new(sync.Mutex),
    31  	}
    32  }
    33  
    34  // returns if registry supports this feature and if ok.
    35  func (f *Map) Get(kind, repo string) (bool, bool) {
    36  	f.mu.Lock()
    37  	defer f.mu.Unlock()
    38  
    39  	if feature, ok := f.store[featureKey{kind, repo}]; ok {
    40  		if time.Now().Before(feature.expire) {
    41  			return feature.enabled, true
    42  		}
    43  	}
    44  
    45  	// feature expired or not found
    46  	return false, false
    47  }
    48  
    49  func (f *Map) Set(kind, repo string, enabled bool) {
    50  	f.mu.Lock()
    51  	f.store[featureKey{kind: kind, repo: repo}] = &featureVal{enabled: enabled, expire: time.Now().Add(f.expireAfter)}
    52  	f.mu.Unlock()
    53  }