@@ -51,7 +51,7 @@ func NewPluginState(ctx context.Context) *PluginState {
51
51
// Note: PluginState uses a sync.Map to back the storage, because it is thread safe.
52
52
// It's aimed to optimize for the "write once and read many times" scenarios.
53
53
type PluginState struct {
54
- // key: RequestID, value: map [StateKey]StateData
54
+ // key: RequestID, value: sync.Map [StateKey]StateData
55
55
storage sync.Map
56
56
// key: RequestID, value: time.Time
57
57
requestToLastAccessTime sync.Map
@@ -66,9 +66,9 @@ func (s *PluginState) Read(requestID string, key StateKey) (StateData, error) {
66
66
return nil , ErrNotFound
67
67
}
68
68
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
72
72
}
73
73
74
74
return nil , ErrNotFound
@@ -77,15 +77,15 @@ func (s *PluginState) Read(requestID string, key StateKey) (StateData, error) {
77
77
// Write stores the given "val" in PluginState with the given "key" in the context of the given "requestID".
78
78
func (s * PluginState ) Write (requestID string , key StateKey , val StateData ) {
79
79
s .requestToLastAccessTime .Store (requestID , time .Now ())
80
- var stateData map [ StateKey ] StateData
80
+ var stateData * sync. Map
81
81
stateMap , ok := s .storage .Load (requestID )
82
82
if ok {
83
- stateData = stateMap .(map [ StateKey ] StateData )
83
+ stateData = stateMap .(* sync. Map )
84
84
} else {
85
- stateData = map [ StateKey ] StateData {}
85
+ stateData = & sync. Map {}
86
86
}
87
87
88
- stateData [ key ] = val
88
+ stateData . Store ( key , val )
89
89
90
90
s .storage .Store (requestID , stateData )
91
91
}
0 commit comments