@@ -235,12 +235,16 @@ func (s *session) queueForSend(msg *Message) error {
235
235
236
236
s .toSend = append (s .toSend , msgBytes )
237
237
238
+ s .notifyMessageOut ()
239
+
240
+ return nil
241
+ }
242
+
243
+ func (s * session ) notifyMessageOut () {
238
244
select {
239
245
case s .messageEvent <- true :
240
246
default :
241
247
}
242
-
243
- return nil
244
248
}
245
249
246
250
// send will validate, persist, queue the message. If the session is logged on, send all messages in the queue.
@@ -261,7 +265,7 @@ func (s *session) sendInReplyTo(msg *Message, inReplyTo *Message) error {
261
265
}
262
266
263
267
s .toSend = append (s .toSend , msgBytes )
264
- s .sendQueued ()
268
+ s .sendQueued (true )
265
269
266
270
return nil
267
271
}
@@ -290,7 +294,7 @@ func (s *session) dropAndSendInReplyTo(msg *Message, inReplyTo *Message) error {
290
294
291
295
s .dropQueued ()
292
296
s .toSend = append (s .toSend , msgBytes )
293
- s .sendQueued ()
297
+ s .sendQueued (true )
294
298
295
299
return nil
296
300
}
@@ -346,9 +350,13 @@ func (s *session) persist(seqNum int, msgBytes []byte) error {
346
350
return s .store .IncrNextSenderMsgSeqNum ()
347
351
}
348
352
349
- func (s * session ) sendQueued () {
350
- for _ , msgBytes := range s .toSend {
351
- s .sendBytes (msgBytes )
353
+ func (s * session ) sendQueued (blockUntilSent bool ) {
354
+ for i , msgBytes := range s .toSend {
355
+ if ! s .sendBytes (msgBytes , blockUntilSent ) {
356
+ s .toSend = s .toSend [i :]
357
+ s .notifyMessageOut ()
358
+ return
359
+ }
352
360
}
353
361
354
362
s .dropQueued ()
@@ -363,18 +371,30 @@ func (s *session) EnqueueBytesAndSend(msg []byte) {
363
371
defer s .sendMutex .Unlock ()
364
372
365
373
s .toSend = append (s .toSend , msg )
366
- s .sendQueued ()
374
+ s .sendQueued (true )
367
375
}
368
376
369
- func (s * session ) sendBytes (msg []byte ) {
377
+ func (s * session ) sendBytes (msg []byte , blockUntilSent bool ) bool {
370
378
if s .messageOut == nil {
371
379
s .log .OnEventf ("Failed to send: disconnected" )
372
- return
380
+ return false
381
+ }
382
+
383
+ if blockUntilSent {
384
+ s .messageOut <- msg
385
+ s .log .OnOutgoing (msg )
386
+ s .stateTimer .Reset (s .HeartBtInt )
387
+ return true
373
388
}
374
389
375
- s .log .OnOutgoing (msg )
376
- s .messageOut <- msg
377
- s .stateTimer .Reset (s .HeartBtInt )
390
+ select {
391
+ case s .messageOut <- msg :
392
+ s .log .OnOutgoing (msg )
393
+ s .stateTimer .Reset (s .HeartBtInt )
394
+ return true
395
+ default :
396
+ return false
397
+ }
378
398
}
379
399
380
400
func (s * session ) doTargetTooHigh (reject targetTooHigh ) (nextState resendState , err error ) {
0 commit comments