-
Notifications
You must be signed in to change notification settings - Fork 789
/
type.go
214 lines (182 loc) · 6.59 KB
/
type.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright (c) 2018 Ashley Jeffs
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package manager
import (
"fmt"
"net/http"
"sync"
"github.com/Jeffail/benthos/lib/cache"
"github.com/Jeffail/benthos/lib/log"
"github.com/Jeffail/benthos/lib/metrics"
"github.com/Jeffail/benthos/lib/processor/condition"
"github.com/Jeffail/benthos/lib/types"
)
//------------------------------------------------------------------------------
// APIReg is an interface representing an API builder.
type APIReg interface {
RegisterEndpoint(path, desc string, h http.HandlerFunc)
}
//------------------------------------------------------------------------------
// Config contains all configuration fields for a Benthos service manager.
type Config struct {
Caches map[string]cache.Config `json:"caches" yaml:"caches"`
Conditions map[string]condition.Config `json:"conditions" yaml:"conditions"`
}
// NewConfig returns a Config with default values.
func NewConfig() Config {
return Config{
Caches: map[string]cache.Config{},
Conditions: map[string]condition.Config{},
}
}
// AddExamples inserts example caches and conditions if none exist in the
// config.
func AddExamples(c *Config) {
if len(c.Caches) == 0 {
c.Caches["example"] = cache.NewConfig()
}
if len(c.Conditions) == 0 {
c.Conditions["example"] = condition.NewConfig()
}
}
//------------------------------------------------------------------------------
// SanitiseConfig creates a sanitised version of a manager config.
func SanitiseConfig(conf Config) (interface{}, error) {
var err error
caches := map[string]interface{}{}
for k, v := range conf.Caches {
if caches[k], err = cache.SanitiseConfig(v); err != nil {
return nil, err
}
}
conditions := map[string]interface{}{}
for k, v := range conf.Conditions {
if conditions[k], err = condition.SanitiseConfig(v); err != nil {
return nil, err
}
}
return map[string]interface{}{
"caches": caches,
"conditions": conditions,
}, nil
}
//------------------------------------------------------------------------------
// Type is an implementation of types.Manager, which is expected by Benthos
// components that need to register service wide behaviours such as HTTP
// endpoints and event listeners, and obtain service wide shared resources such
// as caches and labelled conditions.
type Type struct {
apiReg APIReg
caches map[string]types.Cache
conditions map[string]types.Condition
pipes map[string]<-chan types.Transaction
pipeLock sync.RWMutex
}
// New returns an instance of manager.Type, which can be shared amongst
// components and logical threads of a Benthos service.
func New(
conf Config,
apiReg APIReg,
log log.Modular,
stats metrics.Type,
) (*Type, error) {
t := &Type{
apiReg: apiReg,
caches: map[string]types.Cache{},
conditions: map[string]types.Condition{},
pipes: map[string]<-chan types.Transaction{},
}
for k, conf := range conf.Caches {
newCache, err := cache.New(conf, t, log.NewModule(".resource."+k), metrics.Namespaced(stats, "resource."+k))
if err != nil {
return nil, fmt.Errorf(
"failed to create cache resource '%v' of type '%v': %v",
k, conf.Type, err,
)
}
t.caches[k] = newCache
}
// Sometimes condition resources might refer to other condition resources.
// When they are constructed they will check with the manager to ensure the
// resource they point to is valid, but not use the condition. Since we
// cannot guarantee an order of initialisation we create placeholder
// conditions during construction.
for k := range conf.Conditions {
t.conditions[k] = nil
}
// TODO: Prevent recursive conditions.
for k, newConf := range conf.Conditions {
newCond, err := condition.New(newConf, t, log.NewModule(".resource."+k), metrics.Namespaced(stats, "resource."+k))
if err != nil {
return nil, fmt.Errorf(
"failed to create condition resource '%v' of type '%v': %v",
k, newConf.Type, err,
)
}
t.conditions[k] = newCond
}
// Note: Caches and conditions are considered READONLY from this point
// onwards and are therefore NOT protected by mutexes or channels.
return t, nil
}
//------------------------------------------------------------------------------
// RegisterEndpoint registers a server wide HTTP endpoint.
func (t *Type) RegisterEndpoint(path, desc string, h http.HandlerFunc) {
t.apiReg.RegisterEndpoint(path, desc, h)
}
// GetCache attempts to find a service wide cache by its name.
func (t *Type) GetCache(name string) (types.Cache, error) {
if c, exists := t.caches[name]; exists {
return c, nil
}
return nil, types.ErrCacheNotFound
}
// GetPipe attempts to obtain and return a named output Pipe
func (t *Type) GetPipe(name string) (<-chan types.Transaction, error) {
t.pipeLock.RLock()
pipe, exists := t.pipes[name]
t.pipeLock.RUnlock()
if exists {
return pipe, nil
}
return nil, types.ErrPipeNotFound
}
// SetPipe registers a new transaction chan to a named pipe.
func (t *Type) SetPipe(name string, tran <-chan types.Transaction) {
t.pipeLock.Lock()
t.pipes[name] = tran
t.pipeLock.Unlock()
}
// UnsetPipe removes a named pipe transaction chan.
func (t *Type) UnsetPipe(name string, tran <-chan types.Transaction) {
t.pipeLock.Lock()
if otran, exists := t.pipes[name]; exists && otran == tran {
delete(t.pipes, name)
}
t.pipeLock.Unlock()
}
// GetCondition attempts to find a service wide condition by its name.
func (t *Type) GetCondition(name string) (types.Condition, error) {
if c, exists := t.conditions[name]; exists {
return c, nil
}
return nil, types.ErrConditionNotFound
}
//------------------------------------------------------------------------------