Skip to content

Payload Compression

Overview

The Compression module provides an abstraction for compressing and decompressing data. Its primary use case is to act as a step in the Payload Processing Pipeline, where it can transparently compress message payloads before they are sent to a broker and decompress them on the consumer side. This reduces network bandwidth and storage costs.

How It Works

The module follows a provider pattern, with the GzipProvider being the default implementation. It is integrated into the PayloadProcessor via the CompressionStepAdapter. When "compression" is added as a step to the payload pipeline in your configuration, this adapter is automatically included in the processing chain.

Usage

Usage is typically declarative via configuration. See the Payload Processing Pipeline documentation for an example of how to add the compression step to your pipeline.

API Reference

nala.athomic.compression.protocol.CompressionProtocol

Bases: Protocol

Defines the contract for a data compression provider.

Any class that implements this protocol can be used by Athomic's components, such as the payload processing pipeline, to compress and decompress data. This allows for different compression algorithms (e.g., gzip, brotli) to be used interchangeably.

compress(data) async

Compresses the given byte string.

Parameters:

Name Type Description Default
data bytes

The raw byte string to be compressed.

required

Returns:

Name Type Description
bytes bytes

The compressed byte string.

decompress(data) async

Decompresses the given byte string.

Parameters:

Name Type Description Default
data bytes

The compressed byte string to be decompressed.

required

Returns:

Name Type Description
bytes bytes

The original, decompressed byte string.

Raises:

Type Description
Exception

If the data is corrupt or in an invalid format.

nala.athomic.compression.providers.gzip_provider.GzipProvider

Bases: CompressionProtocol

A concrete compression provider that uses the gzip algorithm.

This class implements the CompressionProtocol by delegating the core compression and decompression logic to a specific algorithm instance obtained from the CompressionAlgorithmFactory. This design decouples the provider from the algorithm's implementation details.

Attributes:

Name Type Description
algorithm CompressionAlgorithmProtocol

The algorithm instance that performs the actual compression and decompression.

__init__(algorithm_name='gzip')

Initializes the GzipProvider.

Parameters:

Name Type Description Default
algorithm_name str

The name of the compression algorithm to create via the factory. Defaults to "gzip".

'gzip'

compress(data) async

Compresses data by delegating to the configured algorithm.

Parameters:

Name Type Description Default
data bytes

The raw byte string to be compressed.

required

Returns:

Name Type Description
bytes bytes

The gzip-compressed byte string.

decompress(data) async

Decompresses data by delegating to the configured algorithm.

Parameters:

Name Type Description Default
data bytes

The gzip-compressed byte string to be decompressed.

required

Returns:

Name Type Description
bytes bytes

The original, decompressed byte string.