-
Notifications
You must be signed in to change notification settings - Fork 255
/
pubsub.go
281 lines (235 loc) · 6.43 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
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
package extension
import (
"context"
"fmt"
"sync"
"time"
"github.com/33cn/chain33/types"
"github.com/33cn/chain33/common/log/log15"
p2ptypes "github.com/33cn/chain33/system/p2p/dht/types"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer"
pubsub "github.com/libp2p/go-libp2p-pubsub"
)
var log = log15.New("module", "pubsub")
var setOnce sync.Once
// TopicMap topic map
type TopicMap map[string]*topicinfo
type topicinfo struct {
pubtopic *pubsub.Topic
sub *pubsub.Subscription
ctx context.Context
cancel context.CancelFunc
topic string
}
// PubSub pub sub
type PubSub struct {
*pubsub.PubSub
host host.Host
topics TopicMap
topicMutex sync.RWMutex
ctx context.Context
config *p2ptypes.PubSubConfig
pt *pubsubTracer
}
// SubMsg sub message
type SubMsg *pubsub.Message
// SubCallBack 订阅消息回调函数
type SubCallBack func(topic string, msg SubMsg)
func setPubSubParameters(psConf *p2ptypes.PubSubConfig) {
// mb -> byte
psConf.MaxMsgSize *= 1 << 20
if psConf.MaxMsgSize <= 0 {
psConf.MaxMsgSize = types.MaxBlockSize
}
if psConf.PeerOutboundQueueSize <= 0 {
psConf.PeerOutboundQueueSize = 1024
}
if psConf.SubBufferSize <= 0 {
psConf.SubBufferSize = 1024
}
if psConf.ValidateQueueSize <= 0 {
psConf.ValidateQueueSize = 1024
}
if psConf.GossipSubDlo <= 0 {
psConf.GossipSubDlo = 6
}
if psConf.GossipSubD <= 0 {
psConf.GossipSubD = 8
}
if psConf.GossipSubDhi <= 0 {
psConf.GossipSubDhi = 15
}
if psConf.GossipSubHeartbeatInterval <= 0 {
psConf.GossipSubHeartbeatInterval = 700
}
if psConf.GossipSubHistoryGossip <= 0 {
psConf.GossipSubHistoryGossip = 3
}
if psConf.GossipSubHistoryLength <= 0 {
psConf.GossipSubHistoryLength = 10
}
if psConf.GossipSubDlo > psConf.GossipSubD || psConf.GossipSubD > psConf.GossipSubDhi {
panic("setPubSubParameters, must GossipSubDlo <= GossipSubD <= GossipSubDhi")
}
// libp2p库内部全局变量暂不支持接口访问,强制只设置一次,主要是避免单元测试datarace,不影响常规执行
setOnce.Do(func() {
pubsub.GossipSubDlo = psConf.GossipSubDlo
pubsub.GossipSubD = psConf.GossipSubD
pubsub.GossipSubDhi = psConf.GossipSubDhi
pubsub.GossipSubHeartbeatInterval = time.Duration(psConf.GossipSubHeartbeatInterval) * time.Millisecond
pubsub.GossipSubHistoryGossip = psConf.GossipSubHistoryGossip
pubsub.GossipSubHistoryLength = psConf.GossipSubHistoryLength
})
}
// NewPubSub new pub sub
func NewPubSub(ctx context.Context, host host.Host, psConf *p2ptypes.PubSubConfig) (*PubSub, error) {
p := &PubSub{
topics: make(TopicMap),
pt: newPubsubTracer(ctx, host),
}
setPubSubParameters(psConf)
psOpts := make([]pubsub.Option, 0)
// pubsub消息默认会基于节点私钥进行签名和验签,支持关闭
if psConf.DisablePubSubMsgSign {
psOpts = append(psOpts, pubsub.WithMessageSigning(false), pubsub.WithStrictSignatureVerification(false))
}
psOpts = append(psOpts, pubsub.WithFloodPublish(true),
pubsub.WithPeerOutboundQueueSize(psConf.PeerOutboundQueueSize),
pubsub.WithValidateQueueSize(psConf.ValidateQueueSize),
pubsub.WithPeerExchange(psConf.EnablePeerExchange),
pubsub.WithMaxMessageSize(psConf.MaxMsgSize),
pubsub.WithRawTracer(p.pt))
//选择使用GossipSub
ps, err := pubsub.NewGossipSub(ctx, host, psOpts...)
if err != nil {
return nil, err
}
p.PubSub = ps
p.ctx = ctx
p.host = host
p.topics = make(TopicMap)
p.config = psConf
return p, nil
}
// HasTopic check topic exist
func (p *PubSub) HasTopic(topic string) bool {
p.topicMutex.RLock()
defer p.topicMutex.RUnlock()
_, ok := p.topics[topic]
return ok
}
func (p *PubSub) setTopic(topic string, tpInfo *topicinfo) {
p.topicMutex.Lock()
defer p.topicMutex.Unlock()
p.topics[topic] = tpInfo
}
// TryJoinTopic check exist before join topic
func (p *PubSub) TryJoinTopic(topic string, opts ...pubsub.TopicOpt) error {
if p.HasTopic(topic) {
return nil
}
return p.JoinTopic(topic, opts...)
}
// JoinTopic join topic
func (p *PubSub) JoinTopic(topic string, opts ...pubsub.TopicOpt) error {
tp, err := p.Join(topic, opts...)
if err != nil {
return err
}
ctx, cancel := context.WithCancel(p.ctx)
p.setTopic(topic, &topicinfo{
pubtopic: tp,
ctx: ctx,
topic: topic,
cancel: cancel,
})
return nil
}
// JoinAndSubTopic 加入topic&subTopic
func (p *PubSub) JoinAndSubTopic(topic string, callback SubCallBack, opts ...pubsub.TopicOpt) error {
tp, err := p.Join(topic, opts...)
if err != nil {
return err
}
subscription, err := tp.Subscribe(pubsub.WithBufferSize(p.config.SubBufferSize))
if err != nil {
return err
}
ctx, cancel := context.WithCancel(p.ctx)
p.setTopic(topic, &topicinfo{
pubtopic: tp,
ctx: ctx,
topic: topic,
cancel: cancel,
sub: subscription,
})
p.pt.addSubscriber(topic, callback)
go p.subTopic(ctx, subscription, callback)
return nil
}
// Publish 发布消息
func (p *PubSub) Publish(topic string, msg []byte) error {
p.topicMutex.RLock()
defer p.topicMutex.RUnlock()
t, ok := p.topics[topic]
if !ok {
log.Error("pubsub publish", "no this topic", topic)
return fmt.Errorf("no this topic:%v", topic)
}
err := t.pubtopic.Publish(t.ctx, msg)
if err != nil {
log.Error("pubsub publish", "err", err)
return err
}
return nil
}
func (p *PubSub) subTopic(ctx context.Context, sub *pubsub.Subscription, callback SubCallBack) {
topic := sub.Topic()
for {
got, err := sub.Next(ctx)
if err != nil {
log.Error("SubMsg", "topic", topic, "sub err", err)
p.RemoveTopic(topic)
return
}
// 不接收自己发布的信息, 即不用于内部模块之间的消息通信
if p.host.ID() == got.ReceivedFrom {
continue
}
callback(topic, got)
}
}
// RemoveTopic remove topic
func (p *PubSub) RemoveTopic(topic string) {
p.topicMutex.Lock()
defer p.topicMutex.Unlock()
info, ok := p.topics[topic]
if !ok {
return
}
log.Info("RemoveTopic", "topic", topic)
info.cancel()
if info.sub != nil {
info.sub.Cancel()
}
err := info.pubtopic.Close()
if err != nil {
log.Error("RemoveTopic", "topic", topic, "close topic err", err)
}
delete(p.topics, topic)
}
// FetchTopicPeers fetch peers with topic
func (p *PubSub) FetchTopicPeers(topic string) []peer.ID {
p.topicMutex.RLock()
defer p.topicMutex.RUnlock()
topicobj, ok := p.topics[topic]
if ok {
return topicobj.pubtopic.ListPeers()
}
return nil
}
// TopicNum get topic number
func (p *PubSub) TopicNum() int {
return len(p.GetTopics())
}