-
Notifications
You must be signed in to change notification settings - Fork 255
/
pubsub.go
165 lines (138 loc) · 3.28 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
package net
import (
"context"
"fmt"
host "github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer"
"sync"
pubsub "github.com/libp2p/go-libp2p-pubsub"
)
// 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 {
ps *pubsub.PubSub
topics TopicMap
topicMutex sync.RWMutex
ctx context.Context
}
// SubMsg sub message
type SubMsg *pubsub.Message
// SubCallBack 订阅消息回调函数
type SubCallBack func(topic string, msg SubMsg)
// NewPubSub new pub sub
func NewPubSub(ctx context.Context, host host.Host, opts ...pubsub.Option) (*PubSub, error) {
p := &PubSub{
ps: nil,
topics: make(TopicMap),
}
//选择使用GossipSub
ps, err := pubsub.NewGossipSub(ctx, host, opts...)
if err != nil {
return nil, err
}
p.ps = ps
p.ctx = ctx
p.topics = make(TopicMap)
return p, nil
}
// GetTopics get topics
func (p *PubSub) GetTopics() []string {
return p.ps.GetTopics()
}
// HasTopic check topic exist
func (p *PubSub) HasTopic(topic string) bool {
p.topicMutex.RLock()
defer p.topicMutex.RUnlock()
_, ok := p.topics[topic]
return ok
}
// JoinAndSubTopic 加入topic&subTopic
func (p *PubSub) JoinAndSubTopic(topic string, callback SubCallBack, opts ...pubsub.TopicOpt) error {
Topic, err := p.ps.Join(topic, opts...)
if err != nil {
return err
}
subscription, err := Topic.Subscribe()
if err != nil {
return err
}
//p.topics = append(p.topics, Topic)
ctx, cancel := context.WithCancel(p.ctx)
p.topicMutex.Lock()
p.topics[topic] = &topicinfo{
pubtopic: Topic,
ctx: ctx,
topic: topic,
cancel: cancel,
sub: subscription,
}
p.topicMutex.Unlock()
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
}
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 {
log.Info("RemoveTopic", "topic", topic)
info.cancel()
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.ps.GetTopics())
}