Skip to content

Commit

Permalink
Merge pull request #224 from SmithChart/client_max_size
Browse files Browse the repository at this point in the history
feat(LonaServer): Allow to set aiohttp client_max_size
  • Loading branch information
fscherf authored Jul 4, 2022
2 parents c929060 + fe7edb6 commit 9f4fc1c
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 23 deletions.
14 changes: 14 additions & 0 deletions doc/content/end-user-documentation/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,17 @@ Testing
.. setting::
:name: TEST_INPUT_EVENT_TIMEOUT
:path: lona.default_settings.TEST_INPUT_EVENT_TIMEOUT

Server
------

.. setting::
:name: AIOHTTP_CLIENT_MAX_SIZE
:path: lona.default_settings.AIOHTTP_CLIENT_MAX_SIZE

This value is used to set the ``client_max_size`` value for the aiohttp server.
It defines the maximum body size of a post request accepted by the server.
See
`aiohttp documentation <https://docs.aiohttp.org/en/stable/web_reference.html#aiohttp.web.Application>`_
for details.
The default value is set to the aiohttp default of ``1024**2`` Bytes.
4 changes: 1 addition & 3 deletions lona/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,20 +396,19 @@ def setup_server(
self.settings.STATIC_DIRS.insert(0, self.static_dir)

# setup server
self.aiohttp_app = Application(loop=loop)

settings_post_overrides = {
**self._get_settings_as_dict(),
**(settings_post_overrides or {}),
}

self.server = LonaServer(
app=self.aiohttp_app,
project_root=self.project_root,
settings_pre_overrides=settings_pre_overrides,
settings_post_overrides=settings_post_overrides,
routes=self.routes,
)
self.aiohttp_app = self.server._app

# setup worker pool
worker_pool = WorkerPool(
Expand Down Expand Up @@ -462,6 +461,5 @@ def run(
# start server
run_server(
args=server_args,
app=self.aiohttp_app,
server=self.server,
)
8 changes: 3 additions & 5 deletions lona/command_line/run_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import signal
import os

from aiohttp.web import Application, run_app
from aiohttp.web import run_app
import aiohttp

from lona.shell.shell import generate_shell_server, embed_shell
Expand All @@ -19,22 +19,20 @@
logger = logging.getLogger('lona')


def run_server(args, app=None, server=None):
def run_server(args, server=None):
loop = asyncio.get_event_loop()

# setup logging
log_formatter, log_filter = setup_logging(args)

# setup lona server
app = app or Application()

server = server or LonaServer(
app=app,
project_root=args.project_root,
settings_paths=args.settings,
settings_pre_overrides=args.settings_pre_overrides,
settings_post_overrides=args.settings_post_overrides,
)
app = server._app

worker_pool = WorkerPool(
settings=server.settings,
Expand Down
3 changes: 3 additions & 0 deletions lona/default_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,6 @@
# testing
TEST_VIEW_START_TIMEOUT = False
TEST_INPUT_EVENT_TIMEOUT = False

# server
AIOHTTP_CLIENT_MAX_SIZE = 1024**2
5 changes: 1 addition & 4 deletions lona/pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,8 @@ async def setup_lona_project_context(
def setup_aiohttp_app(loop: AbstractEventLoop) -> Application:
nonlocal server

aiohttp_app = Application(loop=loop)

# setup lona server
server = LonaServer(
app=aiohttp_app,
project_root=project_root or request.fspath,
settings_paths=settings or [],
settings_pre_overrides=settings_pre_overrides or {},
Expand All @@ -164,7 +161,7 @@ def setup_aiohttp_app(loop: AbstractEventLoop) -> Application:
server._loop = loop
server._worker_pool = WorkerPool(settings=server.settings)

return aiohttp_app
return server._app

client = await aiohttp_client(setup_aiohttp_app)

Expand Down
31 changes: 20 additions & 11 deletions lona/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@
import asyncio
import os

from aiohttp.web import WebSocketResponse, FileResponse, HTTPFound, Response
from aiohttp.web import (
WebSocketResponse,
FileResponse,
Application,
HTTPFound,
Response,
)
from typing_extensions import Literal
from aiohttp import WSMsgType
from jinja2 import Template
Expand Down Expand Up @@ -46,7 +52,7 @@


class LonaServer:
def __init__(self, app, project_root, settings_paths=None,
def __init__(self, project_root, settings_paths=None,
settings_pre_overrides=None, settings_post_overrides=None,
routes=None):

Expand All @@ -55,15 +61,7 @@ def __init__(self, app, project_root, settings_paths=None,
self._websocket_connections = []
self._loop = None
self._worker_pool = None

server_logger.debug("starting server in '%s'", project_root)

# setup aiohttp app
self._app = app
self._app['lona_server'] = self

self._app.on_startup.append(self._start)
self._app.on_shutdown.append(self._stop)
self._app: Application = None

# setup settings
server_logger.debug('setup settings')
Expand Down Expand Up @@ -96,6 +94,17 @@ def __init__(self, app, project_root, settings_paths=None,

self.settings.update(settings_post_overrides)

# setup aiohttp app
server_logger.debug("starting server in '%s'", project_root)

self._app = Application(
client_max_size=self.settings.AIOHTTP_CLIENT_MAX_SIZE,
)
self._app['lona_server'] = self

self._app.on_startup.append(self._start)
self._app.on_shutdown.append(self._stop)

# setup templating
server_logger.debug('setup templating')

Expand Down

0 comments on commit 9f4fc1c

Please sign in to comment.