You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Licensed under the Apache License, Version 2.0 (the "License");
5
+
you may not use this file except in compliance with the License.
6
+
You may obtain a copy of the License at
7
+
8
+
http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+
Unless required by applicable law or agreed to in writing, software
11
+
distributed under the License is distributed on an "AS IS" BASIS,
12
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+
See the License for the specific language governing permissions and
14
+
limitations under the License.
15
+
*/
16
+
17
+
package types
18
+
19
+
import (
20
+
"errors"
21
+
"sync"
22
+
)
23
+
24
+
var (
25
+
// ErrNotFound is the not found error message.
26
+
ErrNotFound=errors.New("not found")
27
+
)
28
+
29
+
// StateData is a generic type for arbitrary data stored in CycleState.
30
+
typeStateDatainterface {
31
+
// Clone is an interface to make a copy of StateData.
32
+
Clone() StateData
33
+
}
34
+
35
+
// StateKey is the type of keys stored in CycleState.
36
+
typeStateKeystring
37
+
38
+
// NewCycleState initializes a new CycleState and returns its pointer.
39
+
funcNewCycleState() *CycleState {
40
+
return&CycleState{}
41
+
}
42
+
43
+
// CycleState provides a mechanism for plugins to store and retrieve arbitrary data.
44
+
// StateData stored by one plugin can be read, altered, or deleted by another plugin.
45
+
// CycleState does not provide any data protection, as all plugins are assumed to be
46
+
// trusted.
47
+
// Note: CycleState uses a sync.Map to back the storage, because it is thread safe. It's aimed to optimize for the "write once and read many times" scenarios.
48
+
typeCycleStatestruct {
49
+
// key: StateKey, value: StateData
50
+
storage sync.Map
51
+
}
52
+
53
+
// Clone creates a copy of CycleState and returns its pointer. Clone returns
54
+
// nil if the context being cloned is nil.
55
+
func (c*CycleState) Clone() *CycleState {
56
+
ifc==nil {
57
+
returnnil
58
+
}
59
+
copy:=NewCycleState()
60
+
// Safe copy storage in case of overwriting.
61
+
c.storage.Range(func(k, vinterface{}) bool {
62
+
copy.storage.Store(k, v.(StateData).Clone())
63
+
returntrue
64
+
})
65
+
66
+
returncopy
67
+
}
68
+
69
+
// Read retrieves data with the given "key" from CycleState. If the key is not
0 commit comments