Skip to content

Commit 2edd3cb

Browse files
authored
Ensure WebSocket max message size is not limited (#153)
1 parent 60a4238 commit 2edd3cb

File tree

4 files changed

+12
-38
lines changed

4 files changed

+12
-38
lines changed

src/surrealdb/__init__.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,10 @@ def __call__(cls, *args, **kwargs):
2828

2929
constructed_url = Url(url)
3030

31-
32-
# Extract `max_size` with a default if not explicitly provided
33-
max_size = kwargs.get("max_size", 2 ** 20)
34-
3531
if constructed_url.scheme == UrlScheme.HTTP or constructed_url.scheme == UrlScheme.HTTPS:
3632
return AsyncHttpSurrealConnection(url=url)
3733
elif constructed_url.scheme == UrlScheme.WS or constructed_url.scheme == UrlScheme.WSS:
38-
return AsyncWsSurrealConnection(url=url, max_size=max_size)
34+
return AsyncWsSurrealConnection(url=url)
3935
else:
4036
raise ValueError(f"Unsupported protocol in URL: {url}. Use 'ws://' or 'http://'.")
4137

@@ -54,32 +50,28 @@ def __call__(cls, *args, **kwargs):
5450

5551
constructed_url = Url(url)
5652

57-
58-
# Extract `max_size` with a default if not explicitly provided
59-
max_size = kwargs.get("max_size", 2 ** 20)
60-
6153
if constructed_url.scheme == UrlScheme.HTTP or constructed_url.scheme == UrlScheme.HTTPS:
6254
return BlockingHttpSurrealConnection(url=url)
6355
elif constructed_url.scheme == UrlScheme.WS or constructed_url.scheme == UrlScheme.WSS:
64-
return BlockingWsSurrealConnection(url=url, max_size=max_size)
56+
return BlockingWsSurrealConnection(url=url)
6557
else:
6658
raise ValueError(f"Unsupported protocol in URL: {url}. Use 'ws://' or 'http://'.")
6759

68-
def Surreal(url: Optional[str] = None, max_size: int = 2 ** 20) -> Union[BlockingWsSurrealConnection, BlockingHttpSurrealConnection]:
60+
def Surreal(url: Optional[str] = None) -> Union[BlockingWsSurrealConnection, BlockingHttpSurrealConnection]:
6961
constructed_url = Url(url)
7062
if constructed_url.scheme == UrlScheme.HTTP or constructed_url.scheme == UrlScheme.HTTPS:
7163
return BlockingHttpSurrealConnection(url=url)
7264
elif constructed_url.scheme == UrlScheme.WS or constructed_url.scheme == UrlScheme.WSS:
73-
return BlockingWsSurrealConnection(url=url, max_size=max_size)
65+
return BlockingWsSurrealConnection(url=url)
7466
else:
7567
raise ValueError(f"Unsupported protocol in URL: {url}. Use 'ws://' or 'http://'.")
7668

7769

78-
def AsyncSurreal(url: Optional[str] = None, max_size: int = 2 ** 20) -> Union[AsyncWsSurrealConnection, AsyncHttpSurrealConnection]:
70+
def AsyncSurreal(url: Optional[str] = None) -> Union[AsyncWsSurrealConnection, AsyncHttpSurrealConnection]:
7971
constructed_url = Url(url)
8072
if constructed_url.scheme == UrlScheme.HTTP or constructed_url.scheme == UrlScheme.HTTPS:
8173
return AsyncHttpSurrealConnection(url=url)
8274
elif constructed_url.scheme == UrlScheme.WS or constructed_url.scheme == UrlScheme.WSS:
83-
return AsyncWsSurrealConnection(url=url, max_size=max_size)
75+
return AsyncWsSurrealConnection(url=url)
8476
else:
8577
raise ValueError(f"Unsupported protocol in URL: {url}. Use 'ws://' or 'http://'.")

src/surrealdb/connections/async_http.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ class AsyncHttpSurrealConnection(AsyncTemplate, UtilsMixin):
2222
2323
Attributes:
2424
url: The URL of the database to process queries for.
25-
max_size: The maximum size of the connection payload.
2625
id: The ID of the connection.
2726
"""
2827

src/surrealdb/connections/async_ws.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,27 @@ class AsyncWsSurrealConnection(AsyncTemplate, UtilsMixin):
2323
"""
2424
A single async connection to a SurrealDB instance. To be used once and discarded.
2525
26-
# Notes
27-
A new connection is created for each query. This is because the async websocket connection is
28-
dropped
29-
3026
Attributes:
3127
url: The URL of the database to process queries for.
3228
user: The username to login on.
3329
password: The password to login on.
3430
namespace: The namespace that the connection will stick to.
3531
database: The database that the connection will stick to.
36-
max_size: The maximum size of the connection.
3732
id: The ID of the connection.
3833
"""
3934
def __init__(
4035
self,
4136
url: str,
42-
max_size: int = 2 ** 20,
4337
) -> None:
4438
"""
4539
The constructor for the AsyncSurrealConnection class.
4640
4741
:param url: The URL of the database to process queries for.
48-
:param max_size: The maximum size of the connection.
4942
"""
5043
self.url: Url = Url(url)
5144
self.raw_url: str = f"{self.url.raw_url}/rpc"
5245
self.host: str = self.url.hostname
5346
self.port: int = self.url.port
54-
self.max_size: int = max_size
5547
self.id: str = str(uuid.uuid4())
5648
self.token: Optional[str] = None
5749
self.socket = None
@@ -64,19 +56,17 @@ async def _send(self, message: RequestMessage, process: str, bypass: bool = Fals
6456
self.check_response_for_error(response, process)
6557
return response
6658

67-
async def connect(self, url: Optional[str] = None, max_size: Optional[int] = None) -> None:
59+
async def connect(self, url: Optional[str] = None) -> None:
6860
# overwrite params if passed in
6961
if url is not None:
7062
self.url = Url(url)
7163
self.raw_url: str = f"{self.url.raw_url}/rpc"
7264
self.host: str = self.url.hostname
7365
self.port: int = self.url.port
74-
if max_size is not None:
75-
self.max_size = max_size
7666
if self.socket is None:
7767
self.socket = await websockets.connect(
7868
self.raw_url,
79-
max_size=self.max_size,
69+
max_size=None,
8070
subprotocols=[websockets.Subprotocol("cbor")]
8171
)
8272

@@ -361,7 +351,7 @@ async def __aenter__(self) -> "AsyncWsSurrealConnection":
361351
"""
362352
self.socket = await websockets.connect(
363353
self.raw_url,
364-
max_size=self.max_size,
354+
max_size=None,
365355
subprotocols=[websockets.Subprotocol("cbor")]
366356
)
367357
return self

src/surrealdb/connections/blocking_ws.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,25 @@ class BlockingWsSurrealConnection(SyncTemplate, UtilsMixin):
2222
"""
2323
A single blocking connection to a SurrealDB instance. To be used once and discarded.
2424
25-
# Notes
26-
A new connection is created for each query. This is because the WebSocket connection is
27-
dropped after the query is completed.
28-
2925
Attributes:
3026
url: The URL of the database to process queries for.
3127
user: The username to login on.
3228
password: The password to login on.
3329
namespace: The namespace that the connection will stick to.
3430
database: The database that the connection will stick to.
35-
max_size: The maximum size of the connection.
3631
id: The ID of the connection.
3732
"""
3833

39-
def __init__(self, url: str, max_size: int = 2 ** 20) -> None:
34+
def __init__(self, url: str) -> None:
4035
"""
4136
The constructor for the BlockingWsSurrealConnection class.
4237
4338
:param url: (str) the URL of the database to process queries for.
44-
:param max_size: (int) The maximum size of the connection.
4539
"""
4640
self.url: Url = Url(url)
4741
self.raw_url: str = f"{self.url.raw_url}/rpc"
4842
self.host: str = self.url.hostname
4943
self.port: int = self.url.port
50-
self.max_size: int = max_size
5144
self.id: str = str(uuid.uuid4())
5245
self.token: Optional[str] = None
5346
self.socket = None
@@ -56,7 +49,7 @@ def _send(self, message: RequestMessage, process: str, bypass: bool = False) ->
5649
if self.socket is None:
5750
self.socket = ws_sync.connect(
5851
self.raw_url,
59-
max_size=self.max_size,
52+
max_size=None,
6053
subprotocols=[websockets.Subprotocol("cbor")],
6154
)
6255
self.socket.send(message.WS_CBOR_DESCRIPTOR)
@@ -361,7 +354,7 @@ def __enter__(self) -> "BlockingWsSurrealConnection":
361354
"""
362355
self.socket = ws_sync.connect(
363356
self.raw_url,
364-
max_size=self.max_size,
357+
max_size=None,
365358
subprotocols=[websockets.Subprotocol("cbor")]
366359
)
367360
return self

0 commit comments

Comments
 (0)