Metadata-Version: 2.4
Name: trevrpc
Version: 1.13.0
Summary: A client for TrevRPC, a protobuf RPC framework over QUIC
Keywords: rpc,quic,protobuf,qmux,asyncio
License-Expression: MIT
License-File: LICENSE
Classifier: Development Status :: 3 - Alpha
Classifier: Framework :: AsyncIO
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Internet
Classifier: Topic :: System :: Networking
Classifier: Typing :: Typed
Requires-Dist: aioquic>=1.9,<1.10
Requires-Dist: protobuf-py>=0.7,<0.8
Requires-Python: >=3.14
Project-URL: Homepage, https://trev.zip/llc/TrevRPC
Project-URL: Documentation, https://trev.zip/llc/TrevRPC/wiki/Python-Guide
Project-URL: Repository, https://trev.zip/llc/TrevRPC
Project-URL: Issues, https://trev.zip/llc/TrevRPC/issues
Project-URL: Changelog, https://trev.zip/llc/TrevRPC/releases
Description-Content-Type: text/markdown

# trevrpc

A Python client for [TrevRPC](https://trev.zip/llc/TrevRPC), a protobuf RPC framework over QUIC.

It calls TrevRPC services over native QUIC with [aioquic](https://github.com/aiortc/aioquic), and over
QMux over TLS on the server's TCP listener where UDP is blocked, on messages generated by
[protobuf-py](https://github.com/bufbuild/protobuf-py). It is a client only, asyncio only, and requires
Python 3.14.

See the [Python Guide](https://trev.zip/llc/TrevRPC/wiki/Python-Guide) for the whole API, and
[#666](https://trev.zip/llc/TrevRPC/issues/666) for what is still in progress.

## Installing

```sh
pip install trevrpc                              # or: uv add trevrpc
pip install protoc-gen-py protoc-gen-trevrpc-py  # the protoc plugins, for generating code
```

## Generating a client

`protoc-gen-py` generates the messages and `protoc-gen-trevrpc-py` the clients, next to them:

```yaml
# buf.gen.yaml
version: v2
plugins:
  - local: protoc-gen-py
    out: gen
  - local: protoc-gen-trevrpc-py
    out: gen
```

## Calling a service

```python
import trevrpc
from gen.greeter_pb import HelloRequest
from gen.greeter_trevrpc import GreeterClient

async with trevrpc.Channel("example.com:443") as channel:
    greeter = GreeterClient(channel)

    # unary
    reply = await greeter.say_hello(HelloRequest(name="Ada"), timeout=5)

    # server streaming
    async with greeter.watch(HelloRequest(name="Ada")) as replies:
        async for reply in replies:
            print(reply.message)

    # client streaming
    upload = greeter.upload()
    await upload.send_all(requests)
    reply = await upload.close_and_recv()

    # bidirectional streaming
    async with greeter.chat() as chat:
        await chat.send(HelloRequest(name="Ada"))
        reply = await chat.recv()
        await chat.close_send()
```

Every method takes the call options as keyword arguments: `timeout` (seconds), `metadata`,
`max_frame_size` (at most the channel's, 4 MiB by default; a larger value raises
`INVALID_ARGUMENT`), and the `on_response_headers` and `on_response_metadata` callbacks. A call that
does not end with an OK status raises `trevrpc.TrevRpcError`, which carries the status's code,
message, trailing metadata and details (`retry_delay`, `find_details()`).

## Connection metadata

Metadata that every call of a channel carries, such as a bearer token, can be set once on the
channel. Each connection carries it until the server confirms it stored it, and later calls refer to
it by ID instead of repeating it. A call's own metadata wins for a key both have.

```python
channel = trevrpc.Channel(
    "example.com:443", connection_metadata={"authorization": f"Bearer {token}"}
)
channel.set_connection_metadata({"authorization": f"Bearer {refreshed}"})  # later calls
```

## Transports

A channel connects over QUIC and falls back to QMux over TLS (ALPN `qmux-02.trevrpc-1`) when QUIC
has not connected within 300 ms, or failed: the first to connect wins. After the fallback won,
reconnects use it directly for 5 minutes before they race again. A call never moves between
connections. The channel options set this up:

```python
channel = trevrpc.Channel(
    "example.com:443",
    transport="auto",  # or "quic", or "tcp" for QMux over TLS only
    tcp_address="example.com:443",  # the server's TCP listener; the address by default
    fallback_delay=0.3,
    reprobe_interval=300,
)
```

`channel.transport` says which one the current connection uses (`"quic"` or `"qmux"`).

## Datagrams

On native QUIC, the messages of directions the schema marks `unreliable_requests` or
`unreliable_responses` may travel as QUIC datagrams: they can be lost or reordered, but never
duplicated, and applications that need ordering add sequence numbers. A request that does not fit a
datagram, or finds the connection's datagram queue full, goes on the stream, as every message does
over QMux over TLS. Datagram responses are held until the response headers have been read, and a
call whose responses ended waits up to 10 ms for the datagrams the server said it sent.

## Compression

Messages travel compressed with zstd where the server agrees, negotiated per connection; see
[Compression](https://trev.zip/llc/TrevRPC/wiki/Compression). The codec is the standard library's
`compression.zstd`, so it needs no extra dependency; on a Python built without libzstd, nothing is
compressed.

- **Dictionaries** trained on your messages compress every message of their types:

  ```python
  compression = trevrpc.Compression(
      [trevrpc.Dictionary(Path("metrics.dict").read_bytes(), ["example.v1.MetricSample"])],
      level=1,
  )
  channel = trevrpc.Channel("example.com:443", compression=compression)
  ```

- **Without a dictionary**, messages of 256 bytes or more compress on methods whose schema sets
  `compression: COMPRESSION_MESSAGE`, with no settings.
- **Stream compression** compresses each streamed direction of a `COMPRESSION_STREAM` method
  against the call's earlier messages, from its third message. Requests compress optimistically,
  without waiting for the server's answer; a server that refuses gets the call replayed without
  it, which the caller never sees. Each channel holds at most `stream_compression_memory` bytes
  for it (64 MiB by default; negative turns it off), about 350 KiB per direction it decodes and
  585 KiB per direction it compresses.

Message types with a field marked `debug_redact` never compress without a dictionary or with
stream compression.

## Flow control

Streaming calls read at most 32 messages or 1 MiB ahead of the caller, and writes wait for the
transport, so a slow side holds the other back through QUIC flow control. Leaving an `async for`
early does not end the call: use the call as an `async with` block, or call `aclose()`.
