-
Notifications
You must be signed in to change notification settings - Fork 70
/
sender.go
414 lines (340 loc) · 11.2 KB
/
sender.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package courier
import (
"context"
"errors"
"fmt"
"log/slog"
"time"
"github.com/nyaruka/courier/utils/clogs"
"github.com/nyaruka/gocommon/analytics"
"github.com/nyaruka/gocommon/urns"
)
type SendResult struct {
externalIDs []string
newURN urns.URN
}
func (r *SendResult) AddExternalID(id string) {
r.externalIDs = append(r.externalIDs, id)
}
func (r *SendResult) ExternalIDs() []string {
return r.externalIDs
}
func (r *SendResult) SetNewURN(u urns.URN) {
r.newURN = u
}
func (r *SendResult) GetNewURN() urns.URN {
return r.newURN
}
type SendError struct {
msg string
retryable bool
loggable bool
clogCode string
clogMsg string
clogExtCode string
}
func (e *SendError) Error() string {
return e.msg
}
// ErrChannelConfig should be returned by a handler send method when channel config is invalid
var ErrChannelConfig error = &SendError{
msg: "channel config invalid",
retryable: false,
loggable: true,
clogCode: "channel_config",
clogMsg: "Channel configuration is missing required values.",
}
// ErrMessageInvalid should be returned by a handler send method when the message it has received is invalid
var ErrMessageInvalid error = &SendError{
msg: "message invalid",
retryable: false,
loggable: true,
clogCode: "message_invalid",
clogMsg: "Message is missing required values.",
}
// ErrConnectionFailed should be returned when connection to the channel fails (timeout or 5XX response)
var ErrConnectionFailed error = &SendError{
msg: "channel connection failed",
retryable: true,
loggable: false,
clogCode: "connection_failed",
clogMsg: "Connection to server failed.",
}
// ErrConnectionThrottled should be returned when channel tells us we're rate limited
var ErrConnectionThrottled error = &SendError{
msg: "channel rate limited",
retryable: true,
loggable: false,
clogCode: "connection_throttled",
clogMsg: "Connection to server has been rate limited.",
}
// ErrResponseStatus should be returned when channel the response has a non-success status code
var ErrResponseStatus error = &SendError{
msg: "response status code",
retryable: false,
loggable: false,
clogCode: "response_status",
clogMsg: "Response has non-success status code.",
}
// ErrResponseUnparseable should be returned when channel response can't be parsed in expected format
var ErrResponseUnparseable error = &SendError{
msg: "response couldn't be parsed",
retryable: false,
loggable: true,
clogCode: "response_unparseable",
clogMsg: "Response could not be parsed in the expected format.",
}
// ErrResponseUnexpected should be returned when channel response doesn't match what we expect
var ErrResponseUnexpected error = &SendError{
msg: "response not expected values",
retryable: false,
loggable: true,
clogCode: "response_unexpected",
clogMsg: "Response doesn't match expected values.",
}
// ErrContactStopped should be returned when channel tells us explicitly that the contact has opted-out
var ErrContactStopped error = &SendError{
msg: "contact opted out",
retryable: false,
loggable: false,
clogCode: "contact_stopped",
clogMsg: "Contact has opted-out of messages from this channel.",
}
func ErrFailedWithReason(code, desc string) *SendError {
return &SendError{
msg: "channel rejected send with reason",
retryable: false,
loggable: false,
clogCode: "rejected_with_reason",
clogMsg: desc,
clogExtCode: code,
}
}
// Foreman takes care of managing our set of sending workers and assigns msgs for each to send
type Foreman struct {
server Server
senders []*Sender
availableSenders chan *Sender
quit chan bool
}
// NewForeman creates a new Foreman for the passed in server with the number of max senders
func NewForeman(server Server, maxSenders int) *Foreman {
foreman := &Foreman{
server: server,
senders: make([]*Sender, maxSenders),
availableSenders: make(chan *Sender, maxSenders),
quit: make(chan bool),
}
for i := 0; i < maxSenders; i++ {
foreman.senders[i] = NewSender(foreman, i)
}
return foreman
}
// Start starts the foreman and all its senders, assigning jobs while there are some
func (f *Foreman) Start() {
for _, sender := range f.senders {
sender.Start()
}
go f.Assign()
}
// Stop stops the foreman and all its senders, the wait group of the server can be used to track progress
func (f *Foreman) Stop() {
for _, sender := range f.senders {
sender.Stop()
}
close(f.quit)
slog.Info("foreman stopping", "comp", "foreman", "state", "stopping")
}
// Assign is our main loop for the Foreman, it takes care of popping the next outgoing messages from our
// backend and assigning them to workers
func (f *Foreman) Assign() {
f.server.WaitGroup().Add(1)
defer f.server.WaitGroup().Done()
log := slog.With("comp", "foreman")
log.Info("senders started and waiting",
"state", "started",
"senders", len(f.senders))
backend := f.server.Backend()
lastSleep := false
for {
select {
// return if we have been told to stop
case <-f.quit:
log.Info("foreman stopped", "state", "stopped")
return
// otherwise, grab the next msg and assign it to a sender
case sender := <-f.availableSenders:
// see if we have a message to work on
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
msg, err := backend.PopNextOutgoingMsg(ctx)
cancel()
if err == nil && msg != nil {
// if so, assign it to our sender
sender.job <- msg
lastSleep = false
} else {
// we received an error getting the next message, log it
if err != nil {
log.Error("error popping outgoing msg", "error", err)
}
// add our sender back to our queue and sleep a bit
if !lastSleep {
log.Debug("sleeping, no messages")
lastSleep = true
}
f.availableSenders <- sender
time.Sleep(250 * time.Millisecond)
}
}
}
}
// Sender is our type for a single goroutine that is sending messages
type Sender struct {
id int
foreman *Foreman
job chan MsgOut
}
// NewSender creates a new sender responsible for sending messages
func NewSender(foreman *Foreman, id int) *Sender {
sender := &Sender{
id: id,
foreman: foreman,
job: make(chan MsgOut, 1),
}
return sender
}
// Start starts our Sender's goroutine and has it start waiting for tasks from the foreman
func (w *Sender) Start() {
w.foreman.server.WaitGroup().Add(1)
go func() {
defer w.foreman.server.WaitGroup().Done()
slog.Debug("started", "comp", "sender", "sender_id", w.id)
for {
// list ourselves as available for work
w.foreman.availableSenders <- w
// grab our next piece of work
msg := <-w.job
// exit if we were stopped
if msg == nil {
slog.Debug("stopped")
return
}
w.sendMessage(msg)
}
}()
}
// Stop stops our senders, callers can use the server's wait group to track progress
func (w *Sender) Stop() {
close(w.job)
}
func (w *Sender) sendMessage(msg MsgOut) {
log := slog.With("comp", "sender", "sender_id", w.id, "channel_uuid", msg.Channel().UUID())
server := w.foreman.server
backend := server.Backend()
// we don't want any individual send taking more than 35s
sendCTX, cancel := context.WithTimeout(context.Background(), time.Second*35)
defer cancel()
log = log.With("msg_id", msg.ID(), "msg_text", msg.Text(), "msg_urn", msg.URN().Identity())
if len(msg.Attachments()) > 0 {
log = log.With("attachments", msg.Attachments())
}
if len(msg.QuickReplies()) > 0 {
log = log.With("quick_replies", msg.QuickReplies())
}
start := time.Now()
// if this is a resend, clear our sent status
if msg.IsResend() {
err := backend.ClearMsgSent(sendCTX, msg.ID())
if err != nil {
log.Error("error clearing sent status for msg", "error", err)
}
}
// was this msg already sent? (from a double queue?)
sent, err := backend.WasMsgSent(sendCTX, msg.ID())
// failing on a lookup isn't a halting problem but we should log it
if err != nil {
log.Error("error looking up msg was sent", "error", err)
}
var status StatusUpdate
var redactValues []string
handler := server.GetHandler(msg.Channel())
if handler != nil {
redactValues = handler.RedactValues(msg.Channel())
}
clog := NewChannelLogForSend(msg, redactValues)
if handler == nil {
// if there's no handler, create a FAILED status for it
status = backend.NewStatusUpdate(msg.Channel(), msg.ID(), MsgStatusFailed, clog)
log.Error(fmt.Sprintf("unable to find handler for channel type: %s", msg.Channel().ChannelType()))
} else if sent {
// if this message was already sent, create a WIRED status for it
status = backend.NewStatusUpdate(msg.Channel(), msg.ID(), MsgStatusWired, clog)
log.Warn("duplicate send, marking as wired")
} else {
status = w.sendByHandler(sendCTX, handler, msg, clog, log)
duration := time.Since(start)
secondDuration := float64(duration) / float64(time.Second)
log.Debug("send complete", "status", status.Status(), "elapsed", duration)
// report to librato
if status.Status() == MsgStatusErrored || status.Status() == MsgStatusFailed {
analytics.Gauge(fmt.Sprintf("courier.msg_send_error_%s", msg.Channel().ChannelType()), secondDuration)
} else {
analytics.Gauge(fmt.Sprintf("courier.msg_send_%s", msg.Channel().ChannelType()), secondDuration)
}
}
// we allot 10 seconds to write our status to the db
writeCTX, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
err = backend.WriteStatusUpdate(writeCTX, status)
if err != nil {
log.Info("error writing msg status", "error", err)
}
clog.End()
// write our logs as well
err = backend.WriteChannelLog(writeCTX, clog)
if err != nil {
log.Info("error writing msg logs", "error", err)
}
// mark our send task as complete
backend.MarkOutgoingMsgComplete(writeCTX, msg, status)
}
func (w *Sender) sendByHandler(ctx context.Context, h ChannelHandler, m MsgOut, clog *ChannelLog, log *slog.Logger) StatusUpdate {
backend := w.foreman.server.Backend()
res := &SendResult{newURN: urns.NilURN}
err := h.Send(ctx, m, res, clog)
status := backend.NewStatusUpdate(m.Channel(), m.ID(), MsgStatusWired, clog)
// fow now we can only store one external id per message
if len(res.ExternalIDs()) > 0 {
status.SetExternalID(res.ExternalIDs()[0])
}
if res.newURN != urns.NilURN {
urnErr := status.SetURNUpdate(m.URN(), res.newURN)
if urnErr != nil {
clog.RawError(urnErr)
}
}
var serr *SendError
if errors.As(err, &serr) {
if serr.loggable {
log.Error("error sending message", "error", err)
}
if serr.retryable {
status.SetStatus(MsgStatusErrored)
} else {
status.SetStatus(MsgStatusFailed)
}
clog.Error(clogs.NewLogError(serr.clogCode, serr.clogExtCode, serr.clogMsg))
// if handler returned ErrContactStopped need to write a stop event
if serr == ErrContactStopped {
channelEvent := backend.NewChannelEvent(m.Channel(), EventTypeStopContact, m.URN(), clog)
if err = backend.WriteChannelEvent(ctx, channelEvent, clog); err != nil {
log.Error("error writing stop event", "error", err)
}
}
} else if err != nil {
log.Error("error sending message", "error", err)
status.SetStatus(MsgStatusErrored)
clog.Error(clogs.NewLogError("internal_error", "", "An internal error occured."))
}
return status
}