-
Notifications
You must be signed in to change notification settings - Fork 10
/
subscription.go
207 lines (187 loc) · 4.87 KB
/
subscription.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
package nats
import (
"context"
"io"
"sync"
hash "github.com/aperturerobotics/bifrost/hash"
"github.com/aperturerobotics/bifrost/peer"
"github.com/aperturerobotics/bifrost/pubsub"
"github.com/aperturerobotics/bifrost/pubsub/util/pubmessage"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/nats-io/nats.go"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
// subscription implements the pubsub subscription handle.
type subscription struct {
// ctx is the context for the subscription + publishing
ctx context.Context
// ctxCancel is the context cancel handle
ctxCancel context.CancelFunc
// ncRel releases the nats client reference
ncRel func()
// relOnce guards release
relOnce sync.Once
// m is the nats instance
m *Nats
// client is the nats client
client *natsClient
// channelID is the channel identifier
channelID string
// sub is the nats subscription handle
sub *nats.Subscription
// privKey is the private key used for the sub
privKey crypto.PrivKey
// mtx guards below
mtx sync.Mutex
// handlers are the active handlers
handlers map[*subscriptionHandler]struct{}
}
// newSubscription constructs a new subscription handle.
func newSubscription(
ctx context.Context,
m *Nats, c *natsClient, ncRel func(),
sub *nats.Subscription,
privKey crypto.PrivKey,
channelID string,
) *subscription {
ssub := &subscription{
client: c,
m: m,
sub: sub,
channelID: channelID,
ncRel: ncRel,
privKey: privKey,
}
ssub.ctx, ssub.ctxCancel = context.WithCancel(ctx)
go ssub.executeSubscription()
return ssub
}
// GetPeerId returns the peer ID for this subscription derived from private key.
func (s *subscription) GetPeerId() peer.ID {
return s.client.npeerID
}
// GetChannelId returns the channel id.
func (s *subscription) GetChannelId() string {
return s.channelID
}
// Publish writes to the channel.
func (s *subscription) Publish(data []byte) error {
pht := s.m.conf.GetPublishHashType()
if pht == hash.HashType_HashType_UNKNOWN {
pht = hash.HashType_HashType_SHA256
}
channelID := s.channelID
privKey := s.privKey
msg, _, err := pubmessage.NewPubMessage(channelID, privKey, pht, data)
if err != nil {
return err
}
enc, err := msg.MarshalVT()
if err != nil {
return err
}
return s.PublishRaw(enc)
}
// PublishRaw publishes pre-encoded data (should be a PubMessage).
func (s *subscription) PublishRaw(data []byte) error {
return s.client.Publish(s.channelID, data)
}
// AddHandler adds a callback that is called with each received message.
// The callback should not block.
// Returns a remove function.
// The handler(s) are also removed when the subscription is released.
func (s *subscription) AddHandler(cb func(m pubsub.Message)) func() {
sh := &subscriptionHandler{cb: cb}
s.mtx.Lock()
if s.handlers == nil {
s.handlers = make(map[*subscriptionHandler]struct{})
}
s.handlers[sh] = struct{}{}
s.mtx.Unlock()
relOnce := sync.Once{}
return func() {
relOnce.Do(func() {
s.mtx.Lock()
if s.handlers != nil {
delete(s.handlers, sh)
}
s.mtx.Unlock()
})
}
}
// Release releases the handle.
func (s *subscription) Release() {
s.relOnce.Do(func() {
_ = s.sub.Unsubscribe()
if s.ncRel != nil {
s.ncRel()
}
s.ctxCancel()
})
s.mtx.Lock()
if s.handlers != nil {
for h := range s.handlers {
delete(s.handlers, h)
}
s.handlers = nil
}
s.mtx.Unlock()
}
// logger attaches additional logging fields.
func (s *subscription) logger() *logrus.Entry {
npp := s.client.npeerIDString
return s.m.le.
WithField("channel-id", s.channelID).
WithField("peer-id", npp)
}
// executeSubscription manages the subscription.
func (s *subscription) executeSubscription() {
ctx := s.ctx
defer s.Release()
for {
msg, err := s.sub.NextMsgWithContext(ctx)
if err != nil {
if err == context.Canceled {
return
}
s.logger().WithError(err).Warn("error processing subscription messages: exiting")
return
}
if err := s.handleNatsMessage(msg); err != nil {
if err != io.EOF && err != context.Canceled {
s.logger().WithError(err).Warnf("rx invalid nats message: %s", string(msg.Data))
}
}
}
}
// handleNatsMessage handles an incoming nats.Msg
func (s *subscription) handleNatsMessage(msg *nats.Msg) error {
// decode nats.msg to SignedMsg.
pm, err := peer.UnmarshalSignedMsg(msg.Data)
if err != nil {
return errors.Wrap(err, "unmarshal nats message data")
}
inner, _, peerID, err := pubmessage.ExtractAndVerify(pm)
if err != nil {
return err
}
s.handlePubMessages([]*pubmessage.Message{
pubmessage.NewMessage(peerID, inner),
})
return nil
}
// handlePubMessages handles an incoming nats.Msg
func (s *subscription) handlePubMessages(msgs []*pubmessage.Message) {
s.mtx.Lock()
if s.handlers != nil {
for _, msg := range msgs {
for handler := range s.handlers {
handler.cb(msg)
}
}
}
s.mtx.Unlock()
}
// _ is a type assertion
var _ pubsub.Subscription = ((*subscription)(nil))