Skip to content

Commit 1e0daf0

Browse files
committed
Fix non-configurable timeout issue in apns_async
1 parent b96739f commit 1e0daf0

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed

push_notifications/apns_async.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ def apns_send_bulk_message(
305305
mutable_content: Optional[bool] = False,
306306
category: Optional[str] = None,
307307
err_func: Optional[ErrFunc] = None,
308+
timeout: Optional[int] = None,
308309
) -> Dict[str, str]:
309310
"""
310311
Sends an APNS notification to one or more registration_ids.
@@ -326,7 +327,11 @@ def apns_send_bulk_message(
326327
Notification Content Extension or UNNotificationCategory configuration.
327328
It allows the app to display custom actions with the notification.
328329
:param content_available: If True the `content-available` flag will be set to 1, allowing the app to be woken up in the background
330+
:param timeout: Timeout in seconds for each notification send operation
329331
"""
332+
if not timeout:
333+
timeout = get_manager().get_apns_error_timeout(application_id)
334+
330335
try:
331336
topic = get_manager().get_apns_topic(application_id)
332337
results: Dict[str, str] = {}
@@ -351,6 +356,7 @@ def apns_send_bulk_message(
351356
mutable_content=mutable_content,
352357
category=category,
353358
err_func=err_func,
359+
timeout=timeout,
354360
)
355361
)
356362

@@ -386,6 +392,7 @@ def apns_send_bulk_message(
386392

387393
async def _send_bulk_request(
388394
registration_ids: list[str],
395+
timeout: int,
389396
alert: Union[str, Alert],
390397
application_id: Optional[str] = None,
391398
creds: Optional[Credentials] = None,
@@ -432,16 +439,17 @@ async def _send_bulk_request(
432439
for registration_id in registration_ids
433440
]
434441

435-
send_requests = [_send_request(client, request) for request in requests]
442+
send_requests = [_send_request(client, request, timeout) for request in requests]
436443
return await asyncio.gather(*send_requests)
437444

438445

439446
async def _send_request(
440447
apns: APNs,
441448
request: NotificationRequest,
449+
timeout: int,
442450
) -> Tuple[str, NotificationResult]:
443451
try:
444-
res = await asyncio.wait_for(apns.send_notification(request), timeout=1)
452+
res = await asyncio.wait_for(apns.send_notification(request), timeout=timeout)
445453
return request.device_token, res
446454

447455
except asyncio.TimeoutError:

push_notifications/conf/app.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,10 @@ def get_apns_use_alternative_port(
380380
def get_apns_topic(self, application_id: Optional[str] = None) -> Optional[str]:
381381
return self._get_application_settings(application_id, "APNS", "TOPIC")
382382

383+
384+
def get_apns_error_timeout(self, application_id: Optional[str] = None) -> int:
385+
return self._get_application_settings(application_id, "APNS", "ERROR_TIMEOUT")
386+
383387
def get_wns_package_security_id(self, application_id: Optional[str] = None) -> str:
384388
return self._get_application_settings(
385389
application_id, "WNS", "PACKAGE_SECURITY_ID"

push_notifications/conf/base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ def get_apns_use_sandbox(self, application_id: Optional[str] = None) -> bool:
2222
def get_apns_use_alternative_port(self, application_id: Optional[str] = None) -> bool:
2323
raise NotImplementedError
2424

25+
def get_apns_error_timeout(self, application_id: Optional[str] = None) -> int:
26+
raise NotImplementedError
27+
2528
def get_wns_package_security_id(self, application_id: Optional[str] = None) -> str:
2629
raise NotImplementedError
2730

push_notifications/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
PUSH_NOTIFICATIONS_SETTINGS.setdefault("APNS_USE_SANDBOX", False)
1919
PUSH_NOTIFICATIONS_SETTINGS.setdefault("APNS_USE_ALTERNATIVE_PORT", False)
2020
PUSH_NOTIFICATIONS_SETTINGS.setdefault("APNS_TOPIC", None)
21+
PUSH_NOTIFICATIONS_SETTINGS.setdefault("APNS_ERROR_TIMEOUT", 5)
2122

2223
# WNS
2324
PUSH_NOTIFICATIONS_SETTINGS.setdefault("WNS_PACKAGE_SECURITY_ID", None)

0 commit comments

Comments
 (0)