From 08168c61ece2fff1ee6e5cc8ce54ad8c47657d96 Mon Sep 17 00:00:00 2001 From: nggit Date: Fri, 15 Nov 2024 11:08:24 +0700 Subject: [PATCH] release 0.0.774 (#152) * small refactor * add Application alias --------- Co-authored-by: nggit <12218311+nggit@users.noreply.github.com> --- README.md | 7 +++---- hello.py | 4 ++-- tests/http_server.py | 4 ++-- tests/test_tremolo_objects.py | 4 ++-- tremolo/__init__.py | 4 ++-- tremolo/lib/http_protocol.py | 33 ++++++++++++++------------------- websocket_chat.py | 12 ++++++------ 7 files changed, 31 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index a9488ea..e7e0cdf 100644 --- a/README.md +++ b/README.md @@ -21,8 +21,7 @@ You can take advantage of this to serve/generate big files efficiently: @app.route('/my/url/speedtest.bin') async def my_big_data(**server): response = server['response'] - # buffer_size = 16384 - buffer_size = server['context'].options['buffer_size'] + buffer_size = 16384 response.set_content_type('application/octet-stream') @@ -55,9 +54,9 @@ With only **3k** lines of code, with **no dependencies** other than the [Python Here is a complete *hello world* example in case you missed the usual `return`. ```python -from tremolo import Tremolo +from tremolo import Application -app = Tremolo() +app = Application() @app.route('/hello') async def hello_world(**server): diff --git a/hello.py b/hello.py index 95b0e81..b77a42a 100644 --- a/hello.py +++ b/hello.py @@ -1,8 +1,8 @@ #!/usr/bin/env python3 -from tremolo import Tremolo +from tremolo import Application -app = Tremolo() +app = Application() @app.route('/hello') diff --git a/tests/http_server.py b/tests/http_server.py index 621e32a..b16ecf7 100644 --- a/tests/http_server.py +++ b/tests/http_server.py @@ -10,7 +10,7 @@ # makes imports relative from the repo directory sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) -from tremolo import Tremolo # noqa: E402 +from tremolo import Application # noqa: E402 from tremolo.exceptions import BadRequest # noqa: E402 from tremolo.utils import memory_usage # noqa: E402 @@ -18,7 +18,7 @@ HTTP_PORT = 28000 TEST_FILE = __file__ -app = Tremolo() +app = Application() @app.on_worker_start # priority=999 (low) diff --git a/tests/test_tremolo_objects.py b/tests/test_tremolo_objects.py index 9a42b25..30d8ecc 100644 --- a/tests/test_tremolo_objects.py +++ b/tests/test_tremolo_objects.py @@ -8,7 +8,7 @@ # makes imports relative from the repo directory sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) -from tremolo import Tremolo # noqa: E402 +from tremolo import Application # noqa: E402 from tremolo.exceptions import BadRequest # noqa: E402 from tremolo.lib.connections import KeepAliveConnections # noqa: E402 from tremolo.lib.contexts import ConnectionContext # noqa: E402 @@ -16,7 +16,7 @@ from tests.http_server import HTTP_PORT # noqa: E402 from tests.utils import function # noqa: E402 -app = Tremolo() +app = Application() class TestTremoloObjects(unittest.TestCase): diff --git a/tremolo/__init__.py b/tremolo/__init__.py index 5700589..6301303 100644 --- a/tremolo/__init__.py +++ b/tremolo/__init__.py @@ -1,6 +1,6 @@ -__version__ = '0.0.773' +__version__ = '0.0.774' -from .tremolo import Tremolo # noqa: E402 +from .tremolo import Tremolo, Tremolo as Application # noqa: E402,F401 from . import exceptions # noqa: E402,F401 diff --git a/tremolo/lib/http_protocol.py b/tremolo/lib/http_protocol.py index c6895a2..cadcfd9 100644 --- a/tremolo/lib/http_protocol.py +++ b/tremolo/lib/http_protocol.py @@ -19,6 +19,8 @@ from .queue import Queue from .websocket import WebSocket +_DEFAULT_QUEUE = (None, None) + class HTTPProtocol(asyncio.Protocol): __slots__ = ('globals', @@ -26,6 +28,7 @@ class HTTPProtocol(asyncio.Protocol): 'options', 'loop', 'logger', + 'fileno', 'queue', 'request', 'response', @@ -40,7 +43,8 @@ def __init__(self, context, loop=None, logger=None, **kwargs): self.options = kwargs self.loop = loop self.logger = logger - self.queue = [None, None] + self.fileno = -1 + self.queue = _DEFAULT_QUEUE self.request = None self.response = None self.handler = None @@ -79,19 +83,12 @@ def handle_task_done(self, task): def connection_made(self, transport): self.context.update(transport=transport) - fileno = self.context.socket.fileno() - - if fileno in self.globals.queues: - self.queue[0], self.queue[1] = self.globals.queues[fileno] + self.fileno = self.context.socket.fileno() - if self.queue[0].qsize() or self.queue[0].qsize(): - self.logger.error('uncleaned queue in fileno: %d', fileno) - self.abort() - return - else: - self.queue[0] = Queue() - self.queue[1] = Queue() - self.globals.queues[fileno] = tuple(self.queue) + try: + self.queue = self.globals.queues.pop(self.fileno) + except KeyError: + self.queue = [Queue(), Queue()] self._waiters['request'] = self.loop.create_future() @@ -514,13 +511,11 @@ def connection_lost(self, _): except Exception as exc: self.print_exception(exc, 'connection_lost') - for queue in self.queue: - if queue is None: - break + if self.queue is not _DEFAULT_QUEUE: + if self.queue[0].clear() and self.queue[1].clear(): + self.globals.queues[self.fileno] = self.queue - queue.clear() - else: - self.queue[0] = self.queue[1] = None + self.queue = _DEFAULT_QUEUE self.context.update(transport=None, socket=None) self.request = None diff --git a/websocket_chat.py b/websocket_chat.py index 5b71da8..2b0b11f 100644 --- a/websocket_chat.py +++ b/websocket_chat.py @@ -2,10 +2,10 @@ import time -from tremolo import Tremolo +from tremolo import Application from tremolo.exceptions import BadRequest -app = Tremolo() +app = Application() @app.on_request @@ -21,7 +21,7 @@ async def middleware_handler(**server): async def ws_handler(websocket=None, request=None, stream=False, **_): """A hybrid handler. - Normally, you should separate http:// and ws:// respectively. + Normally, you should separate the http:// and ws:// handlers individually. """ if websocket is not None: # an upgrade request is received. @@ -32,8 +32,8 @@ async def ws_handler(websocket=None, request=None, stream=False, **_): message = await websocket.receive() # send back the received message await websocket.send( - '[{:s}] Guest{:d}: {:s}'.format( - time.strftime('%H:%M:%S'), request.client[1], message) + '[%s] Guest%s: %s' % (time.strftime('%H:%M:%S'), + request.client[1], message) ) # not an upgrade request. show the html page @@ -51,7 +51,7 @@ async def ws_handler(websocket=None, request=None, stream=False, **_): ws_scheme = b'ws' if request.scheme == b'https': - ws_scheme = b'wss' + ws_scheme += b's' yield b"\ var socket = new WebSocket('%s://%s/');" % (ws_scheme, request.host)