GNS上的APNS通过套接字在几个小时后停止工作

时间:2016-12-21 23:55:54

标签: ssl erlang apple-push-notifications elixir google-compute-engine

这是我从Heroku迁移到Google容器引擎后开始面临的一个有趣问题:

自从迁移到GCE后,在服务器启动/重启/部署几个小时后,我的Elixir应用程序无法再向APNS发送推送通知。我正在使用apns4ex库。这大致是我到目前为止所发现的:

在init内部,库向APNS打开一个:ssl(erlang)套接字并继续在GenServer进程内回收

def connect_socket(host, port, opts, timeout_seconds) do
    address = "#{host}:#{port}"

    case :ssl.connect(host, port, opts, timeout_seconds * 1000) do
      {:ok, socket} ->
        APNS.Logger.debug("successfully connected to #{address}")
        {:ok, socket}
      {:error, reason} ->
        APNS.Logger.error("failed to connect to push socket #{address}, reason given: #{inspect(reason)}")
        {:error, {:connection_failed, address}}
    end
end

现在,从x小时开始,在尝试发送消息后,库开始接收:ssl_closed消息/回调以指示SSL连接已关闭

def handle_info({:ssl_closed, socket}, %{socket_apple: socket} = state) do
  APNS.Logger.debug("ssl socket closed, returning :connect")
  {:connect, {:error, "ssl_closed"}, %{state | socket_apple: nil}}
end

它如何处理这个问题就是让关闭连接并返回:connect,然后重新连接到APNS(here

推送通知停止工作后,调试日志始终在每条消息上报告以下模式。

  1. 尝试发送消息
  2. 报告“发送成功”(没有任何内容发送到手机。此消息是由:ssl.send举报:ok
  3. 引起的
  4. 然后收到ssl套接字关闭消息
  5. 重新连接到gateway.push.apple.com(:ssl.connect返回:ok
  6. 重复
  7. send_package代码:

    def send_package(socket, packet) do
      result = :ssl.send(socket, [packet])
    
      case result do
        :ok ->
          APNS.Logger.debug("success sending ssl package")
        {:error, reason} ->
          APNS.Logger.warn("error #{reason} sending ssl package")
      end
    
      result
    end
    

    相反,成功发送后,它会在第2点停止。

    这是发送推送时我的应用程序的一些原始日志输出(注意显示我描述的模式的最后9行)

    01:41:14.820 request_id=fecds3h3s1so2825c44qfestvvvpv707 [debug] [APNS] #PID<0.20135.97> 23303051:1ad798 sending in poolboy transaction :myapp
    01:41:14.821 request_id=fecds3h3s1so2825c44qfestvvvpv707 [debug] [APNS] #PID<0.20135.97> 23303051:1ad798 sending message
    01:41:14.821 request_id=fecds3h3s1so2825c44qfestvvvpv707 [debug] [APNS] #PID<0.20135.97> 62064556:b12e98 sending in poolboy transaction :myapp
    01:41:14.821 [debug] [APNS] #PID<0.349.0> 23303051:1ad798 handling cast :send
    01:41:14.821 [debug] [APNS] #PID<0.349.0> 23303051:1ad798 message's payload looks good
    01:41:14.821 request_id=fecds3h3s1so2825c44qfestvvvpv707 [debug] [APNS] #PID<0.20135.97> 62064556:b12e98 sending message
    01:41:14.821 request_id=fecds3h3s1so2825c44qfestvvvpv707 [debug] [APNS] #PID<0.20135.97> 19048099:b3ed8e sending in poolboy transaction :myapp
    01:41:14.822 [debug] [APNS] #PID<0.349.0> success sending ssl package
    01:41:14.822 [debug] [APNS] #PID<0.349.0> 23303051:1ad798 success sending
    01:41:14.822 [debug] [APNS] #PID<0.349.0> 23303051:1ad798 handle call :send received :ok
    01:41:14.822 [debug] [APNS] #PID<0.348.0> 62064556:b12e98 handling cast :send
    01:41:14.822 [debug] [APNS] #PID<0.348.0> 62064556:b12e98 message's payload looks good
    01:41:14.823 request_id=fecds3h3s1so2825c44qfestvvvpv707 [debug] [APNS] #PID<0.20135.97> 19048099:b3ed8e sending message
    01:41:14.823 request_id=fecds3h3s1so2825c44qfestvvvpv707 [info] Sent 200 in 22ms
    01:41:14.823 [debug] [APNS] #PID<0.348.0> success sending ssl package
    01:41:14.823 [debug] [APNS] #PID<0.348.0> 62064556:b12e98 success sending
    01:41:14.823 [debug] [APNS] #PID<0.348.0> 62064556:b12e98 handle call :send received :ok
    01:41:14.823 [debug] [APNS] #PID<0.347.0> 19048099:b3ed8e handling cast :send
    01:41:14.824 [debug] [APNS] #PID<0.347.0> 19048099:b3ed8e message's payload looks good
    01:41:14.824 [debug] [APNS] #PID<0.347.0> success sending ssl package
    01:41:14.824 [debug] [APNS] #PID<0.347.0> 19048099:b3ed8e success sending
    01:41:14.824 [debug] [APNS] #PID<0.347.0> 19048099:b3ed8e handle call :send received :ok
    01:41:15.027 [debug] [APNS] #PID<0.348.0> ssl socket closed, returning :connect
    01:41:15.029 [debug] [APNS] #PID<0.347.0> ssl socket closed, returning :connect
    01:41:15.043 [debug] [APNS] #PID<0.349.0> ssl socket closed, returning :connect
    01:41:15.207 [debug] [APNS] #PID<0.348.0> successfully connected to gateway.push.apple.com:2195
    01:41:15.207 [debug] [APNS] #PID<0.348.0> successfully connected to socket
    01:41:15.209 [debug] [APNS] #PID<0.347.0> successfully connected to gateway.push.apple.com:2195
    01:41:15.209 [debug] [APNS] #PID<0.347.0> successfully connected to socket
    01:41:15.214 [debug] [APNS] #PID<0.349.0> successfully connected to gateway.push.apple.com:2195
    01:41:15.214 [debug] [APNS] #PID<0.349.0> successfully connected to socket
    

    一种理论认为GCE正在关闭空闲连接,但这并不能解释为什么重新连接后的另一条消息会导致相同的模式。另外,为什么套接字仅在使用:ssl.send发送后关闭?

1 个答案:

答案 0 :(得分:0)

我和apns4erl有同样的问题,当套接字尝试发送消息后关闭,但问题在我身边,不记得,它是在错误的证书文件或格式不正确的消息