Skip to content

Commit

Permalink
Employ asyncio locks for pipes to avoid drain errors in aiohttp
Browse files Browse the repository at this point in the history
  • Loading branch information
Humbedooh authored May 28, 2020
1 parent 44d8d40 commit 06fcd13
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions pypubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ def __init__(self, server, connection, request):
self.connection = connection
self.acl = {}
self.server = server
self.lock = asyncio.Lock()

# Set topics subscribed to
self.topics = [x for x in request.path.split('/') if x]
Expand Down Expand Up @@ -351,7 +352,8 @@ async def ping(self):
js = b"%s\n" % json.dumps({"stillalive": time.time()}).encode('utf-8')
if self.old_school:
js += b"\0"
await self.connection.write(js)
async with self.lock:
await self.connection.write(js)


class Payload:
Expand Down Expand Up @@ -391,9 +393,11 @@ async def publish(self, subscribers):
if all(el in self.topics for el in sub.topics):
try:
if sub.old_school:
await sub.connection.write(ojs)
async with sub.lock:
await sub.connection.write(ojs)
else:
await sub.connection.write(js)
async with sub.lock:
await sub.connection.write(js)
except Exception:
bad_subs.append(sub)
return bad_subs
Expand Down

0 comments on commit 06fcd13

Please sign in to comment.