Skip to content

Commit

Permalink
Fix increase in latancy with small messages from websocket compressio…
Browse files Browse the repository at this point in the history
…n changes
  • Loading branch information
bdraco committed Nov 7, 2023
1 parent 79f5266 commit 3baf5bb
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions aiohttp/http_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ class WSCloseCode(IntEnum):

ALLOWED_CLOSE_CODES: Final[Set[int]] = {int(i) for i in WSCloseCode}

# For websockets, keeping latency low is extremely important as implementations
# generally expect to be able to send and receive messages quickly. We use a
# larger chunk size than the default to reduce the number of executor calls
# since the executor is a significant source of latency and overhead when
# the chunks are small. A size of 5KiB was chosen because it is also the
# same value python-zlib-ng choose to use as the threshold to release the GIL.

WEBSOCKET_MAX_SYNC_CHUNK_SIZE = 5 * 1024


class WSMsgType(IntEnum):
# websocket spec types
Expand Down Expand Up @@ -626,11 +635,17 @@ async def _send_frame(
if (compress or self.compress) and opcode < 8:
if compress:
# Do not set self._compress if compressing is for this frame
compressobj = ZLibCompressor(level=zlib.Z_BEST_SPEED, wbits=-compress)
compressobj = ZLibCompressor(
level=zlib.Z_BEST_SPEED,
wbits=-compress,
max_sync_chunk_size=WEBSOCKET_MAX_SYNC_CHUNK_SIZE,
)
else: # self.compress
if not self._compressobj:
self._compressobj = ZLibCompressor(
level=zlib.Z_BEST_SPEED, wbits=-self.compress
level=zlib.Z_BEST_SPEED,
wbits=-self.compress,
max_sync_chunk_size=WEBSOCKET_MAX_SYNC_CHUNK_SIZE,
)
compressobj = self._compressobj

Expand Down

0 comments on commit 3baf5bb

Please sign in to comment.