Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
nggit committed Nov 25, 2023
1 parent 58b1c12 commit a6ac837
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 20 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ And other use cases…
## Features
Tremolo is only suitable for those who value [minimalism](https://en.wikipedia.org/wiki/Minimalism_%28computing%29) and stability over features.

With only **3k** lines of code, with no dependencies other than the [Python Standard Library](https://docs.python.org/3/library/index.html), it gives you:
With only **3k** lines of code, with **no dependencies** other than the [Python Standard Library](https://docs.python.org/3/library/index.html), it gives you:

* HTTP/1.x with [WebSocket support](https://nggit.github.io/tremolo-docs/websocket.html)
* Keep-Alive connections with [configurable limit](https://nggit.github.io/tremolo-docs/configuration.html#keepalive_connections)
* Stream chunked uploads
* [Stream multipart uploads](https://nggit.github.io/tremolo-docs/body.html#multipart)
* Download/upload speed throttling
* [Resumable downloads](https://nggit.github.io/tremolo-docs/resumable-downloads.html)
* Framework features; routing, middleware, etc
* Framework features; routing, middleware, etc.
* ASGI server
* PyPy compatible

Expand Down Expand Up @@ -139,7 +139,7 @@ You will find that Tremolo is reasonably fast.
However, it should be noted that bottlenecks often occur on the application side.
Which means that in real-world usage, throughput reflects more on the application than the server.

## Misc
## Misc.
Tremolo utilizes `SO_REUSEPORT` (Linux 3.9+) to load balance worker processes.

```python
Expand Down
4 changes: 2 additions & 2 deletions tremolo/http_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(self, lock=None, **kwargs):

async def _connection_made(self):
for func, _ in self._middlewares['connect']:
if (await func(**self._server)):
if await func(**self._server):
break

async def _connection_lost(self, exc):
Expand All @@ -35,7 +35,7 @@ async def _connection_lost(self, exc):
while i > 0:
i -= 1

if (await self._middlewares['close'][i][0](**self._server)):
if await self._middlewares['close'][i][0](**self._server):
break
finally:
super().connection_lost(exc)
Expand Down
2 changes: 1 addition & 1 deletion tremolo/lib/h1parser/parse_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def getlist(self, name):

return result

return values.replace(b', ', b',').split(b',')
return values.replace(b', ', b',').split(b',', 100)

Check warning on line 16 in tremolo/lib/h1parser/parse_header.py

View check run for this annotation

Codecov / codecov/patch

tremolo/lib/h1parser/parse_header.py#L16

Added line #L16 was not covered by tests


class ParseHeader:
Expand Down
2 changes: 1 addition & 1 deletion tremolo/lib/http_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ async def handle_exception(self, exc):
if isinstance(data, str):
encoding = 'utf-8'

for v in exc.content_type.split(';'):
for v in exc.content_type.split(';', 100):
v = v.lstrip()

if v.startswith('charset='):
Expand Down
16 changes: 8 additions & 8 deletions tremolo/lib/http_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,12 @@ async def stream(self, raw=False):
if not paused:
try:
buf.extend(await agen.__anext__())
except StopAsyncIteration:
except StopAsyncIteration as exc:
if b'0\r\n' not in buf:
del buf[:]
raise BadRequest(
'bad chunked encoding: incomplete read'
)
) from exc

if unread_bytes > 0:
data = buf[:unread_bytes]
Expand Down Expand Up @@ -215,9 +215,9 @@ async def stream(self, raw=False):

try:
chunk_size = int(buf[:i].split(b';', 1)[0], 16)
except ValueError:
except ValueError as exc:

Check warning on line 218 in tremolo/lib/http_request.py

View check run for this annotation

Codecov / codecov/patch

tremolo/lib/http_request.py#L218

Added line #L218 was not covered by tests
del buf[:]
raise BadRequest('bad chunked encoding')
raise BadRequest('bad chunked encoding') from exc

Check warning on line 220 in tremolo/lib/http_request.py

View check run for this annotation

Codecov / codecov/patch

tremolo/lib/http_request.py#L220

Added line #L220 was not covered by tests

data = buf[i + 2:i + 2 + chunk_size]
unread_bytes = chunk_size - len(data)
Expand Down Expand Up @@ -321,8 +321,8 @@ async def files(self, limit=1024):

try:
boundary = ct['boundary'][-1].encode('latin-1')
except KeyError:
raise BadRequest('missing boundary')
except KeyError as exc:
raise BadRequest('missing boundary') from exc

Check warning on line 325 in tremolo/lib/http_request.py

View check run for this annotation

Codecov / codecov/patch

tremolo/lib/http_request.py#L324-L325

Added lines #L324 - L325 were not covered by tests

header = None
body = bytearray()
Expand All @@ -341,12 +341,12 @@ async def files(self, limit=1024):
if not paused:
try:
data = await self._read_instance.__anext__()
except StopAsyncIteration:
except StopAsyncIteration as exc:

Check warning on line 344 in tremolo/lib/http_request.py

View check run for this annotation

Codecov / codecov/patch

tremolo/lib/http_request.py#L344

Added line #L344 was not covered by tests
if header_size == -1 or body_size == -1:
del body[:]
raise BadRequest(
'malformed multipart/form-data: incomplete read'
)
) from exc

if header is None:
self._read_buf.extend(data)
Expand Down
4 changes: 2 additions & 2 deletions tremolo/lib/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ async def recv(self):
data = await task

self.protocol.queue[0].task_done()
except asyncio.CancelledError:
raise TimeoutError('recv timeout')
except asyncio.CancelledError as exc:
raise TimeoutError('recv timeout') from exc
finally:
timer.cancel()

Expand Down
7 changes: 4 additions & 3 deletions tremolo/lib/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@ async def receive(self):

try:
payload = await self.recv()
except TimeoutError:
raise WebSocketServerClosed('receive timeout', code=1000)
except TimeoutError as exc:
raise WebSocketServerClosed('receive timeout',

Check warning on line 114 in tremolo/lib/websocket.py

View check run for this annotation

Codecov / codecov/patch

tremolo/lib/websocket.py#L114

Added line #L114 was not covered by tests
code=1000) from exc
finally:
timer.cancel()

Expand Down Expand Up @@ -166,7 +167,7 @@ def _ping(self):
# ping only if this connection is still listed,
# otherwise let the recv timeout drop it
if self.protocol in self.protocol.options['_connections']:
return self.protocol.loop.create_task(self.ping())
self.protocol.loop.create_task(self.ping())

Check warning on line 170 in tremolo/lib/websocket.py

View check run for this annotation

Codecov / codecov/patch

tremolo/lib/websocket.py#L170

Added line #L170 was not covered by tests

async def ping(self, data=b''):
await self.send(data, opcode=9)
Expand Down

0 comments on commit a6ac837

Please sign in to comment.