-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpubsub.go
72 lines (61 loc) · 1.21 KB
/
pubsub.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
package algorithm
import (
"sync"
"time"
)
// Subcrib 订阅者
type Subcrib chan interface{}
type filterFunc func(v interface{}) bool
// Publisher 发布者
type Publisher struct {
mu sync.RWMutex
subs map[Subcrib]filterFunc
cacheSize int
timeout time.Duration
}
// NewPublisher 新建发布者
func NewPublisher(size int, timeout time.Duration) *Publisher {
pub := &Publisher{
subs: make(map[Subcrib]filterFunc),
cacheSize: size,
timeout: timeout,
}
return pub
}
// Subcrib 订阅
func (pub *Publisher) Subcrib(filter filterFunc) Subcrib {
pub.mu.Lock()
defer pub.mu.Unlock()
sub := make(Subcrib, pub.cacheSize)
pub.subs[sub] = filter
return sub
}
// Publish 发布消息
func (pub *Publisher) Publish(v interface{}) {
pub.mu.Lock()
defer pub.mu.Unlock()
var wg sync.WaitGroup
for sub, filter := range pub.subs {
if filter != nil && !filter(v) {
continue
}
wg.Add(1)
go func(s Subcrib) {
defer wg.Done()
select {
case <-time.Tick(pub.timeout):
case s <- v:
}
}(sub)
}
wg.Wait()
}
// Close 关闭发布者
func (pub *Publisher) Close() {
pub.mu.Lock()
defer pub.mu.Unlock()
for sub := range pub.subs {
delete(pub.subs, sub)
close(sub)
}
}