Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
f add locking (d'oh)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrconlin committed Jun 25, 2020
1 parent d6e86e9 commit a047313
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions autopush/router/apns2.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def __init__(self, cert_file, key_file, topic,
self._max_connections = max_connections
self._max_retry = max_retry
self._max_conn_ttl = conn_ttl
self.lock = threading.Lock()
# `idle_connections` is a pool of potentially active connections.
# These may be forced closed by `_reap`. This ensures that no
# active connections would be forced closed by accident.
Expand Down Expand Up @@ -209,6 +210,7 @@ def _get_connection(self):
response_body="APNS busy, please retry")
self._in_use_connections += 1
try:
self.lock.acquire()
connection = self.idle_connections.pop()
if self.log:
self.log.debug("Got existing APNS connection")
Expand All @@ -219,19 +221,25 @@ def _get_connection(self):
self.port,
ssl_context=self.ssl_context,
force_proto='h2')
finally:
self.lock.release()


def _return_connection(self, connection):
self._in_use_connections = max(0, self._in_use_connections - 1)
if self.log:
self.log.debug("Done with APNS connection")
conn = APNSConnection(connection)
self.lock.acquire()
self.idle_connections.appendleft(conn)
self.lock.release()

def _reap(self, test=False):
while True:
if self.log:
self.log.debug("Reaping APNS connections")
runners = []
self.lock.acquire()
for connection in self.idle_connections:
if connection._last_used < (time.time() - self._max_conn_ttl):
if self.log:
Expand All @@ -243,6 +251,8 @@ def _reap(self, test=False):
self.idle_connections.remove(runner)
except ValueError:
pass
finally:
self.lock.release()
time.sleep(self._reap_sleep)
if test:
return

0 comments on commit a047313

Please sign in to comment.