pushy icon indicating copy to clipboard operation
pushy copied to clipboard

The delay of APNs's response is very high(even up to 700s)

Open extraSix opened this issue 7 years ago • 15 comments

Time: 12:00-17:00 UTC everyday Location: China The delay of APNs's response: The elapse from sending a notification to APNs Server to receiving a response from it. Normal delay: 500ms High delay: 10s-700s Through put: 50/s

Description:

  1. One of my servers' delay is very high (even up to 700 seconds), but another server's delay just slightly increase.
  2. Even though the delay is very high, the responses indicate that the most of notifications was post successfully.
  3. Restarting my push service will immediately reduce the delay.

extraSix avatar Nov 12 '19 00:11 extraSix

In the interest of making it easy to follow this conversation in the future, it sounds like you've also posted this question on the Apple Developer Forums.

Let me begin by saying that I don't think this is likely to be an issue with Pushy itself. We've known for a long time that users in China often experience poor performance when talking to production APNs servers. The last time we checked, there were no APNs servers in China, and so the route to a server is long. You can check this by getting a list of IP addresses with something like nslookup or dig:

jon@cephalopod ~ $ nslookup api.push.apple.com
Server:		8.8.8.8
Address:	8.8.8.8#53

Non-authoritative answer:
api.push.apple.com	canonical name = api.push-apple.com.akadns.net.
Name:	api.push-apple.com.akadns.net
Address: 17.188.162.206
Name:	api.push-apple.com.akadns.net
Address: 17.188.165.9
Name:	api.push-apple.com.akadns.net
Address: 17.188.162.78
Name:	api.push-apple.com.akadns.net
Address: 17.188.162.138
Name:	api.push-apple.com.akadns.net
Address: 17.188.160.11
Name:	api.push-apple.com.akadns.net
Address: 17.188.167.9
Name:	api.push-apple.com.akadns.net
Address: 17.188.168.9
Name:	api.push-apple.com.akadns.net
Address: 17.188.165.11

You can then use something like traceroute or a geo-IP service to get a rough idea for where those servers are.

We're reasonably confident from past experience that the problem exists somewhere at the ISP/backbone level somewhere in China, and there's not much we can do about it from within Pushy.

The thing I really want to address here is:

Restarting my push service will immediately reduce the delay.

My guess is that you're seeing HTTP/2 streams slowly get exhausted. Basically, the APNs server allows us to have (let's say) 2,000 streams open in parallel. We send one notification per stream. With a fresh connection, you can use all 2,000 streams at once and notifications will send quickly. Given a lossy network connection, though, streams will get "jammed" if the server takes a long time to acknowledge a request to send a notification. Eventually, a majority of streams can get "jammed," leaving only a few to pass notifications through at a reasonable pace.

I think you can test this hypothesis by creating an ApnsClientMetricsListener that keeps track of the number of the difference between the number of write attempts and the number of "resolved" notifications (accepted, rejected, or failed). That should give a count of how many notifications are "in flight" and unresolved at any given time. If that number slowly climbs, then the stream exhaustion thing is likely what's going on.

If you're up for it, that'd be very helpful data to gather.

jchambers avatar Nov 12 '19 20:11 jchambers

In the interest of making it easy to follow this conversation in the future, it sounds like you've also posted this question on the Apple Developer Forums.

Let me begin by saying that I don't think this is likely to be an issue with Pushy itself. We've known for a long time that users in China often experience poor performance when talking to production APNs servers. The last time we checked, there were no APNs servers in China, and so the route to a server is long. You can check this by getting a list of IP addresses with something like nslookup or dig:

jon@cephalopod ~ $ nslookup api.push.apple.com
Server:		8.8.8.8
Address:	8.8.8.8#53

Non-authoritative answer:
api.push.apple.com	canonical name = api.push-apple.com.akadns.net.
Name:	api.push-apple.com.akadns.net
Address: 17.188.162.206
Name:	api.push-apple.com.akadns.net
Address: 17.188.165.9
Name:	api.push-apple.com.akadns.net
Address: 17.188.162.78
Name:	api.push-apple.com.akadns.net
Address: 17.188.162.138
Name:	api.push-apple.com.akadns.net
Address: 17.188.160.11
Name:	api.push-apple.com.akadns.net
Address: 17.188.167.9
Name:	api.push-apple.com.akadns.net
Address: 17.188.168.9
Name:	api.push-apple.com.akadns.net
Address: 17.188.165.11

You can then use something like traceroute or a geo-IP service to get a rough idea for where those servers are.

We're reasonably confident from past experience that the problem exists somewhere at the ISP/backbone level somewhere in China, and there's not much we can do about it from within Pushy.

The thing I really want to address here is:

Restarting my push service will immediately reduce the delay.

My guess is that you're seeing HTTP/2 streams slowly get exhausted. Basically, the APNs server allows us to have (let's say) 2,000 streams open in parallel. We send one notification per stream. With a fresh connection, you can use all 2,000 streams at once and notifications will send quickly. Given a lossy network connection, though, streams will get "jammed" if the server takes a long time to acknowledge a request to send a notification. Eventually, a majority of streams can get "jammed," leaving only a few to pass notifications through at a reasonable pace.

I think you can test this hypothesis by creating an ApnsClientMetricsListener that keeps track of the number of the difference between the number of write attempts and the number of "resolved" notifications (accepted, rejected, or failed). That should give a count of how many notifications are "in flight" and unresolved at any given time. If that number slowly climbs, then the stream exhaustion thing is likely what's going on.

If you're up for it, that'd be very helpful data to gather.

The APNs servers which show in nslookup are located in the United States. I have also suspected that there was problems with the network between China and the APNs server. It is confirmed that when the delay rises, the number of in-flight messages must rise. I will reply you when I get the test result.

Supplement: My high delay push server also have other APNS client for the other apps. Their throughputs are lower and delays are normal(500ms).

extraSix avatar Nov 14 '19 15:11 extraSix

Hello,

looks like recently we have started to get similar problem. From Feb 04 evening push delivery throughput to our most used application constantly degrades until we recreate connections to the Apple. Not 100% sure yet what happens, but investigation shows that Apple started to send settings frames with MAX_CONCURRENT_STREAMS=1 and immediately after that with MAX_CONCURRENT_STREAMS=1000. Also it seems, than some connections receive only MAX_CONCURRENT_STREAMS=1. And it happens with 1-1.5 hour interval. After getting these settings, some pushes do not deliver in 40 seconds (we wait so much for acknowledgement). At the same time we see in logs that pushy starts to send PING for some connections, but Apple to all these requests answer with PING ack, so connections are live and are not closed by pushy. On next settings getting situation gets worse, because more deliveries falls off with timeouts. My guess is it happens because there are more connections where delivery time is more than 40 seconds. Maybe pushy could be modified so that ApnsClient::sendNotification has timeout parameter or when its resulting PushNotificationFuture.get fails with timeout, then ApnsClient removes that connection from its connection pool. Maybe it should be based on configurable consecutive timeout counter per connection. Or maybe sendNotification should return connection too and ApnsClient have public method to remove it from pool, so all of that could be managed outside of pushy.

janzar avatar Feb 06 '20 13:02 janzar

Folks, please accept my apologies for the long delay on getting back to this issue.

My high delay push server also have other APNS client for the other apps. Their throughputs are lower and delays are normal(500ms).

@extraSix Are those clients using HTTP/2, or are they using the old "binary" protocol?

Not 100% sure yet what happens, but investigation shows that Apple started to send settings frames with MAX_CONCURRENT_STREAMS=1 and immediately after that with MAX_CONCURRENT_STREAMS=1000.

@janzar We've seen this before. I think this is strange behavior, but have been told it's not a bug (or at least it wasn't when I last asked about it a couple years ago). Basically, whenever the APNs server rejects a notification, it throttles the max concurrent streams extremely aggressively (i.e. down to just 1), but then returns it to a much higher value once a notification has been accepted. Since rejection is pretty routine, this behavior can lead to some very choppy concurrent stream counts.

Your point about timeouts is taken, but I don't know that it will help in this case; if the problem is that we can't communicate with the server as quickly as we'd like, there's no way to "cancel" a push notification and reclaim the HTTP/2 stream that's faster than just waiting for the notification to go through. If we just time out locally then try to send more notifications without telling the server that we've closed the stream, the notification will just get rejected at the protocol level.

I'll see if I can learn more about what's going on with the stream counts.

jchambers avatar Feb 12 '20 23:02 jchambers

Jon, thank you for response.

Today have traced all rejections on one server for 1.5 hours. Then when 10 connections have started to have problems, compared rejections with channels. 2 of 10 had device token not for topic. But there where more than 10 channels that continued to work, that too had this error response. The same with some other rejections. So, seems that there is no correlation between rejections and non-functioning connections.

janzar avatar Feb 14 '20 15:02 janzar

Very strange. Thanks for the additional information!

jchambers avatar Feb 14 '20 16:02 jchambers

Here is how we implemented workaround currently. Wait for the response for Apple for lets say 10 seconds. If there is none, set: channel.attr("some_key").compareAndSet(0L, System.currentTimeMillis() + some_interval (lets say 2-3 seconds)) If after that we get response for some other notification. reset: channel.attr("some_key").set(0) Then inside ApnsChannelPool::acquireWithinEventExecutor where channel.isActive() check happens, if it is active, we also check channel.attr("some_key").get() == 0 || > System.currentTimeMillis() and only then assume that channel is active. So when timeouts starts to begin, we close the connection after +/-12 seconds. all notifications, that are already sent after timeout begin, or are in some internal netty queue for channel, cancels with timeout in next 10 seconds. And we redeliver them. As told earlier, PING through these connections continue to work.

Not the best solution, but at least works for last couple days.

I would like to stay with pushy version from github. So can share my changes with you in some way. So you could extend pushy in some generic way. And if someone experiences similar problems, could plug own code through some interface(s).

janzar avatar Feb 14 '20 16:02 janzar

Disclaimer: We are still using Pushy 0.10.x but it seems the affected code has not changed much.

We started experiencing extreme slowdowns in connections a few weeks ago as well. We also see the connection limited to MAX_CONCURRENT_STREAMS=1. After a lots of logging and debugging we could narrow it down to connections going bad when they are older than one hour. In our log we then see APNs responding with ExpiredProviderToken. We think there may be a problem with the handling of that message.

The Apple Documentation says:

To ensure security, APNs requires new tokens to be generated periodically. A new token has an updated issued at claim key, whose value indicates the time the token was generated. If the timestamp for token issue is not within the last hour, APNs rejects subsequent push messages, returning an ExpiredProviderToken (403) error.

Our workaround right now is to kill connections before they reach one hour. This is working quite well for us for a few days now.

agerauer avatar Feb 17 '20 09:02 agerauer

@helle we too had deployed solution, where reconnect after one hour, for few days, and it worked fine. But later changed to solution what have described above.

@jchambers maybe you could simply add ttl param for signing key. If it is set, then just recreate it after that time. Perhaps it would be enough.

janzar avatar Feb 17 '20 11:02 janzar

Would like to add - when checked for responses, had done it in listener attached to future returned form ApnsClient::sendNotification. Therefore missed ExpiredProviderToken. Seems that it would be great to log such responses with channels in TokenAuthenticationApnsClientHandler, or through ApnsClientMetricsListener, but then channel would be required too. IMHO it would help in future investigations.

janzar avatar Feb 17 '20 12:02 janzar

The token expiration insight is really helpful. At the same time, if this spontaneously became a problem, my strong suspicion is that something has also changed upstream. I'll investigate!

jchambers avatar Feb 17 '20 16:02 jchambers

I added some more logging in a061be6. As you folks point out, I think that will be helpful regardless of whatever else is going on.

jchambers avatar Feb 17 '20 16:02 jchambers

So I have learned that:

  1. The APNs server will, indeed, throttle connections to a single stream as a defensive measure if the auth token expires.
  2. This was always the intended behavior, but had not been enforced until recently.
  3. The expectation from Apple's side is, indeed, that clients should proactively rotate authentication tokens before they expire.

I'll open a pull request to add that behavior momentarily.

jchambers avatar Feb 18 '20 01:02 jchambers

Hi, do you have an estimated timeline to release v0.13.11? I am also facing the same issue as well so I look forward to updating pushy to v0.13.11 in my service.

phisixersai avatar Feb 20 '20 04:02 phisixersai

Hi, do you have an estimated timeline to release v0.13.11?

There are a few open issues that still need to be addressed before the next release, including some logistics around actually doing the release under new ownership.

I'd expect something early next week, but that's an estimate and not a hard promise.

jchambers avatar Feb 20 '20 15:02 jchambers