@@ -117,6 +117,7 @@ async def open_websocket(
117
117
extra_headers : Optional [list [tuple [bytes ,bytes ]]] = None ,
118
118
message_queue_size : int = MESSAGE_QUEUE_SIZE ,
119
119
max_message_size : int = MAX_MESSAGE_SIZE ,
120
+ receive_buffer_size : Union [None , int ] = RECEIVE_BYTES ,
120
121
connect_timeout : float = CONN_TIMEOUT ,
121
122
disconnect_timeout : float = CONN_TIMEOUT
122
123
) -> AsyncGenerator [WebSocketConnection , None ]:
@@ -144,6 +145,9 @@ async def open_websocket(
144
145
:param int max_message_size: The maximum message size as measured by
145
146
``len()``. If a message is received that is larger than this size,
146
147
then the connection is closed with code 1009 (Message Too Big).
148
+ :param Optional[int] receive_buffer_size: The buffer size we use to
149
+ receive messages internally. None to let trio choose. Defaults
150
+ to 4 KiB.
147
151
:param float connect_timeout: The number of seconds to wait for the
148
152
connection before timing out.
149
153
:param float disconnect_timeout: The number of seconds to wait when closing
@@ -182,7 +186,8 @@ async def _open_connection(nursery: trio.Nursery) -> WebSocketConnection:
182
186
resource , use_ssl = use_ssl , subprotocols = subprotocols ,
183
187
extra_headers = extra_headers ,
184
188
message_queue_size = message_queue_size ,
185
- max_message_size = max_message_size )
189
+ max_message_size = max_message_size ,
190
+ receive_buffer_size = receive_buffer_size )
186
191
except trio .TooSlowError :
187
192
raise ConnectionTimeout from None
188
193
except OSError as e :
@@ -311,6 +316,7 @@ async def connect_websocket(
311
316
extra_headers : list [tuple [bytes , bytes ]] | None = None ,
312
317
message_queue_size : int = MESSAGE_QUEUE_SIZE ,
313
318
max_message_size : int = MAX_MESSAGE_SIZE ,
319
+ receive_buffer_size : Union [None , int ] = RECEIVE_BYTES ,
314
320
) -> WebSocketConnection :
315
321
'''
316
322
Return an open WebSocket client connection to a host.
@@ -339,6 +345,9 @@ async def connect_websocket(
339
345
:param int max_message_size: The maximum message size as measured by
340
346
``len()``. If a message is received that is larger than this size,
341
347
then the connection is closed with code 1009 (Message Too Big).
348
+ :param Optional[int] receive_buffer_size: The buffer size we use to
349
+ receive messages internally. None to let trio choose. Defaults
350
+ to 4 KiB.
342
351
:rtype: WebSocketConnection
343
352
'''
344
353
if use_ssl is True :
@@ -368,7 +377,8 @@ async def connect_websocket(
368
377
path = resource ,
369
378
client_subprotocols = subprotocols , client_extra_headers = extra_headers ,
370
379
message_queue_size = message_queue_size ,
371
- max_message_size = max_message_size )
380
+ max_message_size = max_message_size ,
381
+ receive_buffer_size = receive_buffer_size )
372
382
nursery .start_soon (connection ._reader_task )
373
383
await connection ._open_handshake .wait ()
374
384
return connection
@@ -384,6 +394,7 @@ def open_websocket_url(
384
394
max_message_size : int = MAX_MESSAGE_SIZE ,
385
395
connect_timeout : float = CONN_TIMEOUT ,
386
396
disconnect_timeout : float = CONN_TIMEOUT ,
397
+ receive_buffer_size : Union [None , int ] = RECEIVE_BYTES ,
387
398
) -> AbstractAsyncContextManager [WebSocketConnection ]:
388
399
'''
389
400
Open a WebSocket client connection to a URL.
@@ -407,6 +418,9 @@ def open_websocket_url(
407
418
:param int max_message_size: The maximum message size as measured by
408
419
``len()``. If a message is received that is larger than this size,
409
420
then the connection is closed with code 1009 (Message Too Big).
421
+ :param Optional[int] receive_buffer_size: The buffer size we use to
422
+ receive messages internally. None to let trio choose. Defaults
423
+ to 4 KiB.
410
424
:param float connect_timeout: The number of seconds to wait for the
411
425
connection before timing out.
412
426
:param float disconnect_timeout: The number of seconds to wait when closing
@@ -420,6 +434,7 @@ def open_websocket_url(
420
434
subprotocols = subprotocols , extra_headers = extra_headers ,
421
435
message_queue_size = message_queue_size ,
422
436
max_message_size = max_message_size ,
437
+ receive_buffer_size = receive_buffer_size ,
423
438
connect_timeout = connect_timeout , disconnect_timeout = disconnect_timeout )
424
439
425
440
@@ -432,6 +447,7 @@ async def connect_websocket_url(
432
447
extra_headers : list [tuple [bytes , bytes ]] | None = None ,
433
448
message_queue_size : int = MESSAGE_QUEUE_SIZE ,
434
449
max_message_size : int = MAX_MESSAGE_SIZE ,
450
+ receive_buffer_size : Union [None , int ] = RECEIVE_BYTES ,
435
451
) -> WebSocketConnection :
436
452
'''
437
453
Return an open WebSocket client connection to a URL.
@@ -457,13 +473,17 @@ async def connect_websocket_url(
457
473
:param int max_message_size: The maximum message size as measured by
458
474
``len()``. If a message is received that is larger than this size,
459
475
then the connection is closed with code 1009 (Message Too Big).
476
+ :param Optional[int] receive_buffer_size: The buffer size we use to
477
+ receive messages internally. None to let trio choose. Defaults
478
+ to 4 KiB.
460
479
:rtype: WebSocketConnection
461
480
'''
462
481
host , port , resource , return_ssl_context = _url_to_host (url , ssl_context )
463
482
return await connect_websocket (nursery , host , port , resource ,
464
483
use_ssl = return_ssl_context , subprotocols = subprotocols ,
465
484
extra_headers = extra_headers , message_queue_size = message_queue_size ,
466
- max_message_size = max_message_size )
485
+ max_message_size = max_message_size ,
486
+ receive_buffer_size = receive_buffer_size )
467
487
468
488
469
489
def _url_to_host (
@@ -520,6 +540,7 @@ async def wrap_client_stream(
520
540
extra_headers : list [tuple [bytes , bytes ]] | None = None ,
521
541
message_queue_size : int = MESSAGE_QUEUE_SIZE ,
522
542
max_message_size : int = MAX_MESSAGE_SIZE ,
543
+ receive_buffer_size : Union [None , int ] = RECEIVE_BYTES ,
523
544
) -> WebSocketConnection :
524
545
'''
525
546
Wrap an arbitrary stream in a WebSocket connection.
@@ -544,14 +565,18 @@ async def wrap_client_stream(
544
565
:param int max_message_size: The maximum message size as measured by
545
566
``len()``. If a message is received that is larger than this size,
546
567
then the connection is closed with code 1009 (Message Too Big).
568
+ :param Optional[int] receive_buffer_size: The buffer size we use to
569
+ receive messages internally. None to let trio choose. Defaults
570
+ to 4 KiB.
547
571
:rtype: WebSocketConnection
548
572
'''
549
573
connection = WebSocketConnection (stream ,
550
574
WSConnection (ConnectionType .CLIENT ),
551
575
host = host , path = resource ,
552
576
client_subprotocols = subprotocols , client_extra_headers = extra_headers ,
553
577
message_queue_size = message_queue_size ,
554
- max_message_size = max_message_size )
578
+ max_message_size = max_message_size ,
579
+ receive_buffer_size = receive_buffer_size )
555
580
nursery .start_soon (connection ._reader_task )
556
581
await connection ._open_handshake .wait ()
557
582
return connection
@@ -562,6 +587,7 @@ async def wrap_server_stream(
562
587
stream : trio .abc .Stream ,
563
588
message_queue_size : int = MESSAGE_QUEUE_SIZE ,
564
589
max_message_size : int = MAX_MESSAGE_SIZE ,
590
+ receive_buffer_size : Union [None , int ] = RECEIVE_BYTES ,
565
591
) -> WebSocketRequest :
566
592
'''
567
593
Wrap an arbitrary stream in a server-side WebSocket.
@@ -576,19 +602,24 @@ async def wrap_server_stream(
576
602
:param int max_message_size: The maximum message size as measured by
577
603
``len()``. If a message is received that is larger than this size,
578
604
then the connection is closed with code 1009 (Message Too Big).
605
+ :param Optional[int] receive_buffer_size: The buffer size we use to
606
+ receive messages internally. None to let trio choose. Defaults
607
+ to 4 KiB.
579
608
:type stream: trio.abc.Stream
580
609
:rtype: WebSocketRequest
581
610
'''
582
611
connection = WebSocketConnection (
583
612
stream ,
584
613
WSConnection (ConnectionType .SERVER ),
585
614
message_queue_size = message_queue_size ,
586
- max_message_size = max_message_size )
615
+ max_message_size = max_message_size ,
616
+ receive_buffer_size = receive_buffer_size )
587
617
nursery .start_soon (connection ._reader_task )
588
618
request = await connection ._get_request ()
589
619
return request
590
620
591
621
622
+
592
623
async def serve_websocket (
593
624
handler : Callable [[WebSocketRequest ], Awaitable [None ]],
594
625
host : str | bytes | None ,
@@ -598,6 +629,7 @@ async def serve_websocket(
598
629
handler_nursery : trio .Nursery | None = None ,
599
630
message_queue_size : int = MESSAGE_QUEUE_SIZE ,
600
631
max_message_size : int = MAX_MESSAGE_SIZE ,
632
+ receive_buffer_size : Union [None , int ] = RECEIVE_BYTES ,
601
633
connect_timeout : float = CONN_TIMEOUT ,
602
634
disconnect_timeout : float = CONN_TIMEOUT ,
603
635
task_status : trio .TaskStatus [WebSocketServer ] = trio .TASK_STATUS_IGNORED ,
@@ -630,6 +662,9 @@ async def serve_websocket(
630
662
:param int max_message_size: The maximum message size as measured by
631
663
``len()``. If a message is received that is larger than this size,
632
664
then the connection is closed with code 1009 (Message Too Big).
665
+ :param Optional[int] receive_buffer_size: The buffer size we use to
666
+ receive messages internally. None to let trio choose. Defaults
667
+ to 4 KiB.
633
668
:param float connect_timeout: The number of seconds to wait for a client
634
669
to finish connection handshake before timing out.
635
670
:param float disconnect_timeout: The number of seconds to wait for a client
@@ -658,6 +693,7 @@ async def serve_websocket(
658
693
handler_nursery = handler_nursery ,
659
694
message_queue_size = message_queue_size ,
660
695
max_message_size = max_message_size ,
696
+ receive_buffer_size = receive_buffer_size ,
661
697
connect_timeout = connect_timeout ,
662
698
disconnect_timeout = disconnect_timeout ,
663
699
)
@@ -957,7 +993,8 @@ def __init__(
957
993
client_subprotocols : Iterable [str ] | None = None ,
958
994
client_extra_headers : list [tuple [bytes , bytes ]] | None = None ,
959
995
message_queue_size : int = MESSAGE_QUEUE_SIZE ,
960
- max_message_size : int = MAX_MESSAGE_SIZE
996
+ max_message_size : int = MAX_MESSAGE_SIZE ,
997
+ receive_buffer_size : Union [None , int ] = RECEIVE_BYTES ,
961
998
) -> None :
962
999
'''
963
1000
Constructor.
@@ -984,6 +1021,9 @@ def __init__(
984
1021
:param int max_message_size: The maximum message size as measured by
985
1022
``len()``. If a message is received that is larger than this size,
986
1023
then the connection is closed with code 1009 (Message Too Big).
1024
+ :param Optional[int] receive_buffer_size: The buffer size we use to
1025
+ receive messages internally. None to let trio choose. Defaults
1026
+ to 4 KiB.
987
1027
'''
988
1028
# NOTE: The implementation uses _close_reason for more than an advisory
989
1029
# purpose. It's critical internal state, indicating when the
@@ -996,6 +1036,7 @@ def __init__(
996
1036
self ._message_size = 0
997
1037
self ._message_parts : List [Union [bytes , str ]] = []
998
1038
self ._max_message_size = max_message_size
1039
+ self ._receive_buffer_size : Optional [int ] = receive_buffer_size
999
1040
self ._reader_running = True
1000
1041
if ws_connection .client :
1001
1042
assert host is not None
@@ -1528,7 +1569,7 @@ async def _reader_task(self) -> None:
1528
1569
1529
1570
# Get network data.
1530
1571
try :
1531
- data = await self ._stream .receive_some (RECEIVE_BYTES )
1572
+ data = await self ._stream .receive_some (self . _receive_buffer_size )
1532
1573
except (trio .BrokenResourceError , trio .ClosedResourceError ):
1533
1574
await self ._abort_web_socket ()
1534
1575
break
@@ -1619,6 +1660,7 @@ def __init__(
1619
1660
handler_nursery : trio .Nursery | None = None ,
1620
1661
message_queue_size : int = MESSAGE_QUEUE_SIZE ,
1621
1662
max_message_size : int = MAX_MESSAGE_SIZE ,
1663
+ receive_buffer_size : Union [None , int ] = RECEIVE_BYTES ,
1622
1664
connect_timeout : float = CONN_TIMEOUT ,
1623
1665
disconnect_timeout : float = CONN_TIMEOUT ,
1624
1666
) -> None :
@@ -1637,6 +1679,9 @@ def __init__(
1637
1679
:param handler_nursery: An optional nursery to spawn connection tasks
1638
1680
inside of. If ``None``, then a new nursery will be created
1639
1681
internally.
1682
+ :param Optional[int] receive_buffer_size: The buffer size we use to
1683
+ receive messages internally. None to let trio choose. Defaults
1684
+ to 4 KiB.
1640
1685
:param float connect_timeout: The number of seconds to wait for a client
1641
1686
to finish connection handshake before timing out.
1642
1687
:param float disconnect_timeout: The number of seconds to wait for a client
@@ -1649,6 +1694,7 @@ def __init__(
1649
1694
self ._listeners = listeners
1650
1695
self ._message_queue_size = message_queue_size
1651
1696
self ._max_message_size = max_message_size
1697
+ self ._receive_buffer_size = receive_buffer_size
1652
1698
self ._connect_timeout = connect_timeout
1653
1699
self ._disconnect_timeout = disconnect_timeout
1654
1700
@@ -1741,7 +1787,8 @@ async def _handle_connection(self, stream: trio.abc.Stream) -> None:
1741
1787
connection = WebSocketConnection (stream ,
1742
1788
WSConnection (ConnectionType .SERVER ),
1743
1789
message_queue_size = self ._message_queue_size ,
1744
- max_message_size = self ._max_message_size )
1790
+ max_message_size = self ._max_message_size ,
1791
+ receive_buffer_size = self ._receive_buffer_size )
1745
1792
nursery .start_soon (connection ._reader_task )
1746
1793
with trio .move_on_after (self ._connect_timeout ) as connect_scope :
1747
1794
request = await connection ._get_request ()
0 commit comments