-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathperiodic_entity_collector.go
More file actions
300 lines (273 loc) · 8.15 KB
/
periodic_entity_collector.go
File metadata and controls
300 lines (273 loc) · 8.15 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package oidfed
import (
"slices"
"sync"
"time"
"github.com/gofiber/fiber/v2"
"github.com/pkg/errors"
"github.com/go-oidfed/lib/apimodel"
"github.com/go-oidfed/lib/cache"
"github.com/go-oidfed/lib/internal"
"github.com/go-oidfed/lib/internal/utils"
"github.com/go-oidfed/lib/unixtime"
)
// EntityObserver is a callback interface that PeriodicEntityCollector
// can call for each discovered entity, e.g. to trigger proactive resolve generation.
type EntityObserver interface {
// OnDiscoveredEntities is called with the trust anchor and the full set of
// entities discovered for it.
OnDiscoveredEntities(trustAnchor string, entities []*CollectedEntity)
}
// PeriodicEntityCollector runs background entity collection for a set of
// trust anchors at a configurable interval. It implements EntityCollector by
// delegating synchronous collection to an inner Collector while warming caches
// in the background.
type PeriodicEntityCollector struct {
// Collector used for actual collection (defaults to SimpleEntityCollector).
Collector EntityCollector
// TrustAnchors is the full list of trust anchors to iterate over.
TrustAnchors []string
// Interval between background collection rounds.
Interval time.Duration `yaml:"interval"`
// Concurrency limits simultaneous trust anchor collections per run.
// If 0 or negative, a sensible default is used.
Concurrency int `yaml:"concurrency"`
SortEntitiesComparisonFunc func(a, b *CollectedEntity) int
PagingLimit int `yaml:"paging_limit"`
// internal state
cacheMutex sync.RWMutex
startOnce sync.Once
stopOnce sync.Once
stopCh chan struct{}
// Optional handler invoked after each trust anchor collection with the
// discovered entities; can be used to trigger proactive resolver jobs.
Handler EntityObserver
}
type cachedCollection struct {
Entities []*CollectedEntity
LastUpdated unixtime.Unixtime
}
// defaultInterval is used if no interval is configured.
const defaultInterval = time.Hour * 8
// periodicCacheSubsystem defines the cache subsystem key for this collector.
const (
periodicCacheSubsystem = "periodic_entity_collection"
cacheSubSubSystemAll = "all"
cacheSubSubSystemRequests = "requests"
)
// Start launches the periodic background collection. Calling Start multiple
// times is safe; only the first call has an effect.
func (p *PeriodicEntityCollector) Start() {
p.startOnce.Do(
func() {
if p.Collector == nil {
p.Collector = &SimpleEntityCollector{}
}
p.stopCh = make(chan struct{})
interval := p.Interval
if interval <= 0 {
interval = defaultInterval
}
// Run an initial round immediately to warm caches, then tick.
go p.runOnce()
ticker := time.NewTicker(interval)
go func() {
defer ticker.Stop()
for {
select {
case <-ticker.C:
p.runOnce()
case <-p.stopCh:
return
}
}
}()
},
)
}
// Stop stops the background collection loop. It is safe to call multiple times.
func (p *PeriodicEntityCollector) Stop() {
p.stopOnce.Do(
func() {
if p.stopCh != nil {
close(p.stopCh)
}
},
)
}
// CollectEntities serves from the cached periodic results, applying filters
// and trimming based on the request.
func (p *PeriodicEntityCollector) CollectEntities(req apimodel.EntityCollectionRequest) (
*EntityCollectionResponse, *ErrorResponse,
) {
p.Start()
if p.PagingLimit < req.Limit || req.Limit <= 0 {
req.Limit = p.PagingLimit
}
reqHash, err := utils.HashStruct(req)
if err != nil {
return nil, &ErrorResponse{
Status: fiber.StatusInternalServerError,
Error: ErrorServerError(errors.Wrap(err, "PeriodicEntityCollector: error while hashing request").Error()),
}
}
cacheRequestKey := cache.Key(periodicCacheSubsystem, cacheSubSubSystemRequests, reqHash)
var res EntityCollectionResponse
p.cacheMutex.RLock()
set, err := cache.Get(cacheRequestKey, &res)
p.cacheMutex.RUnlock()
if err != nil {
return nil, &ErrorResponse{
Status: fiber.StatusInternalServerError,
Error: ErrorServerError(
errors.Wrap(
err, "PeriodicEntityCollector: error while retrieving cached response",
).Error(),
),
}
}
if set {
return &res, nil
}
var cc cachedCollection
p.cacheMutex.RLock()
set, err = cache.Get(cache.Key(periodicCacheSubsystem, cacheSubSubSystemAll, req.TrustAnchor), &cc)
p.cacheMutex.RUnlock()
if err != nil {
return nil, &ErrorResponse{
Status: fiber.StatusInternalServerError,
Error: ErrorServerError(
errors.Wrap(
err, "PeriodicEntityCollector: error while retrieving cached collection",
).Error(),
),
}
}
if !set {
return nil, &ErrorResponse{
Error: ErrorInvalidTrustAnchor("trust anchor not supported"),
Status: fiber.StatusNotFound,
}
}
entities := FilterAndTrimEntities(cc.Entities, req)
if req.From != "" {
n, found := slices.BinarySearchFunc(
entities, &CollectedEntity{EntityID: req.From}, p.SortEntitiesComparisonFunc,
)
if !found {
return nil, &ErrorResponse{
Error: &Error{Error: EntityIDNotFound},
Status: fiber.StatusNotFound,
}
}
entities = entities[n:]
}
var nextEntityID string
if len(entities) > req.Limit {
others := entities[req.Limit:]
entities = entities[:req.Limit]
nextEntityID = others[0].EntityID
go preparePaginatedResponses(req, others, &cc.LastUpdated, p.Interval)
}
res = EntityCollectionResponse{
Entities: entities,
LastUpdated: &cc.LastUpdated,
Next: nextEntityID,
}
if err = cache.Set(cacheRequestKey, res, p.Interval); err != nil {
internal.WithError(err).Error("PeriodicEntityCollector cache set error")
}
return &res, nil
}
func preparePaginatedResponses(
req apimodel.EntityCollectionRequest, entities []*CollectedEntity,
lastUpdated *unixtime.Unixtime, interval time.Duration,
) {
for len(entities) > 0 {
var others []*CollectedEntity
if len(entities) > req.Limit {
others = entities[req.Limit:]
entities = entities[:req.Limit]
}
var nextEntityID string
if len(others) > 0 {
nextEntityID = others[0].EntityID
}
res := EntityCollectionResponse{
Entities: entities,
LastUpdated: lastUpdated,
Next: nextEntityID,
}
req.From = entities[0].EntityID
reqHash, err := utils.HashStruct(req)
if err != nil {
internal.WithError(err).Error("PeriodicEntityCollector: error while hashing request")
}
cacheRequestKey := cache.Key(periodicCacheSubsystem, cacheSubSubSystemRequests, reqHash)
if err = cache.Set(cacheRequestKey, res, interval); err != nil {
internal.WithError(err).Error("PeriodicEntityCollector cache set error")
}
entities = others
}
}
func (p *PeriodicEntityCollector) runOnce() {
anchors := p.TrustAnchors
if len(anchors) == 0 {
return
}
// Determine effective concurrency.
conc := p.Concurrency
if conc <= 0 {
conc = 8
}
if conc > len(anchors) {
conc = len(anchors)
}
p.cacheMutex.Lock()
defer p.cacheMutex.Unlock()
if err := cache.Clear(periodicCacheSubsystem); err != nil {
internal.WithError(err).Error("PeriodicEntityCollector cache clear error")
}
// Worker pool pattern with a buffered semaphore channel.
sem := make(chan struct{}, conc)
var wg sync.WaitGroup
for _, ta := range anchors {
sem <- struct{}{}
wg.Add(1)
go func(trustAnchor string) {
defer wg.Done()
defer func() { <-sem }()
// Use a minimal request focused on warming caches.
req := apimodel.EntityCollectionRequest{
TrustAnchor: trustAnchor,
EntityClaims: []string{
"entity_types",
"trust_marks",
},
}
internal.Logf("PeriodicEntityCollector: collecting for trust anchor %s", trustAnchor)
res, _ := p.Collector.CollectEntities(req)
if res == nil {
return
}
if p.SortEntitiesComparisonFunc != nil {
slices.SortFunc(res.Entities, p.SortEntitiesComparisonFunc)
}
if err := cache.Set(
cache.Key(periodicCacheSubsystem, cacheSubSubSystemAll, trustAnchor),
cachedCollection{
Entities: res.Entities,
LastUpdated: unixtime.Now(),
},
p.Interval,
); err != nil {
internal.WithError(err).Error("PeriodicEntityCollector cache set error")
}
// Notify handler for proactive resolve generation.
if p.Handler != nil && len(res.Entities) > 0 {
p.Handler.OnDiscoveredEntities(trustAnchor, res.Entities)
}
}(ta)
}
wg.Wait()
}