This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 230
/
registry.go
524 lines (476 loc) · 15.2 KB
/
registry.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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
// Copyright 2020 Pilosa Corp.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package testhook
import (
"fmt"
"reflect"
"runtime/debug"
"sync"
"time"
)
// KV represents a key/value mapping. You don't need to use the type
// for anything, it's just to make typing easier.
type KV map[string]interface{}
// Registry represents a set of known objects of a given common type.
// A typical implementation might maintain a map of objects it's seen which
// haven't been deleted. A minimal implementation just does nothing.
//
// A Registry may implement reference-count semantics, or may treat
// reopening of a still-open object as an error.
//
// All objects provided to a registry should have the same type.
type Registry interface {
Created(interface{}, KV) error // Object has been created, and must be destroyed later.
Opened(interface{}, KV) error // Object has been opened, and must be closed later.
Seen(interface{}, KV) error // Object has been interacted with, and should be between an open and a close.
Closed(interface{}, KV) error // Object has been closed, and must have been opened previously.
Destroyed(interface{}, KV) error // Object has been destroyed
Live() (map[interface{}]*RegistryEntry, error) // Currently live objects
}
// RegistryHook is a generic interface, but any real implementation should
// implement at least one of RegistryHookPreOpen, RegistryHookPostOpen,
// etcetera. Pre-hooks are called before any error checks by the registry,
// post-hooks are called with after those error checks, and are passed the
// (possibly nil) error that would be returned at that point. If the post-hook
// overrides the error, the registry continues with operations as though it
// hadn't occurred, which may be a very bad idea.
type RegistryHook interface{}
type RegistryHookPreCreate interface {
Created(interface{}, KV, *RegistryEntry) error
}
type RegistryHookPostCreate interface {
WasCreated(interface{}, KV, *RegistryEntry, error) error
}
type RegistryHookPreOpen interface {
Opened(interface{}, KV, *RegistryEntry) error
}
type RegistryHookPostOpen interface {
WasOpened(interface{}, KV, *RegistryEntry, error) error
}
type RegistryHookPreSee interface {
Seen(interface{}, KV, *RegistryEntry) error
}
type RegistryHookPostSee interface {
WasSeen(interface{}, KV, *RegistryEntry, error) error
}
type RegistryHookPreClose interface {
Closed(interface{}, KV, *RegistryEntry) error
}
type RegistryHookPostClose interface {
WasClosed(interface{}, KV, *RegistryEntry, error) error
}
type RegistryHookPreDestroy interface {
Destroyed(interface{}, KV, *RegistryEntry) error
}
type RegistryHookPostDestroy interface {
WasDestroyed(interface{}, KV, *RegistryEntry, error) error
}
// If a registry's hooks implement RegistryHookLive, Live() should
// return only those entries for which a non-nil error was returned, with the
// error inserted in the RegistryEntry.
type RegistryHookLive interface {
Live(interface{}, *RegistryEntry) error
}
// RegistryHooks represents a set of registry hook values to use for
// registries, corresponding to different types.
type RegistryHooks map[reflect.Type]RegistryHook
// RegistryEntry represents the data we might have about an entry. Every
// entry in it could be zero-valued in some implementations
type RegistryEntry struct {
Error error
Stack []byte
Stamp time.Time
Data KV
OpenCount int
}
// NopRegistry doesn't do anything; it exists to fit the interface but not
// consume resources.
type NopRegistry struct{}
var _ Registry = &NopRegistry{}
func (*NopRegistry) Created(interface{}, KV) error { return nil }
func (*NopRegistry) Opened(interface{}, KV) error { return nil }
func (*NopRegistry) Seen(interface{}, KV) error { return nil }
func (*NopRegistry) Closed(interface{}, KV) error { return nil }
func (*NopRegistry) Destroyed(interface{}, KV) error { return nil }
func (*NopRegistry) Live() (map[interface{}]*RegistryEntry, error) { return nil, nil }
func NewNopRegistry() *NopRegistry {
return &NopRegistry{}
}
// SimpleRegistry asserts that objects are created, then opened, then
// possibly seen, then closed, then destroyed, and that they are not
// opened more than once at a time, or destroyed while open. As a
// convenience feature for users whose use cases might rely on this,
// it will actually accept a new item being opened without being
// previously created; to prevent this, use a PreOpen hook that checks
// for a nil *RegistryEntry. The default Live check will report only
// objects with an open count other than 0, but if you provide a Live
// hook, you can return errors for objects still existing.
type SimpleRegistry struct {
hooks RegistryHook
entries map[interface{}]*RegistryEntry
mu sync.Mutex
}
var _ Registry = &SimpleRegistry{}
func NewSimpleRegistry(hooks RegistryHook) *SimpleRegistry {
return &SimpleRegistry{entries: map[interface{}]*RegistryEntry{}, hooks: hooks}
}
func (s *SimpleRegistry) Created(o interface{}, kv KV) (err error) {
s.mu.Lock()
defer s.mu.Unlock()
existing := s.entries[o]
if hook, ok := s.hooks.(RegistryHookPreCreate); ok {
if err := hook.Created(o, kv, existing); err != nil {
return err
}
}
if existing != nil {
err = fmt.Errorf("object %T:%v previously registered at %v", o, o, existing.Stamp)
} else {
existing = &RegistryEntry{
Stack: debug.Stack(),
Data: kv,
Stamp: time.Now(),
}
}
if hook, ok := s.hooks.(RegistryHookPostCreate); ok {
err = hook.WasCreated(o, kv, existing, err)
}
if err != nil {
return err
}
// if you overrode the error, or there wasn't one and you didn't
// create one, we now stash the entry.
s.entries[o] = existing
return nil
}
func (s *SimpleRegistry) Opened(o interface{}, kv KV) (err error) {
s.mu.Lock()
defer s.mu.Unlock()
existing := s.entries[o]
if hook, ok := s.hooks.(RegistryHookPreOpen); ok {
if err := hook.Opened(o, kv, existing); err != nil {
return err
}
}
if existing == nil {
existing = &RegistryEntry{
Stack: debug.Stack(),
Data: kv,
Stamp: time.Now(),
}
s.entries[o] = existing
}
existing.OpenCount++
if existing.OpenCount > 1 {
err = fmt.Errorf("object %T:%v opened %d times", o, o, existing.OpenCount)
}
if hook, ok := s.hooks.(RegistryHookPostOpen); ok {
err = hook.WasOpened(o, kv, existing, err)
}
if err != nil {
if existing != nil {
existing.OpenCount--
}
return err
}
return nil
}
func (s *SimpleRegistry) Seen(o interface{}, kv KV) (err error) {
s.mu.Lock()
defer s.mu.Unlock()
existing := s.entries[o]
if hook, ok := s.hooks.(RegistryHookPreSee); ok {
if err = hook.Seen(o, kv, existing); err != nil {
return err
}
}
if existing == nil {
err = fmt.Errorf("object %T:%v seen but not previously registered", o, o)
}
if hook, ok := s.hooks.(RegistryHookPostSee); ok {
err = hook.WasSeen(o, kv, existing, err)
}
return err
}
func (s *SimpleRegistry) Closed(o interface{}, kv KV) (err error) {
s.mu.Lock()
defer s.mu.Unlock()
existing := s.entries[o]
if hook, ok := s.hooks.(RegistryHookPreClose); ok {
if err = hook.Closed(o, kv, existing); err != nil {
return err
}
}
if existing == nil {
err = fmt.Errorf("object %T:%v closed but not previously registered", o, o)
} else {
existing.OpenCount--
if existing.OpenCount < 0 {
err = fmt.Errorf("object %T:%v closed more often than it was open: %d", o, o, existing.OpenCount)
}
}
if hook, ok := s.hooks.(RegistryHookPostClose); ok {
err = hook.WasClosed(o, kv, existing, err)
}
if err != nil {
// if a close "failed", we don't want to count it as being closed.
if existing != nil {
existing.OpenCount++
}
return err
}
return nil
}
func (s *SimpleRegistry) Destroyed(o interface{}, kv KV) (err error) {
s.mu.Lock()
defer s.mu.Unlock()
existing := s.entries[o]
if hook, ok := s.hooks.(RegistryHookPreDestroy); ok {
if err = hook.Destroyed(o, kv, existing); err != nil {
return err
}
}
if existing == nil {
err = fmt.Errorf("object %T:%v destroyed but not previously registered", o, o)
} else if existing.OpenCount != 0 {
err = fmt.Errorf("object %T:%v destroyed while open count is %d", o, o, existing.OpenCount)
}
if hook, ok := s.hooks.(RegistryHookPostDestroy); ok {
err = hook.WasDestroyed(o, kv, existing, err)
}
if err != nil {
fmt.Printf("destroyed error: %v\n", err)
return err
}
// remove the entry from the list.
delete(s.entries, o)
return nil
}
func (s *SimpleRegistry) Live() (results map[interface{}]*RegistryEntry, err error) {
s.mu.Lock()
defer s.mu.Unlock()
results = make(map[interface{}]*RegistryEntry)
if hook, ok := s.hooks.(RegistryHookLive); ok {
for k, v := range s.entries {
v.Error = hook.Live(k, v)
if v.Error != nil {
results[k] = v
}
}
} else {
for k, v := range s.entries {
if v.OpenCount != 0 {
v.Error = fmt.Errorf("open count %d", v.OpenCount)
results[k] = v
}
}
}
return results, nil
}
type PreHook func(interface{}, KV, *RegistryEntry) error
type PostHook func(interface{}, KV, *RegistryEntry, error) error
type LiveHook func(interface{}, *RegistryEntry) error
// PluggableRegistry is a registry which takes dynamically-generated functions,
// and calls them if they're not nil.
type PluggableRegistry struct {
implCreated PreHook
implWasCreated PostHook
implOpened PreHook
implWasOpened PostHook
implSeen PreHook
implWasSeen PostHook
implClosed PreHook
implWasClosed PostHook
implDestroyed PreHook
implWasDestroyed PostHook
implLive LiveHook
}
func (p *PluggableRegistry) Created(i interface{}, kv KV, ent *RegistryEntry) error {
if p.implCreated != nil {
return p.implCreated(i, kv, ent)
}
return nil
}
func (p *PluggableRegistry) WasCreated(i interface{}, kv KV, ent *RegistryEntry, err error) error {
if p.implWasCreated != nil {
return p.implWasCreated(i, kv, ent, err)
}
return nil
}
func (p *PluggableRegistry) Opened(i interface{}, kv KV, ent *RegistryEntry) error {
if p.implOpened != nil {
return p.implOpened(i, kv, ent)
}
return nil
}
func (p *PluggableRegistry) WasOpened(i interface{}, kv KV, ent *RegistryEntry, err error) error {
if p.implWasOpened != nil {
return p.implWasOpened(i, kv, ent, err)
}
return nil
}
func (p *PluggableRegistry) Seen(i interface{}, kv KV, ent *RegistryEntry) error {
if p.implSeen != nil {
return p.implSeen(i, kv, ent)
}
return nil
}
func (p *PluggableRegistry) WasSeen(i interface{}, kv KV, ent *RegistryEntry, err error) error {
if p.implWasSeen != nil {
return p.implWasSeen(i, kv, ent, err)
}
return nil
}
func (p *PluggableRegistry) Closed(i interface{}, kv KV, ent *RegistryEntry) error {
if p.implClosed != nil {
return p.implClosed(i, kv, ent)
}
return nil
}
func (p *PluggableRegistry) WasClosed(i interface{}, kv KV, ent *RegistryEntry, err error) error {
if p.implWasClosed != nil {
return p.implWasClosed(i, kv, ent, err)
}
return nil
}
func (p *PluggableRegistry) Destroyed(i interface{}, kv KV, ent *RegistryEntry) error {
if p.implDestroyed != nil {
return p.implDestroyed(i, kv, ent)
}
return nil
}
func (p *PluggableRegistry) WasDestroyed(i interface{}, kv KV, ent *RegistryEntry, err error) error {
if p.implWasDestroyed != nil {
return p.implWasDestroyed(i, kv, ent, err)
}
return nil
}
func (p *PluggableRegistry) Live(i interface{}, ent *RegistryEntry) error {
if p.implLive != nil {
return p.implLive(i, ent)
}
return nil
}
func composePreHooks(fns ...PreHook) PreHook {
if len(fns) == 0 {
return nil
}
if len(fns) == 1 {
return fns[0]
}
return func(i interface{}, kv KV, ent *RegistryEntry) error {
for _, fn := range fns {
err := fn(i, kv, ent)
if err != nil {
return err
}
}
return nil
}
}
func composePostHooks(fns ...PostHook) PostHook {
if len(fns) == 0 {
return nil
}
if len(fns) == 1 {
return fns[0]
}
return func(i interface{}, kv KV, ent *RegistryEntry, err error) error {
// We run the functions in reverse order, so the last
// hook added has the option of overriding a lower hook's
// opinion.
for i := range fns {
fn := fns[len(fns)-1-i]
err = fn(i, kv, ent, err)
}
return err
}
}
func composeLiveHooks(fns ...LiveHook) LiveHook {
if len(fns) == 0 {
return nil
}
if len(fns) == 1 {
return fns[0]
}
return func(i interface{}, ent *RegistryEntry) error {
for _, fn := range fns {
err := fn(i, ent)
if err != nil {
return err
}
}
return nil
}
}
// Compose takes a list of RegistryHook objects, and combines their hook
// functions into a unified registry. For Pre hooks and Live hooks, the
// functions are called in the order they were provided to this function,
// and the first one to return an error causes the remainder to be
// skipped; for Post hooks, they are called in the opposite order, and
// the whole list continues to be called, with each function getting passed
// the error (or non-error) value returned by the previous one.
func Compose(hooks ...RegistryHook) *PluggableRegistry {
var created, opened, seen, closed, destroyed []PreHook
var wasCreated, wasOpened, wasSeen, wasClosed, wasDestroyed []PostHook
var live []LiveHook
for _, h := range hooks {
if hook, ok := h.(RegistryHookPreCreate); ok {
created = append(created, hook.Created)
}
if hook, ok := h.(RegistryHookPostCreate); ok {
wasCreated = append(wasCreated, hook.WasCreated)
}
if hook, ok := h.(RegistryHookPreOpen); ok {
opened = append(opened, hook.Opened)
}
if hook, ok := h.(RegistryHookPostOpen); ok {
wasOpened = append(wasOpened, hook.WasOpened)
}
if hook, ok := h.(RegistryHookPreSee); ok {
seen = append(seen, hook.Seen)
}
if hook, ok := h.(RegistryHookPostSee); ok {
wasSeen = append(wasSeen, hook.WasSeen)
}
if hook, ok := h.(RegistryHookPreClose); ok {
closed = append(closed, hook.Closed)
}
if hook, ok := h.(RegistryHookPostClose); ok {
wasClosed = append(wasClosed, hook.WasClosed)
}
if hook, ok := h.(RegistryHookPreDestroy); ok {
destroyed = append(destroyed, hook.Destroyed)
}
if hook, ok := h.(RegistryHookPostDestroy); ok {
wasDestroyed = append(wasDestroyed, hook.WasDestroyed)
}
if hook, ok := h.(RegistryHookLive); ok {
live = append(live, hook.Live)
}
}
return &PluggableRegistry{
implCreated: composePreHooks(created...),
implWasCreated: composePostHooks(wasCreated...),
implOpened: composePreHooks(opened...),
implWasOpened: composePostHooks(wasOpened...),
implSeen: composePreHooks(seen...),
implWasSeen: composePostHooks(wasSeen...),
implClosed: composePreHooks(closed...),
implWasClosed: composePostHooks(wasClosed...),
implDestroyed: composePreHooks(destroyed...),
implWasDestroyed: composePostHooks(wasDestroyed...),
implLive: composeLiveHooks(live...),
}
}