forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 1
/
type.go
156 lines (133 loc) · 5.04 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
// 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"
"github.com/Jeffail/benthos/lib/cache"
"github.com/Jeffail/benthos/lib/processor/condition"
"github.com/Jeffail/benthos/lib/types"
"github.com/Jeffail/benthos/lib/util/service/log"
"github.com/Jeffail/benthos/lib/util/service/metrics"
)
//------------------------------------------------------------------------------
// 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{
"example": cache.NewConfig(),
},
Conditions: map[string]condition.Config{
"example": condition.NewConfig(),
},
}
}
//------------------------------------------------------------------------------
// 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
}
// 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{},
}
for k, conf := range conf.Caches {
newCache, err := cache.New(conf, t, log, stats)
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 will 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 simple create placeholder
// conditions during construction. However, we make sure we don't add the
// constructing condition itself in order to avoid recursive conditions.
for k := range conf.Conditions {
t.conditions[k] = nil
}
// TODO: Prevent recursive conditions.
for k, newConf := range conf.Conditions {
for k2 := range conf.Conditions {
if k != k2 {
t.conditions[k2] = nil
}
}
newCond, err := condition.New(newConf, t, log, stats)
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
}
// 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
}
//------------------------------------------------------------------------------