-
Notifications
You must be signed in to change notification settings - Fork 23
/
receiver.go
289 lines (255 loc) · 7.43 KB
/
receiver.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
282
283
284
285
286
287
288
289
package websocket
import (
"github.com/smancke/guble/protocol"
"github.com/smancke/guble/server/router"
"github.com/smancke/guble/server/store"
"errors"
"fmt"
"math"
"strconv"
"strings"
log "github.com/Sirupsen/logrus"
)
var errUnreadMsgsAvailable = errors.New("unread messages available")
// Receiver is a helper class, for managing a combined pull push on a topic.
// It is used for implementation of the + (receive) command in the guble protocol.
type Receiver struct {
cancelC chan bool
sendC chan []byte
applicationID string
router router.Router
messageStore store.MessageStore
path protocol.Path
doFetch bool
doSubscription bool
startID int64
maxCount int
lastSentID uint64
shouldStop bool
route *router.Route
enableNotifications bool
userID string
}
// NewReceiverFromCmd parses the info in the command
func NewReceiverFromCmd(
applicationID string,
cmd *protocol.Cmd,
sendChannel chan []byte,
router router.Router,
userID string) (rec *Receiver, err error) {
messageStore, err := router.MessageStore()
if err != nil {
return nil, err
}
rec = &Receiver{
applicationID: applicationID,
sendC: sendChannel,
router: router,
messageStore: messageStore,
cancelC: make(chan bool, 1),
enableNotifications: true,
userID: userID,
}
if len(cmd.Arg) == 0 || cmd.Arg[0] != '/' {
return nil, fmt.Errorf("command requires at least a path argument, but non given")
}
args := strings.SplitN(cmd.Arg, " ", 3)
rec.path = protocol.Path(args[0])
if len(args) > 1 {
rec.doFetch = true
rec.startID, err = strconv.ParseInt(args[1], 10, 64)
if err != nil {
return nil, fmt.Errorf("startid has to be empty or int, but was %q: %v", args[1], err)
}
}
rec.doSubscription = true
if len(args) > 2 {
rec.doSubscription = false
rec.maxCount, err = strconv.Atoi(args[2])
if err != nil {
return nil, fmt.Errorf("maxCount has to be empty or int, but was %q: %v", args[1], err)
}
}
return rec, nil
}
// Start starts the receiver loop
func (rec *Receiver) Start() error {
rec.shouldStop = false
if rec.doFetch && !rec.doSubscription {
go rec.fetchOnlyLoop()
} else {
go rec.subscriptionLoop()
}
return nil
}
func (rec *Receiver) subscriptionLoop() {
for !rec.shouldStop {
if rec.doFetch {
if err := rec.fetch(); err != nil {
logger.WithError(err).WithField("rec", rec).Error("Error while fetching subscription")
rec.sendError(protocol.ERROR_INTERNAL_SERVER, err.Error())
return
}
if err := rec.messageStore.DoInTx(rec.path.Partition(), rec.subscribeIfNoUnreadMessagesAvailable); err != nil {
if err == errUnreadMsgsAvailable {
logger.WithFields(log.Fields{
"lastSentId": rec.lastSentID,
"receiver": rec,
}).Error("errUnreadMsgsAvailable")
rec.startID = int64(rec.lastSentID) + 1
continue // fetch again
} else {
logger.WithError(err).WithField("recStartId", rec.startID).
Error("Error while subscribeIfNoUnreadMessagesAvailable")
rec.sendError(protocol.ERROR_INTERNAL_SERVER, err.Error())
return
}
}
} else {
rec.subscribe()
}
rec.receiveFromSubscription()
if !rec.shouldStop {
//fmt.Printf(" router closed .. on msg: %v\n", rec.lastSendId)
// the router kicked us out, because we are too slow for realtime listening,
// so we setup parameters for fetching and closing the gap. Than we can subscribe again.
rec.startID = int64(rec.lastSentID) + 1
rec.doFetch = true
}
}
}
func (rec *Receiver) subscribeIfNoUnreadMessagesAvailable(maxMessageID uint64) error {
if maxMessageID > rec.lastSentID {
return errUnreadMsgsAvailable
}
rec.subscribe()
return nil
}
func (rec *Receiver) subscribe() {
rec.route = router.NewRoute(
router.RouteConfig{
RouteParams: router.RouteParams{"application_id": rec.applicationID, "user_id": rec.userID},
Path: rec.path,
ChannelSize: 10,
},
)
_, err := rec.router.Subscribe(rec.route)
if err != nil {
rec.sendError(protocol.ERROR_SUBSCRIBED_TO, string(rec.path), err.Error())
} else {
rec.sendOK(protocol.SUCCESS_SUBSCRIBED_TO, string(rec.path))
}
}
func (rec *Receiver) receiveFromSubscription() {
for {
select {
case m, ok := <-rec.route.MessagesChannel():
if !ok {
logger.WithFields(log.Fields{
"applicationId": rec.applicationID,
}).Debug("Router closed the channel returning from subscription for")
return
}
logger.WithFields(log.Fields{
"applicationId": rec.applicationID,
"messageMetadata": m.Metadata(),
}).Debug("Delivering message")
if m.ID > rec.lastSentID {
rec.lastSentID = m.ID
rec.sendC <- m.Bytes()
} else {
logger.WithFields(log.Fields{
"msgId": m.ID,
}).Debug("Message already sent to client. Dropping message.")
}
case <-rec.cancelC:
rec.shouldStop = true
rec.router.Unsubscribe(rec.route)
rec.route = nil
rec.sendOK(protocol.SUCCESS_CANCELED, string(rec.path))
return
}
}
}
func (rec *Receiver) fetchOnlyLoop() {
err := rec.fetch()
if err != nil {
logger.WithError(err).WithField("rec", rec).Error("Error while fetching")
rec.sendError(protocol.ERROR_INTERNAL_SERVER, err.Error())
}
}
func (rec *Receiver) fetch() error {
fetch := &store.FetchRequest{
Partition: rec.path.Partition(),
MessageC: make(chan *store.FetchedMessage, 10), //TODO MAKE more tests when the receiver will be refactored after the route params is integrated.Initial capacity was 3
ErrorC: make(chan error),
StartC: make(chan int),
Count: rec.maxCount,
}
if rec.startID >= 0 {
fetch.Direction = 1
fetch.StartID = uint64(rec.startID)
if rec.maxCount == 0 {
fetch.Count = math.MaxInt32
}
} else {
fetch.Direction = -1
maxID, err := rec.messageStore.MaxMessageID(rec.path.Partition())
if err != nil {
return err
}
fetch.StartID = maxID
if rec.maxCount == 0 {
fetch.Count = -1 * int(rec.startID)
}
}
rec.messageStore.Fetch(fetch)
for {
select {
case numberOfResults := <-fetch.StartC:
rec.sendOK(protocol.SUCCESS_FETCH_START, fmt.Sprintf("%v %v", rec.path, numberOfResults))
case msgAndID, open := <-fetch.MessageC:
if !open {
rec.sendOK(protocol.SUCCESS_FETCH_END, string(rec.path))
return nil
}
logger.WithFields(log.Fields{
"msgId": msgAndID.ID,
"msg": string(msgAndID.Message),
"lastSendId": rec.lastSentID,
}).Info("Reply sent")
rec.lastSentID = msgAndID.ID
rec.sendC <- msgAndID.Message
case err := <-fetch.ErrorC:
return err
case <-rec.cancelC:
rec.shouldStop = true
rec.sendOK(protocol.SUCCESS_CANCELED, string(rec.path))
// TODO implement cancellation in message store
return nil
}
}
}
// Stop stops/cancels the receiver
func (rec *Receiver) Stop() error {
rec.cancelC <- true
return nil
}
func (rec *Receiver) sendError(name string, argPattern string, params ...interface{}) {
notificationMessage := &protocol.NotificationMessage{
Name: name,
Arg: fmt.Sprintf(argPattern, params...),
IsError: true,
}
rec.sendC <- notificationMessage.Bytes()
}
func (rec *Receiver) sendOK(name string, argPattern string, params ...interface{}) {
if rec.enableNotifications {
notificationMessage := &protocol.NotificationMessage{
Name: name,
Arg: fmt.Sprintf(argPattern, params...),
IsError: false,
}
rec.sendC <- notificationMessage.Bytes()
}
}