-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregistry.go
More file actions
67 lines (57 loc) · 1.36 KB
/
Copy pathregistry.go
File metadata and controls
67 lines (57 loc) · 1.36 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
package chanprobe
import (
"sort"
"sync"
)
// Registry stores named queues and returns snapshots for all registered queues.
type Registry struct {
mu sync.RWMutex
queues map[string]Snapshoter
}
var defaultRegistry = NewRegistry()
// NewRegistry returns an empty queue registry.
func NewRegistry() *Registry {
return &Registry{
queues: make(map[string]Snapshoter),
}
}
// DefaultRegistry returns the process-wide registry used by New by default.
func DefaultRegistry() *Registry {
return defaultRegistry
}
// Register stores a named snapshot provider. Invalid inputs are ignored.
func (r *Registry) Register(name string, q Snapshoter) {
if r == nil || name == "" || q == nil {
return
}
r.mu.Lock()
defer r.mu.Unlock()
r.queues[name] = q
}
// Unregister removes a name from the registry.
func (r *Registry) Unregister(name string) {
if r == nil {
return
}
r.mu.Lock()
defer r.mu.Unlock()
delete(r.queues, name)
}
// Snapshots returns snapshots for registered queues sorted by name.
func (r *Registry) Snapshots() []Snapshot {
if r == nil {
return nil
}
r.mu.RLock()
defer r.mu.RUnlock()
names := make([]string, 0, len(r.queues))
for name := range r.queues {
names = append(names, name)
}
sort.Strings(names)
out := make([]Snapshot, 0, len(names))
for _, name := range names {
out = append(out, r.queues[name].Snapshot())
}
return out
}