Skip to content

Commit f86b9f0

Browse files
committed
Use async with self.lock rather than explicit acquire/release.
Fixes: #42.
1 parent 3f0c9d0 commit f86b9f0

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

aioapns/connection.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -354,33 +354,31 @@ async def acquire(self) -> APNsBaseClientProtocol:
354354
for connection in self.connections:
355355
if not connection.is_busy:
356356
return connection
357-
else:
358-
await self._lock.acquire()
357+
358+
async with self._lock:
359359
for connection in self.connections:
360360
if not connection.is_busy:
361-
self._lock.release()
362361
return connection
363362
if len(self.connections) < self.max_connections:
364363
try:
365364
connection = await self.create_connection()
366365
except Exception as e:
367366
logger.error("Could not connect to server: %s", str(e))
368-
self._lock.release()
369367
raise ConnectionError()
370368
self.connections.append(connection)
371369
logger.info(
372370
"Connection established (total: %d)", len(self.connections)
373371
)
374-
self._lock.release()
375372
return connection
376-
else:
377-
self._lock.release()
378-
logger.warning("Pool is busy, wait...")
379-
while True:
380-
await asyncio.sleep(0.01)
381-
for connection in self.connections:
382-
if not connection.is_busy:
383-
return connection
373+
374+
logger.warning(
375+
"Pool is completely busy and has hit max connections, retrying..."
376+
)
377+
while True:
378+
await asyncio.sleep(0.01)
379+
for connection in self.connections:
380+
if not connection.is_busy:
381+
return connection
384382

385383
async def send_notification(
386384
self, request: NotificationRequest

0 commit comments

Comments
 (0)