为什么我与 Apple APNS 的 TCP 连接挂起并强行断开连接

Why does my TCP connection with Apple APNS hangs and forcibly disconnects

我正在使用一个很棒的库 PushSharp 向许多客户端设备发送 Apple 和 Android 推送通知。 自上周以来,我们经常面临连接问题。在这些问题之前,我们的托管环境没有更新或发生任何事情。

问题出在 ApnsConnection.cs 的这一行:

stream.AuthenticateAsClient (Configuration.Host, certificates, System.Security.Authentication.SslProtocols.Tls, false);

有时TCP连接在这里停止响应。大约 21 分钟后,抛出以下异常:

System.IO.IOException: Unable to read data from the connection: 
An existing connection was forcibly closed by the remote host. ---> 
System.Net.Sockets.SocketException: An existing connection was forcibly 
closed by the remote host

来自 PushSharp 的日志显示如下:

13:11:33 | PushServiceV2 | APNS-Client[7]: Sending Batch ID=1, Count=21
13:11:33 | PushServiceV2 | APNS-Client[7]: Connecting (Batch ID=1)
13:11:33 | PushServiceV2 | APNS-Client[7]: client.ConnectAsync
13:11:33 | PushServiceV2 | APNS-Client[7]: client.Client.SetSocketOption
13:11:33 | PushServiceV2 | APNS-Client[7]: SetSocketKeepAliveValues
13:11:33 | PushServiceV2 | APNS-Client[7]: Configuration.SkipSsl:False
13:11:33 | PushServiceV2 | APNS-Client[7]: Create our ssl stream
13:11:33 | PushServiceV2 | APNS-Client[7]: stream.AuthenticateAsClient
13:32:03 | PushServiceV2 | APNS-CLIENT[7]: Send Batch Error: Batch ID=1, Error=System.IO.IOException:

如您所见,在 "connection is forcibly closed by the remote host."

之前需要 21 分钟

为了解决任何问题,我首先检查发送一批推送通知后是否断开所有 TCP 连接。此外,我将批次之间的时间增加到 5 分钟。如果 Apple 因为我建立了很多连接而断开了我的连接,我想这应该可以解决它。但事实并非如此。

有没有关于如何进一步调查此问题的任何线索或提示?

非常感谢!

我想我们只是达到了连接数的限制。我已阅读此文档:Troubleshooting Push Notifications 并记下了这一行:

Another possibility is that you've connected too many times to APNs and further connections have been temporarily blocked. As is documented in the Local and Remote Notification Programming Guide, developers are expected to open a connection and leave it open. If a connection is opened and closed repeatedly, APNs will treat it as a denial of service attack and block connections for a period of time.

我重写了我的实现,PushSharp so that TCP connections are created and closed as little times as possible. It's running for a few days now without any problems. In the logging I see that the connection is only disconnected once each day by apple, and PushSharp 的使用很好地重新连接。