Skip to content

Commit 68708ce

Browse files
committed
refactor(core): replace manual sync map with LRUExpireCache in progress store
Signed-off-by: Daniil Antoshin <daniil.antoshin@flant.com>
1 parent 8fba32e commit 68708ce

1 file changed

Lines changed: 16 additions & 17 deletions

File tree

  • images/virtualization-artifact/pkg/controller/vmop/migration/internal/progress

images/virtualization-artifact/pkg/controller/vmop/migration/internal/progress/store.go

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@ limitations under the License.
1717
package progress
1818

1919
import (
20-
"sync"
2120
"time"
2221

2322
"k8s.io/apimachinery/pkg/types"
23+
utilcache "k8s.io/apimachinery/pkg/util/cache"
24+
)
25+
26+
const (
27+
storeMaxSize = 1024
28+
storeTTL = 30 * time.Minute
2429
)
2530

2631
type State struct {
@@ -35,41 +40,35 @@ type State struct {
3540
}
3641

3742
type Store struct {
38-
mu sync.RWMutex
39-
states map[types.UID]State
43+
cache *utilcache.LRUExpireCache
4044
}
4145

4246
func NewStore() *Store {
43-
return &Store{states: make(map[types.UID]State)}
47+
return &Store{cache: utilcache.NewLRUExpireCache(storeMaxSize)}
4448
}
4549

4650
func (s *Store) Load(uid types.UID) (State, bool) {
47-
s.mu.RLock()
48-
defer s.mu.RUnlock()
49-
state, ok := s.states[uid]
50-
return state, ok
51+
v, ok := s.cache.Get(uid)
52+
if !ok {
53+
return State{}, false
54+
}
55+
return v.(State), true
5156
}
5257

5358
func (s *Store) Store(uid types.UID, state State) {
5459
if uid == "" {
5560
return
5661
}
57-
s.mu.Lock()
58-
defer s.mu.Unlock()
59-
s.states[uid] = state
62+
s.cache.Add(uid, state, storeTTL)
6063
}
6164

6265
func (s *Store) Delete(uid types.UID) {
6366
if uid == "" {
6467
return
6568
}
66-
s.mu.Lock()
67-
defer s.mu.Unlock()
68-
delete(s.states, uid)
69+
s.cache.Remove(uid)
6970
}
7071

7172
func (s *Store) Len() int {
72-
s.mu.RLock()
73-
defer s.mu.RUnlock()
74-
return len(s.states)
73+
return len(s.cache.Keys())
7574
}

0 commit comments

Comments
 (0)