Skip to content

Commit

Permalink
refs #15264 Implemented locking for notifications Queue in ApnsConnec…
Browse files Browse the repository at this point in the history
…tion

suggested in Redth#672 (comment)
  • Loading branch information
yaroslav-metelev committed Sep 18, 2019
1 parent 28ec3eb commit b4f95a9
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions PushSharp.Apple/ApnsConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,21 @@ async Task SendBatch ()

// Pause the timer
timerBatchWait.Change (Timeout.Infinite, Timeout.Infinite);

if (notifications.Count <= 0)
return;


// Let's store the batch items to send internally
var toSend = new List<CompletableApnsNotification> ();

while (notifications.Count > 0 && toSend.Count < Configuration.InternalBatchSize) {
var n = notifications.Dequeue ();
toSend.Add (n);
}
lock (notificationBatchQueueLock)
{
if (notifications.Count <= 0)
return;

while (notifications.Count > 0 && toSend.Count < Configuration.InternalBatchSize)
{
var n = notifications.Dequeue();
toSend.Add(n);
}
}

Log.Info ("APNS-Client[{0}]: Sending Batch ID={1}, Count={2}", id, batchId, toSend.Count);

Expand Down Expand Up @@ -305,8 +308,11 @@ async Task Reader ()
// The remaining items in the list were sent after the failed notification
// we can assume these were ignored by apple so we need to send them again
// Requeue the remaining notifications
foreach (var s in sent)
notifications.Enqueue (s.Notification);
lock (notificationBatchQueueLock)
{
foreach (var s in sent)
notifications.Enqueue(s.Notification);
}

// Clear our sent list
sent.Clear ();
Expand Down

0 comments on commit b4f95a9

Please sign in to comment.