/
stream.go
151 lines (138 loc) · 3.51 KB
/
stream.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
package floodsub
import (
"context"
"io"
"github.com/aperturerobotics/bifrost/peer"
"github.com/aperturerobotics/bifrost/pubsub"
"github.com/aperturerobotics/bifrost/pubsub/util/pubmessage"
stream_packet "github.com/aperturerobotics/bifrost/stream/packet"
"github.com/sirupsen/logrus"
)
// streamHandler is a remote floodsub peer with a stream.
type streamHandler struct {
tpl pubsub.PeerLinkTuple
m *FloodSub
initiator bool
stream *stream_packet.Session
le *logrus.Entry
packetCh chan *Packet
peerID peer.ID
ctx context.Context
ctxCancel context.CancelFunc
}
// writePacket writes a packet.
func (s *streamHandler) writePacket(pkt *Packet) {
select {
case s.packetCh <- pkt:
case <-s.ctx.Done():
}
}
// executeSession executes the stream session.
func (s *streamHandler) executeSession() error {
ctx := s.ctx
defer s.stream.Close()
defer s.ctxCancel()
go s.readPump(ctx)
// le := s.le.WithField("initiator", s.initiator)
// le.Info("executing session")
for {
select {
case <-ctx.Done():
return ctx.Err()
case pkt := <-s.packetCh:
if err := s.stream.SendMsg(pkt); err != nil {
return err
}
}
}
}
// processPacket processes the incoming packet.
func (s *streamHandler) processPacket(msg *Packet) {
if subs := msg.GetSubscriptions(); len(subs) != 0 {
s.handleSubscriptions(subs)
}
if pubs := msg.GetPublish(); len(pubs) != 0 {
s.handlePublish(pubs)
}
}
// handlePublish handles incoming published packets
func (s *streamHandler) handlePublish(pkts []*peer.SignedMsg) {
for _, pkt := range pkts {
pktInner, _, _, err := pubmessage.ExtractAndVerify(pkt)
if err != nil {
s.le.WithError(err).Warnf(
"invalid message from peer id: %q",
pkt.GetFromPeerId(),
)
continue
}
chid := pktInner.GetChannel()
s.m.mtx.Lock()
_, chOk := s.m.channels[chid]
s.m.mtx.Unlock()
if !chOk {
s.le.Warnf("received message for non-subscribed channel %s", chid)
continue
}
s.m.handleValidMessage(s.ctx, s.peerID, pkt, pktInner)
}
}
// handleSubscriptions processes subscription packet data
func (s *streamHandler) handleSubscriptions(subs []*SubscriptionOpts) {
s.m.mtx.Lock()
defer s.m.mtx.Unlock()
for _, sub := range subs {
chid := sub.GetChannelId()
if chid == "" {
continue
}
le := s.le.WithField("channel-id", chid)
if sub.GetSubscribe() {
cm, ok := s.m.peerChannels[chid]
if !ok {
cm = make(map[pubsub.PeerLinkTuple]struct{})
s.m.peerChannels[chid] = cm
}
if _, ok := cm[s.tpl]; !ok {
le.
WithField("tpl-peer-id", s.tpl.PeerID.String()).
Debug("peer subscribed to channel")
cm[s.tpl] = struct{}{}
}
} else {
tm, ok := s.m.peerChannels[chid]
if !ok {
continue
}
if _, ok := tm[s.tpl]; ok {
le.Debug("peer unsubscribed from channel")
delete(tm, s.tpl)
}
if len(tm) == 0 {
delete(s.m.peerChannels, chid)
}
}
}
}
// readPump reads messages from the stream.
func (s *streamHandler) readPump(ctx context.Context) {
defer s.ctxCancel()
msg := &Packet{}
for {
if err := s.stream.RecvMsg(msg); err != nil {
if err != io.EOF && err != context.Canceled && err.Error() != "broken pipe" && err.Error() == "NO_ERROR" {
s.le.WithError(err).Warn("error receiving message")
} else {
s.le.Debug("session reader exiting")
}
return
}
s.le.
WithField("subscription-count", len(msg.GetSubscriptions())).
WithField("publish-count", len(msg.GetPublish())).
Debug("received message from peer")
// process message
s.processPacket(msg)
msg.Reset()
}
}