Skip to main content

A middleware for the FastStream framework to support message compression.

Project description

faststream-compressors

A middleware for the FastStream framework to support message compression.

⚠️ Note: RPC Limitation

Due to a bug in FastStream, middleware does not run after receiving a response from the broker, preventing message decompression when using RPC. I've reported this issue to the FastStream developers, and we're hoping for a fix soon.

In the meantime, you can register separate routers for RPC and Pub/Sub:

  • RPC Router: Enable only the DecompressionMiddleware.
  • Pub/Sub Router: Enable both DecompressionMiddleware and CompressionMiddleware.

Example

from faststream.nats import NatsBroker

from faststream_compressors.compressors import GzipCompressor, GzipDecompressor
from faststream_compressors.middlewares import CompressionMiddleware
from faststream_compressors.middlewares.nats import NatsDecompressionMiddleware


broker = NatsBroker(    
    middlewares=(
        # Compression methods used for compressing messages.
        # The order in which compressors are specified matters.
        CompressionMiddleware.make_middleware(compressors=GzipCompressor()),
        
        # Your other middlewares here

        # Compression methods used for decompressing messages.
        # The order does not matter here
        NatsDecompressionMiddleware.make_middleware(decompressors=GzipDecompressor()),
    )
)
Broker Is Supported? Middleware
NATS faststream_compressors.middlewares.nats.NatsDecompressionMiddleware
Other

You can submit a pull request to add support for decompression middleware for your broker. I expect that FastStream will update its middleware API soon, allowing us to create a universal middleware for each broker. For now, only NATS is supported.

Compression Method Is Supported? Compressor Extra Dependency
gzip faststream_compressors.compressors.GzipCompressor
faststream_compressors.compressors.GzipDecompressor
lz4 faststream_compressors.compressors.lz4.Lz4Compressor
faststream_compressors.compressors.lz4.Lz4Decompressor
faststream-compressors[lz4]
Other

You can submit a pull request to add support for your compression method or use your custom algorithm that adheres to the BaseCompressor interface.

from faststream import FastStream, Header
from faststream.nats import NatsBroker

from faststream_compressors.compressors import BaseCompressor
from faststream_compressors.middlewares import CompressionMiddleware
from faststream_compressors.middlewares.nats import NatsDecompressionMiddleware


class MyCompressor(BaseCompressor):
    ENCODING = "xor1"

    def __call__(self, data: bytes) -> bytes:
        return bytes(byte ^ 1 for byte in data)


broker = NatsBroker(
    middlewares=(
        CompressionMiddleware.make_middleware(compressors=MyCompressor()),
        NatsDecompressionMiddleware.make_middleware(decompressors=MyCompressor()),
    )
)
app = FastStream(broker)


@broker.subscriber("my-subject")
async def my_handler(data: str, encoding: str = Header("content-encoding")):
    print(data, encoding)


@app.after_startup
async def ping():
    await broker.publish("My secret message", "my-subject")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

faststream_compressors-0.2.1.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

faststream_compressors-0.2.1-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file faststream_compressors-0.2.1.tar.gz.

File metadata

  • Download URL: faststream_compressors-0.2.1.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.4 Darwin/24.0.0

File hashes

Hashes for faststream_compressors-0.2.1.tar.gz
Algorithm Hash digest
SHA256 493b5122d4b69265d322f30195adcfa2891b90b315be1b38b5ad3aeacd61bb2f
MD5 8dd99308958e34521348f5271ac222e3
BLAKE2b-256 c6feb553e2e57c903385a968e68c0b87563a65e15eb4a70fcfec28e2f9baf1c1

See more details on using hashes here.

File details

Details for the file faststream_compressors-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for faststream_compressors-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a6212ac6ac21e05df2fd7f26a3f49bed202865544575fac911f70ad0c8c8e36f
MD5 8a4978cb289e6888688086229c112afe
BLAKE2b-256 beab4fd754b5743420177563d7497d387302ca5cb07bdd55ca4e5e03dd5e0293

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page