move timedValue -> cachevalue package (#19114)
This commit is contained in:
96
cmd/utils.go
96
cmd/utils.go
@@ -926,102 +926,6 @@ func iamPolicyClaimNameSA() string {
|
||||
return "sa-policy"
|
||||
}
|
||||
|
||||
// timedValue contains a synchronized value that is considered valid
|
||||
// for a specific amount of time.
|
||||
// An Update function must be set to provide an updated value when needed.
|
||||
type timedValue[I any] struct {
|
||||
// Update must return an updated value.
|
||||
// If an error is returned the cached value is not set.
|
||||
// Only one caller will call this function at any time, others will be blocking.
|
||||
// The returned value can no longer be modified once returned.
|
||||
// Should be set before calling Get().
|
||||
Update func() (item I, err error)
|
||||
|
||||
// TTL for a cached value.
|
||||
// If not set 1 second TTL is assumed.
|
||||
// Should be set before calling Get().
|
||||
TTL time.Duration
|
||||
|
||||
// When set to true, return the last cached value
|
||||
// even if updating the value errors out
|
||||
Relax bool
|
||||
|
||||
// Once can be used to initialize values for lazy initialization.
|
||||
// Should be set before calling Get().
|
||||
Once sync.Once
|
||||
|
||||
// Managed values.
|
||||
value I
|
||||
valueSet bool
|
||||
lastUpdate time.Time
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
// newTimedValue
|
||||
func newTimedValue[I any]() *timedValue[I] {
|
||||
return &timedValue[I]{}
|
||||
}
|
||||
|
||||
// Get will return a cached value or fetch a new one.
|
||||
// If the Update function returns an error the value is forwarded as is and not cached.
|
||||
func (t *timedValue[I]) Get() (item I, err error) {
|
||||
item, ok := t.get(t.ttl())
|
||||
if ok {
|
||||
return item, nil
|
||||
}
|
||||
|
||||
item, err = t.Update()
|
||||
if err != nil {
|
||||
if t.Relax {
|
||||
// if update fails, return current
|
||||
// cached value along with error.
|
||||
//
|
||||
// Let the caller decide if they want
|
||||
// to use the returned value based
|
||||
// on error.
|
||||
item, ok = t.get(0)
|
||||
if ok {
|
||||
return item, err
|
||||
}
|
||||
}
|
||||
return item, err
|
||||
}
|
||||
|
||||
t.update(item)
|
||||
return item, nil
|
||||
}
|
||||
|
||||
func (t *timedValue[_]) ttl() time.Duration {
|
||||
ttl := t.TTL
|
||||
if ttl <= 0 {
|
||||
ttl = time.Second
|
||||
}
|
||||
return ttl
|
||||
}
|
||||
|
||||
func (t *timedValue[I]) get(ttl time.Duration) (item I, ok bool) {
|
||||
t.mu.RLock()
|
||||
defer t.mu.RUnlock()
|
||||
if t.valueSet {
|
||||
item = t.value
|
||||
if ttl <= 0 {
|
||||
return item, true
|
||||
}
|
||||
if time.Since(t.lastUpdate) < ttl {
|
||||
return item, true
|
||||
}
|
||||
}
|
||||
return item, false
|
||||
}
|
||||
|
||||
func (t *timedValue[I]) update(item I) {
|
||||
t.mu.Lock()
|
||||
defer t.mu.Unlock()
|
||||
t.value = item
|
||||
t.valueSet = true
|
||||
t.lastUpdate = time.Now()
|
||||
}
|
||||
|
||||
// On MinIO a directory object is stored as a regular object with "__XLDIR__" suffix.
|
||||
// For ex. "prefix/" is stored as "prefix__XLDIR__"
|
||||
func encodeDirObject(object string) string {
|
||||
|
||||
Reference in New Issue
Block a user