Metadata-Version: 2.4
Name: mini-aiohttp
Version: 0.1.0
Summary: A small, dependency-free async HTTP/1.1 client and server built on asyncio
License: MIT
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Provides-Extra: test
Requires-Dist: pytest>=7; extra == "test"
Requires-Dist: pytest-asyncio>=0.21; extra == "test"
Dynamic: license-file

# mini-aiohttp

A small, dependency-free async HTTP/1.1 client and server built on Python's `asyncio`.

## Features

- Async client with keep-alive connection pooling per host
- Async server with routing, keep-alive, and request body limits
- Connection cap: new connections get `503` when the limit is reached, so memory stays bounded
- Timeouts on requests and idle connections
- Chunked response parsing (client) and `Content-Length` request bodies (server)
- HTTPS on the client side

## Install

```bash
pip install .            # from source
pip install .[test]      # with test dependencies
```

## Quick start

```python
import asyncio
from mini_aiohttp import Server, Response, ClientSession

app = Server(max_connections=100_000)

@app.route("/")
async def index(req):
    return Response(200, "Hello")

@app.route("/echo", method="POST")
async def echo(req):
    return Response.json({"got": req.json()})

async def main():
    server = asyncio.create_task(app.serve_forever("127.0.0.1", 8080))
    await asyncio.sleep(0.2)
    async with ClientSession() as s:
        r = await s.get("http://127.0.0.1:8080/")
        print(r.status, r.body.decode())
    server.cancel()

asyncio.run(main())
```

## Running tests

```bash
pip install .[test]
pytest
```

## Scaling to many connections

A single Python process can hold tens of thousands of idle keep-alive connections. Reaching 100k needs OS and deployment tuning:

1. **File descriptor limit.** Each socket uses one. Raise it before starting: `ulimit -n 200000`. The server logs a warning if the limit is too low.
2. **Listen backlog.** Raise the kernel queue: `sysctl -w net.core.somaxconn=65535`. The server already requests a backlog of 4096.
3. **Client-side ports.** One client machine opening outbound connections to a single `ip:port` is limited by ephemeral ports (about 28k to 64k). For more, spread load across several source IPs or client hosts.
4. **Multiple processes.** Python runs one event loop per process. Start several workers with `Server.start(..., reuse_port=True)` on Linux to use all CPU cores.
5. **Measure it.** Test with a load tool such as `wrk`, `oha`, or `locust` on your own hardware before you rely on any number.

## Limits (by design, v0.1)

- No HTTPS server (put a reverse proxy such as nginx in front).
- No chunked request bodies on the server.
- No HTTP/2 or WebSockets.

## License

MIT
