-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy pathpubsub.go
88 lines (78 loc) · 1.89 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package pubsub
import (
"sync"
"time"
)
/*
将消息发送给主题
设计思想:
publisher结构体中包含属性为subscribers的map结构,该map key为channel, 通过Publish方法,将消息发送到channel中
*/
type (
subscriber chan interface{}
topicFunc func(v interface{}) bool
)
type Publisher struct {
m sync.RWMutex //读写锁
buffer int //订阅队列的大小
timeout time.Duration //发布超时时间
subscribers map[subscriber]topicFunc //订阅者信息
}
//构建发布者对象,设置订阅队列的大小和超时时间
func NewPublisher(buf int, t time.Duration) *Publisher {
return &Publisher{
buffer: buf,
timeout: t,
subscribers: make(map[subscriber]topicFunc),
}
}
//添加一个订阅者,订阅所有的主题
func (p *Publisher) Subscribe() subscriber {
return p.SubscribeTopic(nil)
}
//添加一个订阅者,订阅指定的主题
func (p *Publisher) SubscribeTopic(topic topicFunc) subscriber {
ch := make(subscriber, p.buffer)
p.m.Lock()
defer p.m.Unlock()
p.subscribers[ch] = topic
return ch
}
//退出订阅, 同时关闭chan
func (p *Publisher) Exit(sub subscriber) {
p.m.Lock()
defer p.m.Unlock()
delete(p.subscribers, sub)
close(sub)
}
//关闭发布者,同时关闭所有订阅者通道
func (p *Publisher) Close() {
p.m.Lock()
defer p.m.Unlock()
for sub := range p.subscribers {
delete(p.subscribers, sub)
close(sub)
}
}
//发布主题
func (p *Publisher) Publish(v interface{}) {
p.m.Lock()
defer p.m.Unlock()
var wg sync.WaitGroup
for sub, topic := range p.subscribers {
wg.Add(1)
go p.sendTopic(sub, topic, v, &wg)
}
wg.Wait()
}
//发送主题, 使用select语句处理channel, 允许超时
func (p *Publisher) sendTopic(sub subscriber, topic topicFunc, v interface{}, wg *sync.WaitGroup) {
defer wg.Done()
if topic != nil && !topic(v) {
return
}
select {
case sub<-v:
case <-time.After(p.timeout):
}
}