Skip to content

Commit 3e31e2a

Browse files
krisctlprabhakk-mw
authored andcommitted
Increases the maximum websocket message size limit to 500 MB in matlab-proxy and matlab-proxy-manager.
fixes #57
1 parent 8e41745 commit 3e31e2a

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

matlab_proxy/app.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -586,8 +586,9 @@ async def matlab_view(req):
586586
and reqH.get(UPGRADE, "").lower() == "websocket"
587587
and req.method == "GET"
588588
):
589-
ws_server = web.WebSocketResponse()
590-
589+
ws_server = web.WebSocketResponse(
590+
max_msg_size=constants.MAX_WEBSOCKET_MESSAGE_SIZE_IN_MB, compress=True
591+
)
591592
await ws_server.prepare(req)
592593

593594
async with aiohttp.ClientSession(
@@ -600,6 +601,8 @@ async def matlab_view(req):
600601
try:
601602
async with client_session.ws_connect(
602603
matlab_base_url + req.path_qs,
604+
max_msg_size=constants.MAX_WEBSOCKET_MESSAGE_SIZE_IN_MB, # max websocket message size from MATLAB to browser
605+
compress=12, # enable websocket messages compression
603606
) as ws_client:
604607

605608
async def wsforward(ws_from, ws_to):
@@ -631,13 +634,24 @@ async def wsforward(ws_from, ws_to):
631634
await ws_to.close(
632635
code=ws_to.close_code, message=msg.extra
633636
)
637+
elif mt == aiohttp.WSMsgType.ERROR:
638+
logger.error(f"WebSocket error received: {msg}")
639+
if "exceeds limit" in str(msg.data):
640+
logger.error(
641+
f"Message too large: {msg.data}. Please refresh browser tab to reconnect."
642+
)
643+
break
634644
else:
635645
raise ValueError(f"Unexpected message type: {msg}")
636646

637647
await asyncio.wait(
638648
[
639-
asyncio.create_task(wsforward(ws_server, ws_client)),
640-
asyncio.create_task(wsforward(ws_client, ws_server)),
649+
asyncio.create_task(
650+
wsforward(ws_server, ws_client)
651+
), # browser to MATLAB
652+
asyncio.create_task(
653+
wsforward(ws_client, ws_server)
654+
), # MATLAB to browser
641655
],
642656
return_when=asyncio.FIRST_COMPLETED,
643657
)

matlab_proxy/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
CONNECTOR_SECUREPORT_FILENAME: Final[str] = "connector.securePort"
77
VERSION_INFO_FILE_NAME: Final[str] = "VersionInfo.xml"
88
MAX_HTTP_REQUEST_SIZE: Final[int] = 500_000_000 # 500MB
9+
MAX_WEBSOCKET_MESSAGE_SIZE_IN_MB: Final[int] = 500_000_000 # 500MB
910
MATLAB_LOGS_FILE_NAME: Final[str] = "matlab_logs.txt"
1011
USER_CODE_OUTPUT_FILE_NAME: Final[str] = "startup_code_output.txt"
1112

matlab_proxy_manager/web/app.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import matlab_proxy.util.mwi.environment_variables as mwi_env
1515
import matlab_proxy.util.system as mwi_sys
1616
import matlab_proxy_manager.lib.api as mpm_lib
17+
import matlab_proxy.constants as mp_constants
1718
from matlab_proxy_manager.utils import constants, helpers, logger
1819
from matlab_proxy_manager.utils import environment_variables as mpm_env
1920
from matlab_proxy_manager.utils.auth import authenticate_access_decorator
@@ -342,7 +343,9 @@ async def _forward_websocket_request(
342343
Returns:
343344
web.WebSocketResponse: The response from the backend server
344345
"""
345-
ws_server = web.WebSocketResponse()
346+
ws_server = web.WebSocketResponse(
347+
max_msg_size=mp_constants.MAX_WEBSOCKET_MESSAGE_SIZE_IN_MB, compress=True
348+
)
346349
await ws_server.prepare(req)
347350

348351
async with aiohttp.ClientSession(
@@ -351,7 +354,11 @@ async def _forward_websocket_request(
351354
connector=aiohttp.TCPConnector(ssl=False),
352355
) as client_session:
353356
try:
354-
async with client_session.ws_connect(proxy_url) as ws_client:
357+
async with client_session.ws_connect(
358+
proxy_url,
359+
max_msg_size=mp_constants.MAX_WEBSOCKET_MESSAGE_SIZE_IN_MB, # max websocket message size from MATLAB to browser
360+
compress=12, # enable websocket messages compression
361+
) as ws_client:
355362

356363
async def ws_forward(ws_src, ws_dest):
357364
async for msg in ws_src:
@@ -389,6 +396,13 @@ async def ws_forward(ws_src, ws_dest):
389396
await ws_dest.close(
390397
code=ws_dest.close_code, message=msg.extra
391398
)
399+
elif msg_type == aiohttp.WSMsgType.ERROR:
400+
log.error(f"WebSocket error received: {msg}")
401+
if "exceeds limit" in str(msg.data):
402+
log.error(
403+
f"Message too large: {msg.data}. Please refresh browser tab to reconnect."
404+
)
405+
break
392406
else:
393407
raise ValueError(f"Unexpected message type: {msg}")
394408

0 commit comments

Comments
 (0)