nomad/houston — pull request #17 by jcxplorer (2013-07-15 20:21:26+00:00)
Title: Automatically handle dropped connections from APNs

About 3 months ago I tried to make Houston automatically handle dropped connections from APNs. It's been in production ever since, pushing many thousands of notifications per day without issues, so I'm bringing it here for discussion.

As you probably know, whenever APNs encounters an invalid device token it will drop the connection and no further notifications will get delivered.

Here's my proposal to fix this: https://github.com/jcxplorer/houston/compare/dropped-connections

My patch does two things:

* Switches to the enhanced notification format.
* If there's more than one notification being sent, we'll check if APNs has returned an error before disconnecting. Because we're now using the enhanced notification format we know which notification failed so we can skip it and continue from the next one.

There are two `IO.select` calls in that code. The first one that executes inside the loop is async so there's no slowdown of any kind. The last one is a synchronous call with a configurable timeout with a default of 0.5 seconds, so we don't disconnect right away after sending many push notifications in case we still get an error.

Again, if you're sending only one notification, the synchronous `IO.select` never gets executed, because there's nothing to recover from, thus not slowing down sending a single push notification.

I think this implementation is simple enough and so far it has worked great. It also lets you use Houston like before, no changes required. If this is something you'd like, I'll be happy to port it to the latest version of Houston and send a pull request!