-
Notifications
You must be signed in to change notification settings - Fork 0
/
pubsubby.go
124 lines (103 loc) · 2.82 KB
/
pubsubby.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
package pubsubby
import (
"sync"
"github.com/cheekybits/genny/generic"
"github.com/itsmontoya/pubsubby/utilities"
)
// New will return a new instance of Pubsubby
func New() *Pubsubby {
var p Pubsubby
p.psm = make(map[Key]*pubsub)
return &p
}
// Pubsubby manages a set of pubsub's
type Pubsubby struct {
mux sync.RWMutex
psm map[Key]*pubsub
}
// get will attempt to get a pubsub for a given key
// Note: This function is thread-safe, locking does not need to be handled elsewhere
func (p *Pubsubby) get(key Key) (ps *pubsub, ok bool) {
p.mux.RLock()
ps, ok = p.psm[key]
p.mux.RUnlock()
return
}
// create will create a pubsub for a given key if the pubsub does not yet exist
// Note: This function is thread-safe, locking does not need to be handled elsewhere
func (p *Pubsubby) create(key Key) (ps *pubsub) {
var ok bool
// Attempt to get the value first, this will allow us to avoid a write-lock if the value exists
if ps, ok = p.get(key); ok {
// Fast-track successful, return early
return
}
p.mux.Lock()
// Check if the value still does not exist (in case it was created before new lock was acquired)
if ps, ok = p.psm[key]; !ok {
ps = &pubsub{}
p.psm[key] = ps
}
p.mux.Unlock()
return
}
// Subscribe will add a subscriber to the functions list for a matching pubsub key
func (p *Pubsubby) Subscribe(key Key, fn SubFn) {
ps := p.create(key)
ps.Subscribe(fn)
}
// Publish will publish a value to the subscribers for a matching pubsub key
func (p *Pubsubby) Publish(key Key, val Value) {
ps := p.create(key)
all := p.create("*")
ps.Publish(key, val)
all.Publish(key, val)
}
// pubsub is a pubsub item
type pubsub struct {
mux sync.RWMutex
fns []SubFn
}
func (p *pubsub) pop(i int) {
p.fns = append(p.fns[:i], p.fns[i+1:]...)
}
// Subscribe will add a subscriber to the functions list
func (p *pubsub) Subscribe(fn SubFn) {
p.mux.Lock()
p.fns = append(p.fns, fn)
p.mux.Unlock()
}
// Publish will publish a value to the subscribers
func (p *pubsub) Publish(key Key, val Value) {
p.mux.Lock()
defer p.mux.Unlock()
// Iterate through all the subscribers
for i, fn := range p.fns {
if fn(key, val) {
// Function's end variable returned as true, pop the function from the subscribers list
p.pop(i)
}
}
}
// Len is used to determine the length of the subscribers
func (p *pubsub) Len() (n int) {
p.mux.RLock()
n = len(p.fns)
p.mux.RUnlock()
return
}
// List is for debugging purposes, will allow to peek at the current subscibers
func (p *pubsub) List() (fis []utilities.FuncInfo) {
p.mux.RLock()
defer p.mux.RUnlock()
for _, fn := range p.fns {
fis = append(fis, utilities.NewFuncInfo(fn))
}
return
}
// SubFn will take a value and return an "end" boolean
type SubFn func(key Key, val Value) (end bool)
// Key is the key type
type Key generic.Type
// Value is the value type
type Value generic.Type