Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add send_timeout, close_timeout, and send_x timeout parameter #2537

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions aiohttp/web_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ def __bool__(self):
class WebSocketResponse(StreamResponse):

def __init__(self, *,
timeout=10.0, receive_timeout=None,
autoclose=True, autoping=True, heartbeat=None,
protocols=(), compress=True):
timeout=None, receive_timeout=None, send_timeout=None,
close_timeout=10.0, autoclose=True, autoping=True,
heartbeat=None, protocols=(), compress=True):
super().__init__(status=101)
self._protocols = protocols
self._ws_protocol = None
Expand All @@ -47,8 +47,9 @@ def __init__(self, *,
self._loop = None
self._waiting = None
self._exception = None
self._timeout = timeout
self._receive_timeout = receive_timeout
self._send_timeout = send_timeout
self._close_timeout = timeout or close_timeout
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please deprecate timeout.
Something like

if timeout is not None:
    warnings.warn("timeout parameter is deprecated, use close_timeout instead", DeprecationWarning)
    close_timeout = timeout

self._autoclose = autoclose
self._autoping = autoping
self._heartbeat = heartbeat
Expand Down Expand Up @@ -177,23 +178,27 @@ async def pong(self, message='b'):
raise RuntimeError('Call .prepare() first')
await self._writer.pong(message)

async def send_str(self, data):
async def send_str(self, data, *, timeout=None):
if self._writer is None:
raise RuntimeError('Call .prepare() first')
if not isinstance(data, str):
raise TypeError('data argument must be str (%r)' % type(data))
await self._writer.send(data, binary=False)
with async_timeout.timeout(timeout or self._send_timeout,
loop=self._loop):
await self._writer.send(data, binary=False)

async def send_bytes(self, data):
async def send_bytes(self, data, *, timeout=None):
if self._writer is None:
raise RuntimeError('Call .prepare() first')
if not isinstance(data, (bytes, bytearray, memoryview)):
raise TypeError('data argument must be byte-ish (%r)' %
type(data))
await self._writer.send(data, binary=True)
with async_timeout.timeout(timeout or self._send_timeout,
loop=self._loop):
await self._writer.send(data, binary=True)

async def send_json(self, data, *, dumps=json.dumps):
await self.send_str(dumps(data))
async def send_json(self, data, *, timeout=None, dumps=json.dumps):
await self.send_str(dumps(data), timeout=timeout)

async def write_eof(self):
if self._eof_sent:
Expand Down Expand Up @@ -233,7 +238,8 @@ async def close(self, *, code=1000, message=b''):
return True

try:
with async_timeout.timeout(self._timeout, loop=self._loop):
with async_timeout.timeout(self._close_timeout,
loop=self._loop):
msg = await self._reader.read()
except asyncio.CancelledError:
self._close_code = 1006
Expand Down
1 change: 1 addition & 0 deletions changes/2310.doc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Document the send_timeout, close_timeout, and the timeout parameters for the send_x calls (#2310)
1 change: 1 addition & 0 deletions changes/2310.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add send_timeout and close_timeout to WebSocketResponse along with a timeout parameter for the send_x calls (#2310)
21 changes: 17 additions & 4 deletions docs/web_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,8 @@ Response
WebSocketResponse
^^^^^^^^^^^^^^^^^

.. class:: WebSocketResponse(*, timeout=10.0, receive_timeout=None, \
.. class:: WebSocketResponse(*, timeout=None, receive_timeout=None, \
send_timeout=None, close_timeout=10.0, \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please describe added params below.

autoclose=True, autoping=True, heartbeat=None, \
protocols=(), compress=True)

Expand Down Expand Up @@ -979,12 +980,16 @@ WebSocketResponse

The method is converted into :term:`coroutine`

.. comethod:: send_str(data)
.. comethod:: send_str(data, *, timeout=None)

Send *data* to peer as :const:`~aiohttp.WSMsgType.TEXT` message.

:param str data: data to send.

:param timeout: timeout for `send` operation.

timeout value overrides response`s send_timeout attribute.

:raise RuntimeError: if connection is not started or closing

:raise TypeError: if data is not :class:`str`
Expand All @@ -993,12 +998,16 @@ WebSocketResponse

The method is converted into :term:`coroutine`

.. comethod:: send_bytes(data)
.. comethod:: send_bytes(data, *, timeout=None)

Send *data* to peer as :const:`~aiohttp.WSMsgType.BINARY` message.

:param data: data to send.

:param timeout: timeout for `send` operation.

timeout value overrides response`s send_timeout attribute.

:raise RuntimeError: if connection is not started or closing

:raise TypeError: if data is not :class:`bytes`,
Expand All @@ -1008,12 +1017,16 @@ WebSocketResponse

The method is converted into :term:`coroutine`

.. comethod:: send_json(data, *, dumps=json.dumps)
.. comethod:: send_json(data, *, timeout=None, dumps=json.dumps)

Send *data* to peer as JSON string.

:param data: data to send.

:param timeout: timeout for `send` operation.

timeout value overrides response`s send_timeout attribute.

:param callable dumps: any :term:`callable` that accepts an object and
returns a JSON string
(:func:`json.dumps` by default).
Expand Down