-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapterhost.go
234 lines (196 loc) · 6.11 KB
/
adapterhost.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package discovery
import (
"context"
"errors"
"fmt"
"strings"
"sync"
"time"
"github.com/getsentry/sentry-go"
"github.com/overmindtech/sdp-go"
log "github.com/sirupsen/logrus"
)
// AdapterHost This struct holds references to all Adapters in a process
// and provides utility functions to work with them. Methods of this
// struct are safe to call concurrently.
type AdapterHost struct {
// Map of types to all adapters for that type
adapters []Adapter
mutex sync.RWMutex
}
func NewAdapterHost() *AdapterHost {
sh := &AdapterHost{
adapters: make([]Adapter, 0),
}
// Add meta-adapters so that we can respond to queries for `overmind-type`,
// `overmind-scope` and `overmind-adapter` resources
sh.addBuiltinAdapters()
return sh
}
func (sh *AdapterHost) addBuiltinAdapters() {
_ = sh.AddAdapters(&TypeAdapter{sh: sh})
_ = sh.AddAdapters(&ScopeAdapter{sh: sh})
_ = sh.AddAdapters(&SourcesAdapter{sh: sh})
}
var ErrAdapterAlreadyExists = errors.New("adapter already exists")
// AddAdapters Adds an adapter to this engine
func (sh *AdapterHost) AddAdapters(adapters ...Adapter) error {
sh.mutex.Lock()
defer sh.mutex.Unlock()
for _, newAdapter := range adapters {
for _, existingAdapter := range sh.adapters {
if existingAdapter.Type() == newAdapter.Type() && scopesOverlap(existingAdapter.Scopes(), newAdapter.Scopes()) {
log.Errorf("Error: Adapter with type %s and overlapping scopes already exists. Existing adapter scopes: %v, New adapter scopes: %v",
existingAdapter.Type(), existingAdapter.Scopes(), newAdapter.Scopes())
return fmt.Errorf("adapter with type %s and overlapping scopes already exists", newAdapter.Type())
}
}
sh.adapters = append(sh.adapters, newAdapter)
}
return nil
}
// scopesOverlap checks if there is any overlap between two slices of scopes
func scopesOverlap(scopes1, scopes2 []string) bool {
scopeSet := make(map[string]struct{}, len(scopes1))
for _, scope := range scopes1 {
scopeSet[scope] = struct{}{}
}
for _, scope := range scopes2 {
if _, exists := scopeSet[scope]; exists {
return true
}
}
return false
}
// Adapters Returns a slice of all known adapters
func (sh *AdapterHost) Adapters() []Adapter {
sh.mutex.RLock()
defer sh.mutex.RUnlock()
adapters := make([]Adapter, 0)
for _, adapter := range sh.adapters {
adapters = append(adapters, adapter)
}
return adapters
}
// VisibleAdapters Returns a slice of all known adapters excluding hidden ones
func (sh *AdapterHost) VisibleAdapters() []Adapter {
allAdapters := sh.Adapters()
result := make([]Adapter, 0)
// Add all adapters unless they are hidden
for _, adapter := range allAdapters {
if hs, ok := adapter.(HiddenAdapter); ok {
if hs.Hidden() {
// If the adapter is hidden, continue without adding it
continue
}
}
result = append(result, adapter)
}
return result
}
// AdapterByType Returns the adapters for a given type
func (sh *AdapterHost) AdaptersByType(typ string) []Adapter {
sh.mutex.RLock()
defer sh.mutex.RUnlock()
adapters := make([]Adapter, 0)
for _, adapter := range sh.adapters {
if adapter.Type() == typ {
adapters = append(adapters, adapter)
}
}
return adapters
}
// ExpandQuery Expands queries with wildcards to no longer contain wildcards.
// Meaning that if we support 5 types, and a query comes in with a wildcard
// type, this function will expand that query into 5 queries, one for each
// type.
//
// The same goes for scopes, if we have a query with a wildcard scope, and
// a single adapter that supports 5 scopes, we will end up with 5 queries. The
// exception to this is if we have a adapter that supports all scopes, but is
// unable to list them. In this case there will still be some queries with
// wildcard scopes as they can't be expanded
//
// This functions returns a map of queries with the adapters that they should be
// run against
func (sh *AdapterHost) ExpandQuery(q *sdp.Query) map[*sdp.Query]Adapter {
var checkAdapters []Adapter
if IsWildcard(q.GetType()) {
// If the query has a wildcard type, all non-hidden adapters might try
// to respond
checkAdapters = sh.VisibleAdapters()
} else {
// If the type is specific, pull just adapters for that type
checkAdapters = append(checkAdapters, sh.AdaptersByType(q.GetType())...)
}
expandedQueries := make(map[*sdp.Query]Adapter)
for _, adapter := range checkAdapters {
// is the adapter is hidden
isHidden := false
if hs, ok := adapter.(HiddenAdapter); ok {
isHidden = hs.Hidden()
}
for _, adapterScope := range adapter.Scopes() {
// Create a new query if:
//
// * The adapter supports all scopes, or
// * The query scope is a wildcard (and the adapter is not hidden), or
// * The query scope substring matches adapter scope
if IsWildcard(adapterScope) || (IsWildcard(q.GetScope()) && !isHidden) || strings.Contains(adapterScope, q.GetScope()) {
dest := sdp.Query{}
q.Copy(&dest)
dest.Type = adapter.Type()
// Choose the more specific scope
if IsWildcard(adapterScope) {
dest.Scope = q.GetScope()
} else {
dest.Scope = adapterScope
}
expandedQueries[&dest] = adapter
}
}
}
return expandedQueries
}
// ClearAllAdapters Removes all adapters from the engine
func (sh *AdapterHost) ClearAllAdapters() {
sh.mutex.Lock()
sh.adapters = make([]Adapter, 0)
sh.mutex.Unlock()
sh.addBuiltinAdapters()
}
// StartPurger Starts the purger for all caching adapters
func (sh *AdapterHost) StartPurger(ctx context.Context) {
for _, s := range sh.Adapters() {
if c, ok := s.(CachingAdapter); ok {
cache := c.Cache()
if cache != nil {
err := cache.StartPurger(ctx)
if err != nil {
sentry.CaptureException(fmt.Errorf("failed to start purger for adapter %s: %w", s.Name(), err))
}
}
}
}
}
func (sh *AdapterHost) Purge() {
for _, s := range sh.Adapters() {
if c, ok := s.(CachingAdapter); ok {
cache := c.Cache()
if cache != nil {
cache.Purge(time.Now())
}
}
}
}
// ClearCaches Clears caches for all caching adapters
func (sh *AdapterHost) ClearCaches() {
for _, s := range sh.Adapters() {
if c, ok := s.(CachingAdapter); ok {
cache := c.Cache()
if cache != nil {
cache.Clear()
}
}
}
}