-
Notifications
You must be signed in to change notification settings - Fork 199
/
interceptorsContainer.go
148 lines (120 loc) · 3.64 KB
/
interceptorsContainer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package containers
import (
"fmt"
"github.com/ElrondNetwork/elrond-go-core/core/check"
"github.com/ElrondNetwork/elrond-go-core/core/container"
logger "github.com/ElrondNetwork/elrond-go-logger"
"github.com/ElrondNetwork/elrond-go/process"
)
var _ process.InterceptorsContainer = (*interceptorsContainer)(nil)
var log = logger.GetOrCreate("process/factory/containers")
// interceptorsContainer is an interceptors holder organized by type
type interceptorsContainer struct {
objects *container.MutexMap
}
// NewInterceptorsContainer will create a new instance of a container
func NewInterceptorsContainer() *interceptorsContainer {
return &interceptorsContainer{
objects: container.NewMutexMap(),
}
}
// Get returns the object stored at a certain key.
// Returns an error if the element does not exist
func (ic *interceptorsContainer) Get(key string) (process.Interceptor, error) {
value, ok := ic.objects.Get(key)
if !ok {
return nil, fmt.Errorf("%w in interceptors container for key %v", process.ErrInvalidContainerKey, key)
}
interceptor, ok := value.(process.Interceptor)
if !ok {
return nil, process.ErrWrongTypeInContainer
}
return interceptor, nil
}
// Add will add an object at a given key. Returns
// an error if the element already exists
func (ic *interceptorsContainer) Add(key string, interceptor process.Interceptor) error {
if check.IfNil(interceptor) {
return process.ErrNilContainerElement
}
ok := ic.objects.Insert(key, interceptor)
if !ok {
return process.ErrContainerKeyAlreadyExists
}
return nil
}
// AddMultiple will add objects with given keys. Returns
// an error if one element already exists, lengths mismatch or an interceptor is nil
func (ic *interceptorsContainer) AddMultiple(keys []string, interceptors []process.Interceptor) error {
if len(keys) != len(interceptors) {
return process.ErrLenMismatch
}
for idx, key := range keys {
if len(key) == 0 {
continue
}
err := ic.Add(key, interceptors[idx])
if err != nil {
return err
}
}
return nil
}
// Replace will add (or replace if it already exists) an object at a given key
func (ic *interceptorsContainer) Replace(key string, interceptor process.Interceptor) error {
if check.IfNil(interceptor) {
return process.ErrNilContainerElement
}
ic.objects.Set(key, interceptor)
return nil
}
// Remove will remove an object at a given key
func (ic *interceptorsContainer) Remove(key string) {
ic.objects.Remove(key)
}
// Len returns the length of the added objects
func (ic *interceptorsContainer) Len() int {
return ic.objects.Len()
}
// Iterate will call the provided handler for each and every key-value pair
func (ic *interceptorsContainer) Iterate(handler func(key string, interceptor process.Interceptor) bool) {
if handler == nil {
return
}
for _, keyVal := range ic.objects.Keys() {
key, ok := keyVal.(string)
if !ok {
continue
}
val, ok := ic.objects.Get(key)
if !ok {
continue
}
interceptor, ok := val.(process.Interceptor)
if !ok {
continue
}
shouldContinue := handler(key, interceptor)
if !shouldContinue {
return
}
}
}
// Close will call the close method on all contained interceptors
func (ic *interceptorsContainer) Close() error {
var errFound error
ic.Iterate(func(key string, interceptor process.Interceptor) bool {
log.Debug("interceptorsContainer closing interceptor", "key", key)
err := interceptor.Close()
if err != nil {
log.Error("error closing interceptor", "key", key, "error", err)
errFound = err
}
return true
})
return errFound
}
// IsInterfaceNil returns true if there is no value under the interface
func (ic *interceptorsContainer) IsInterfaceNil() bool {
return ic == nil
}