Skip to content

Commit

Permalink
Merge pull request #40 from wsrpc/fix-frames-order
Browse files Browse the repository at this point in the history
Use lock to keep correct frames order
  • Loading branch information
mosquito committed Apr 2, 2021
2 parents 2279c5c + efcf375 commit df63507
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
13 changes: 13 additions & 0 deletions tests/test_frames_order.py
@@ -0,0 +1,13 @@
import asyncio

from wsrpc_aiohttp import WSRPCClient


async def test_frames_are_sent_in_correct_order(client: WSRPCClient):
async with client:
data = 'hello, world' * 1024
coros = [
client.call("ping", data=data)
for _ in range(100)
]
await asyncio.gather(*coros)
9 changes: 6 additions & 3 deletions wsrpc_aiohttp/websocket/client.py
@@ -1,4 +1,5 @@
import logging
from asyncio import Lock
from typing import Union, Optional

import aiohttp
Expand Down Expand Up @@ -29,6 +30,7 @@ def __init__(
self._session = session or aiohttp.ClientSession(
loop=self._loop, **kwargs
)
self.send_lock = Lock()

self.socket = None # type: SocketType
self.closed = False
Expand Down Expand Up @@ -74,9 +76,10 @@ async def _send(self, **kwargs):
if self.socket.closed:
raise aiohttp.ClientConnectionError("Connection was closed.")

return await self.socket.send_json(
kwargs, dumps=lambda x: dumps(x)
)
async with self.send_lock:
return await self.socket.send_json(
kwargs, dumps=lambda x: dumps(x)
)
except aiohttp.WebSocketError:
self._loop.create_task(self.close())
raise
Expand Down

0 comments on commit df63507

Please sign in to comment.