Skip to content

apns - handling error from previous batch #774

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 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion PushSharp.Apple/ApnsConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void Initialize (ApnsServerEnvironment serverEnvironment, X509Certificate2 certi

Certificate = certificate;

MillisecondsToWaitBeforeMessageDeclaredSuccess = 3000;
MillisecondsToWaitBeforeMessageDeclaredSuccess = 750;
ConnectionTimeout = 10000;
MaxConnectionAttempts = 3;

Expand Down
25 changes: 22 additions & 3 deletions PushSharp.Apple/ApnsConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ async Task Reader ()
// read (in the case that all the messages sent successfully, apple will send us nothing
// So, let's make our read timeout after a reasonable amount of time to wait for apple to tell
// us of any errors that happened.
readCancelToken.CancelAfter (750);
readCancelToken.CancelAfter (Configuration.MillisecondsToWaitBeforeMessageDeclaredSuccess);

int len = -1;

Expand Down Expand Up @@ -252,10 +252,15 @@ async Task Reader ()
//Get the index of our failed notification (by identifier)
var failedIndex = sent.FindIndex (n => n.Identifier == identifier);

// If we didn't find an index for the failed notification, something is wrong
// Let's just return
// Looks like failed index was from previous batch. The reason is that readCancelToken timeout wasn't enought
// to get response from apns.
// So put notifications from current batch again to queue and use new socket for next batches
if (failedIndex < 0)
{
Log.Info ("APNS-Client[{0}]: Cant find failed notification {1} in current batch", id, identifier);
EnqueRemainingBatchItems ();
return;
}

// Get all the notifications before the failed one and mark them as sent!
if (failedIndex > 0) {
Expand Down Expand Up @@ -286,6 +291,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
EnqueRemainingBatchItems();
}

private void EnqueRemainingBatchItems()
{
foreach (var s in sent)
notifications.Enqueue (s.Notification);

Expand All @@ -307,6 +317,15 @@ bool socketCanWrite ()
if (!client.Client.Connected)
return false;

// looks like response for previous batch wasn't read (and some errors were in previous batch)
// Unfortunatelly we already notified client that notifications were sent.
// But at least we won't use this socket for new batch because otherwise new enqueued notifications won't be sent too...
if (client.Available > 0)
{
Log.Info ("APNS-Client[{0}]: Previous batch wasn't processed correctly. Try to increase ResponseWaitTimeout");
return false;
}

var p = client.Client.Poll (1000, SelectMode.SelectWrite);

Log.Info ("APNS-Client[{0}]: Can Write? {1}", id, p);
Expand Down