push_notifications: Use lru_cache decorator.

This does the same thing in a simpler way.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2021-06-07 17:02:27 -07:00 committed by Tim Abbott
parent 187737ce5a
commit 0bc002270c
2 changed files with 12 additions and 18 deletions

View File

@ -4,6 +4,7 @@ import base64
import logging import logging
import re import re
import time import time
from functools import lru_cache
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Sequence, Tuple, Union from typing import TYPE_CHECKING, Any, Dict, List, Optional, Sequence, Tuple, Union
import gcm import gcm
@ -59,26 +60,20 @@ def hex_to_b64(data: str) -> str:
# Sending to APNs, for iOS # Sending to APNs, for iOS
# #
_apns_client: Optional["APNsClient"] = None
_apns_client_initialized = False
@lru_cache(maxsize=None)
def get_apns_client() -> "Optional[APNsClient]": def get_apns_client() -> "Optional[APNsClient]":
# We lazily do this import as part of optimizing Zulip's base # We lazily do this import as part of optimizing Zulip's base
# import time. # import time.
from apns2.client import APNsClient from apns2.client import APNsClient
global _apns_client, _apns_client_initialized if settings.APNS_CERT_FILE is None:
if not _apns_client_initialized: return None
# NB if called concurrently, this will make excess connections.
# That's a little sloppy, but harmless unless a server gets # NB if called concurrently, this will make excess connections.
# hammered with a ton of these all at once after startup. # That's a little sloppy, but harmless unless a server gets
if settings.APNS_CERT_FILE is not None: # hammered with a ton of these all at once after startup.
_apns_client = APNsClient( return APNsClient(credentials=settings.APNS_CERT_FILE, use_sandbox=settings.APNS_SANDBOX)
credentials=settings.APNS_CERT_FILE, use_sandbox=settings.APNS_SANDBOX
)
_apns_client_initialized = True
return _apns_client
def apns_enabled() -> bool: def apns_enabled() -> bool:

View File

@ -1313,7 +1313,7 @@ class TestAPNs(PushNotificationTest):
""" """
import zerver.lib.push_notifications import zerver.lib.push_notifications
zerver.lib.push_notifications._apns_client_initialized = False zerver.lib.push_notifications.get_apns_client.cache_clear()
try: try:
with self.settings(APNS_CERT_FILE="/foo.pem"), mock.patch( with self.settings(APNS_CERT_FILE="/foo.pem"), mock.patch(
"apns2.client.APNsClient" "apns2.client.APNsClient"
@ -1321,10 +1321,9 @@ class TestAPNs(PushNotificationTest):
client = get_apns_client() client = get_apns_client()
self.assertEqual(mock_client.return_value, client) self.assertEqual(mock_client.return_value, client)
finally: finally:
# Reset the values set by `get_apns_client` so that we don't # Reset the cache for `get_apns_client` so that we don't
# leak changes to the rest of the world. # leak changes to the rest of the world.
zerver.lib.push_notifications._apns_client_initialized = False zerver.lib.push_notifications.get_apns_client.cache_clear()
zerver.lib.push_notifications._apns_client = None
def test_not_configured(self) -> None: def test_not_configured(self) -> None:
self.setup_apns_tokens() self.setup_apns_tokens()