Skip to content
This repository has been archived by the owner on Jul 30, 2024. It is now read-only.

Commit

Permalink
feat: add first version (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Nov 2, 2023
1 parent 5aafad9 commit 3dc8e58
Show file tree
Hide file tree
Showing 8 changed files with 621 additions and 10 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ jobs:
- "3.9"
- "3.10"
- "3.11"
- "3.12"
os:
- ubuntu-latest
- windows-latest
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,26 @@

Enable zlib_ng on aiohttp

zlib is be a bottleneck for aiohttp, especially for websocket connections. `aiohttp-zlib-ng` replaces usage of `zlib` in `aiohttp` with `zlib-ng` which is a drop-in faster replacement.

## Installation

Install this via pip (or your favourite package manager):

`pip install aiohttp-zlib-ng`

## Usage

Importing the module is enough to enable [zlib-ng](https://github.com/pycompression/python-zlib-ng) support in aiohttp.

```python

import aiohttp_zlib_ng

aiohttp_zlib_ng.disable_zlib_ng()
aiohttp_zlib_ng.enable_zlib_ng()
```

## Contributors ✨

Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
Expand Down
555 changes: 554 additions & 1 deletion poetry.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ packages = [

[tool.poetry.dependencies]
python = "^3.8"
aiohttp = ">=3.8.5"
zlib-ng = ">=0.2.0"

[tool.poetry.group.dev.dependencies]
pytest = "^7.0"
Expand Down
36 changes: 36 additions & 0 deletions src/aiohttp_zlib_ng/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,37 @@
__version__ = "0.0.0"

import importlib
import zlib as zlib_original

import aiohttp
from zlib_ng import zlib_ng as zlib_ng

TARGETS = (
"compression_utils",
"http_writer",
"http_websocket",
"http_writer",
"multipart",
"web_response",
)


def enable_zlib_ng() -> None:
"""Enable zlib-ng."""
for location in TARGETS:
try:
importlib.import_module(f"aiohttp.{location}")
except ImportError:
continue
if module := getattr(aiohttp, location, None):
module.zlib = zlib_ng


def disable_zlib_ng() -> None:
"""Disable zlib-ng and restore the original zlib."""
for location in TARGETS:
if module := getattr(aiohttp, location, None):
module.zlib = zlib_original


enable_zlib_ng()
3 changes: 0 additions & 3 deletions src/aiohttp_zlib_ng/main.py

This file was deleted.

15 changes: 15 additions & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import zlib as zlib_original

import aiohttp.http_websocket
from zlib_ng import zlib_ng as zlib_ng

from aiohttp_zlib_ng import disable_zlib_ng, enable_zlib_ng


def test_enable_disable():
"""Test enable/disable."""
assert aiohttp.http_websocket.zlib is zlib_ng
disable_zlib_ng()
assert aiohttp.http_websocket.zlib is zlib_original
enable_zlib_ng()
assert aiohttp.http_websocket.zlib is zlib_ng
5 changes: 0 additions & 5 deletions tests/test_main.py

This file was deleted.

0 comments on commit 3dc8e58

Please sign in to comment.