Skip to content

Commit b100b9f

Browse files
committed
convert map to sync.map in plugin state
Signed-off-by: Nir Rozenbaum <[email protected]>
1 parent b1230a3 commit b100b9f

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

pkg/epp/plugins/plugin_state.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func NewPluginState(ctx context.Context) *PluginState {
5151
// Note: PluginState uses a sync.Map to back the storage, because it is thread safe.
5252
// It's aimed to optimize for the "write once and read many times" scenarios.
5353
type PluginState struct {
54-
// key: RequestID, value: map[StateKey]StateData
54+
// key: RequestID, value: sync.Map[StateKey]StateData
5555
storage sync.Map
5656
// key: RequestID, value: time.Time
5757
requestToLastAccessTime sync.Map
@@ -66,9 +66,9 @@ func (s *PluginState) Read(requestID string, key StateKey) (StateData, error) {
6666
return nil, ErrNotFound
6767
}
6868

69-
stateData := stateMap.(map[StateKey]StateData)
70-
if value, ok := stateData[key]; ok {
71-
return value, nil
69+
stateData := stateMap.(*sync.Map)
70+
if value, ok := stateData.Load(key); ok {
71+
return value.(StateData), nil
7272
}
7373

7474
return nil, ErrNotFound
@@ -77,15 +77,15 @@ func (s *PluginState) Read(requestID string, key StateKey) (StateData, error) {
7777
// Write stores the given "val" in PluginState with the given "key" in the context of the given "requestID".
7878
func (s *PluginState) Write(requestID string, key StateKey, val StateData) {
7979
s.requestToLastAccessTime.Store(requestID, time.Now())
80-
var stateData map[StateKey]StateData
80+
var stateData *sync.Map
8181
stateMap, ok := s.storage.Load(requestID)
8282
if ok {
83-
stateData = stateMap.(map[StateKey]StateData)
83+
stateData = stateMap.(*sync.Map)
8484
} else {
85-
stateData = map[StateKey]StateData{}
85+
stateData = &sync.Map{}
8686
}
8787

88-
stateData[key] = val
88+
stateData.Store(key, val)
8989

9090
s.storage.Store(requestID, stateData)
9191
}

0 commit comments

Comments
 (0)