Pr0Ger / PyAPNs2

Python library for interacting with the Apple Push Notification service (APNs) via HTTP/2 protocol
MIT License
349 stars 179 forks source link

ConnectionResetError raised from `hyper` #30

Closed ryanwang520 closed 6 years ago

ryanwang520 commented 7 years ago

Hi! It seems that apple will disconnect the socket if the apns client does not send any message to the apns server for a while(maybe several hours),and a ConnectionResetError would be raised from the underlying hyper package when you send message using the remotely closed socket. To handle this, I just hack it like the code below, which I don't think is an elegant way.

apns = APNsClient()
try:
    apns.send_notification(device_token, payload, topic=APNS_TOPIC)
except hyper.common.exceptions.ConnectionResetError:
    global apns
    ## just init a new apns client instance
    apns = APNsClient()
    apns.send_notification(device_token, payload, topic=APNS_TOPIC)

There may exist a good way to solve the connection reset problem just in the pyapns2 package,
any ideas?

Thanks.

leovp commented 7 years ago

What about APNsClient.connect()? The docstring says:

Establish a connection to APNs. If already connected, the function does nothing. If the connection fails, the function retries up to MAX_CONNECTION_RETRIES times.

ryanwang520 commented 7 years ago

This just won't work, the APNsClient.connect method just calls the corresponding connect method from hyper, which does nothing if the connection is already established. you can just refer to the code from the link below. https://github.com/Lukasa/hyper/blob/development/hyper/http20/connection.py#L344

vimiPonyai commented 4 years ago

So what`s the best solution for that issue.