-
Notifications
You must be signed in to change notification settings - Fork 12
/
client_sendq.go
236 lines (205 loc) · 6.11 KB
/
client_sendq.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
package client
import (
"context"
"errors"
"fmt"
"sync/atomic"
"time"
"github.com/companyzero/bisonrelay/client/clientdb"
"github.com/companyzero/bisonrelay/client/clientintf"
"github.com/companyzero/bisonrelay/rpc"
)
// The DB Send Queue is used to manage outbound messages at the highest level
// of the client code. It stores messages, such that on client restart they
// will continue to be sent to their destinations.
// addToSendQ adds the given message to the DB send queue.
func (c *Client) addToSendQ(typ string, msg interface{}, priority uint,
dests ...clientintf.UserID) (clientdb.SendQID, error) {
var sendqID clientdb.SendQID
// Trick to ease storing this msg payload: compose as a full blobified
// RM.
blob, err := rpc.ComposeCompressedRM(c.localID.signMessage, msg, c.cfg.CompressLevel)
if err != nil {
return sendqID, err
}
estSize := rpc.EstimateRoutedRMWireSize(len(blob))
maxMsgSize := int(c.q.MaxMsgSize())
if estSize > maxMsgSize {
return sendqID, fmt.Errorf("cannot enqueue message %T "+
"estimated as larger than max message size %d > %d: %w",
msg, estSize, maxMsgSize, errRMTooLarge)
}
err = c.dbUpdate(func(tx clientdb.ReadWriteTx) error {
var err error
sendqID, err = c.db.AddToSendQueue(tx, typ, dests, blob, priority)
return err
})
if err == nil {
c.log.Tracef("Added %q with %d dests to sendq %s", typ,
len(dests), sendqID)
}
return sendqID, err
}
// removeFromSendQ marks the message of the given sendq id as sent to the given
// destination. Errors are only logged since this is called when the message
// has already been sent and there's nothing more that can be done in case of
// errors.
func (c *Client) removeFromSendQ(id clientdb.SendQID, dest clientintf.UserID) {
err := c.dbUpdate(func(tx clientdb.ReadWriteTx) error {
return c.db.RemoveFromSendQueue(tx, id, dest)
})
if err != nil {
c.log.Errorf("Unable to remove dest %s from send queue %s: %v",
dest, id, err)
} else {
c.log.Tracef("Removed dest %s from sendq %s", dest, id)
}
}
// sendWithSendQPriority adds the given message to the sendq with the given
// type and sends it to the specified destinations. Each sending is done
// asynchronously, so this returns immediately after enqueing.
//
// If progressChan is specified, updates about the sending progress are sent
// there.
func (c *Client) sendWithSendQPriority(typ string, msg interface{}, priority uint,
progressChan chan SendProgress, dests ...clientintf.UserID) error {
sqid, err := c.addToSendQ(typ, msg, priority, dests...)
if err != nil {
return err
}
var sent, total atomic.Int64
total.Store(int64(len(dests)))
// Send the msg to each destination.
for _, dest := range dests {
uid := dest
ru, err := c.UserByID(uid)
if err != nil {
c.log.Warnf("Unable to find user for sendq entry %s: %v", typ, err)
continue
}
// Helper called when we get an error.
failed := func(err error) {
if !errors.Is(err, clientintf.ErrSubsysExiting) {
ru.log.Errorf("unable to queue %T: %v",
msg, err)
c.removeFromSendQ(sqid, uid)
}
}
// Queue synchronously to ensure outbound order.
replyChan := make(chan error)
err = ru.queueRMPriority(msg, priority, replyChan, typ)
if err != nil {
failed(err)
total.Add(-1)
continue
}
// Wait for reply asynchronously.
go func() {
err := <-replyChan
if err != nil {
failed(err)
} else {
c.removeFromSendQ(sqid, uid)
}
// Alert about progress.
if !errors.Is(err, clientintf.ErrSubsysExiting) && (progressChan != nil) {
vSent := int(sent.Add(1))
progressChan <- SendProgress{
Sent: vSent,
Total: int(total.Load()),
Err: err,
}
}
}()
}
return nil
}
// sendWithSendQ sends a msg using the send queue with default priority.
func (c *Client) sendWithSendQ(typ string, msg interface{}, dests ...clientintf.UserID) error {
return c.sendWithSendQPriority(typ, msg, priorityDefault, nil, dests...)
}
// runSendQ sends outstanding msgs from the DB send queue.
func (c *Client) runSendQ(ctx context.Context) error {
<-c.abLoaded
var sendq []clientdb.SendQueueElement
err := c.db.View(c.dbCtx, func(tx clientdb.ReadTx) error {
var err error
sendq, err = c.db.ListSendQueue(tx)
return err
})
if err != nil {
return err
}
// Flatten the sendq into a single list.
type sendEL struct {
tries int
msg *clientdb.SendQueueElement
uid *clientintf.UserID
}
sendlist := make([]sendEL, 0)
for i := range sendq {
for d := range sendq[i].Dests {
sendlist = append(sendlist, sendEL{
msg: &sendq[i],
uid: &sendq[i].Dests[d],
})
}
}
if len(sendlist) == 0 {
c.log.Debugf("No queued messages to send")
return nil
}
var i int
removeCurrent := func() {
c.removeFromSendQ(sendlist[i].msg.ID, *sendlist[i].uid)
copy(sendlist[i:], sendlist[i+1:])
sendlist = sendlist[:len(sendlist)-1]
}
const maxTries = 5 // Max attempts at sending same msg.
c.log.Infof("Starting to send %d queued messages", len(sendlist))
for len(sendlist) > 0 {
if canceled(ctx) {
return ctx.Err()
}
// Attempt to send the next message.
el := sendlist[i]
ru, err := c.UserByID(*el.uid)
if err != nil {
// User not found, drop it from queue.
c.log.Warnf("Removing queued msg %s due to unknown user %s",
el.msg.Type, el.uid)
removeCurrent()
continue
}
_, rm, err := rpc.DecomposeRM(c.localID.verifyMessage, el.msg.Msg, uint(c.q.MaxMsgSize()))
if err != nil {
c.log.Warnf("Unable to decompose queued RM %s: %v",
el.msg.Type, err)
removeCurrent()
continue
}
err = ru.sendRMPriority(rm, el.msg.Type, el.msg.Priority)
if err != nil {
// Failed to send this. Try next one so we're not stuck.
ru.log.Errorf("Unable to send RM from sendq: %v", err)
sendlist[i].tries += 1
if sendlist[i].tries >= maxTries {
removeCurrent()
} else {
i = (i + 1) % len(sendlist)
}
select {
case <-time.After(time.Second):
case <-ctx.Done():
return ctx.Err()
}
} else {
// Successfully sent this one.
ru.log.Debugf("Sent sendq element %s. %d remaining",
sendq[i].Type, len(sendlist)-1)
removeCurrent()
}
}
c.log.Infof("Finished sending queued messages")
return nil
}