Skip to content

Commit

Permalink
release 0.0.774 (#152)
Browse files Browse the repository at this point in the history
* small refactor

* add Application alias

---------

Co-authored-by: nggit <[email protected]>
  • Loading branch information
nggit and nggit authored Nov 15, 2024
1 parent fee7105 commit 08168c6
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 37 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions hello.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env python3

from tremolo import Tremolo
from tremolo import Application

app = Tremolo()
app = Application()


@app.route('/hello')
Expand Down
4 changes: 2 additions & 2 deletions tests/http_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
# 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

HTTP_HOST = '127.0.0.1'
HTTP_PORT = 28000
TEST_FILE = __file__

app = Tremolo()
app = Application()


@app.on_worker_start # priority=999 (low)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_tremolo_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
# 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
from tests import handlers, middlewares, hooks # noqa: E402
from tests.http_server import HTTP_PORT # noqa: E402
from tests.utils import function # noqa: E402

app = Tremolo()
app = Application()


class TestTremoloObjects(unittest.TestCase):
Expand Down
4 changes: 2 additions & 2 deletions tremolo/__init__.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down
33 changes: 14 additions & 19 deletions tremolo/lib/http_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@
from .queue import Queue
from .websocket import WebSocket

_DEFAULT_QUEUE = (None, None)


class HTTPProtocol(asyncio.Protocol):
__slots__ = ('globals',
'context',
'options',
'loop',
'logger',
'fileno',
'queue',
'request',
'response',
Expand All @@ -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
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions websocket_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 08168c6

Please sign in to comment.