Skip to content

notification Queue replace to ConcurrentQueue #770

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 26 additions & 26 deletions PushSharp.Apple/ApnsConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading.Tasks;
using System.Security.Cryptography.X509Certificates;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Threading;
using System.Net;
using PushSharp.Core;
Expand Down Expand Up @@ -68,38 +69,33 @@ public ApnsConnection (ApnsConfiguration configuration)
byte[] buffer = new byte[6];
int id;


SemaphoreSlim connectingSemaphore = new SemaphoreSlim (1);
SemaphoreSlim batchSendSemaphore = new SemaphoreSlim (1);
object notificationBatchQueueLock = new object ();


//readonly object connectingLock = new object ();
Queue<CompletableApnsNotification> notifications = new Queue<CompletableApnsNotification> ();
ConcurrentQueue<CompletableApnsNotification> notifications = new ConcurrentQueue<CompletableApnsNotification>();
List<SentNotification> sent = new List<SentNotification> ();

Timer timerBatchWait;

public void Send (CompletableApnsNotification notification)
{
lock (notificationBatchQueueLock) {

notifications.Enqueue (notification);

if (notifications.Count >= Configuration.InternalBatchSize) {

// Make the timer fire immediately and send a batch off
timerBatchWait.Change (0, Timeout.Infinite);
return;
}

// Restart the timer to wait for more notifications to be batched
// This timer will keep getting 'restarted' before firing as long as notifications
// are queued before the timer's due time
// if the timer is actually called, it means no more notifications were queued,
// so we should flush out the queue instead of waiting for more to be batched as they
// might not ever come and we don't want to leave anything stranded in the queue
timerBatchWait.Change (Configuration.InternalBatchingWaitPeriod, Timeout.InfiniteTimeSpan);
notifications.Enqueue(notification);

if (notifications.Count >= Configuration.InternalBatchSize) {

// Make the timer fire immediately and send a batch off
timerBatchWait.Change (0, Timeout.Infinite);
return;
}

// Restart the timer to wait for more notifications to be batched
// This timer will keep getting 'restarted' before firing as long as notifications
// are queued before the timer's due time
// if the timer is actually called, it means no more notifications were queued,
// so we should flush out the queue instead of waiting for more to be batched as they
// might not ever come and we don't want to leave anything stranded in the queue
timerBatchWait.Change (Configuration.InternalBatchingWaitPeriod, Timeout.InfiniteTimeSpan);
}

long batchId = 0;
Expand All @@ -113,15 +109,19 @@ async Task SendBatch ()
// Pause the timer
timerBatchWait.Change (Timeout.Infinite, Timeout.Infinite);

if (notifications.Count <= 0)
if (notifications.IsEmpty)
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);
while (!notifications.IsEmpty && toSend.Count < Configuration.InternalBatchSize) {

CompletableApnsNotification n;
if (notifications.TryDequeue (out n))
{
toSend.Add(n);
}
}


Expand Down