Skip to main content

FastStream SQL broker

Project description

Downloads Package version Documentation

faststream-sqlbroker

A SQL-backed broker for FastStream. Documentation.

Transactional outbox

Implementing the transactional outbox pattern becomes as simple as the following.

Publish messages transactionally with your other database operations.

from sqlalchemy.ext.asyncio import create_async_engine

from faststream import AckPolicy, FastStream
from faststream.kafka import KafkaBroker

from faststream_sqlbroker.sqlbroker import SqlBroker
from faststream_sqlbroker.sqlbroker.retry import ExponentialBackoffRetryStrategy

engine = create_async_engine("postgresql+asyncpg://user:pass@localhost/mydb")
broker_sqlbroker = SqlBroker(engine=engine)
broker_kafka = KafkaBroker("127.0.0.1:9092")
app = FastStream(broker_sqlbroker, on_startup=[broker_kafka.connect])
publisher_sqlbroker = broker_sqlbroker.publisher()


@app.after_startup # just an example
async def publish_examples():
    async with engine.begin() as connection:
        # ... your other database operations using `connection` ...
        await publisher_sqlbroker.publish(
            {"message": "Hello, SqlBroker!"},
            queue="sqlbroker_queue",
            connection=connection,
        )

And relay the messages from the database to another broker.

publisher_kafka = broker_kafka.publisher("kafka_topic")


@publisher_kafka
@broker_sqlbroker.subscriber(
    queues=["sqlbroker_queue"],
    max_workers=10,
    retry_strategy=ExponentialBackoffRetryStrategy(
        initial_delay_seconds=1,
        multiplier=2,
        max_delay_seconds=60 * 5,
        max_total_delay_seconds=60 * 60 * 6,
        max_attempts=None,
    ),
    max_fetch_interval=1,
    min_fetch_interval=0,
    fetch_batch_size=10,
    overfetch_factor=1.5,
    flush_interval=3,
    release_stuck_interval=5,
    release_stuck_timeout=60 * 60,
    max_deliveries=20,
    ack_policy=AckPolicy.NACK_ON_ERROR,
)
async def handle_msg(msg_body: dict) -> dict:
    return msg_body

Origins

Originated as a PR to FastStream.

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_sqlbroker-0.1.0a4.tar.gz (20.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

faststream_sqlbroker-0.1.0a4-py3-none-any.whl (32.1 kB view details)

Uploaded Python 3

File details

Details for the file faststream_sqlbroker-0.1.0a4.tar.gz.

File metadata

  • Download URL: faststream_sqlbroker-0.1.0a4.tar.gz
  • Upload date:
  • Size: 20.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for faststream_sqlbroker-0.1.0a4.tar.gz
Algorithm Hash digest
SHA256 408f9a5ff89e2d72cddb64100509b9bb196c4f42f2d5de9aebba13b8fc5bdf41
MD5 d5e42dc27c9bafb0fc3f6c232fb9fad9
BLAKE2b-256 a97a0519110b16510c70a936fee0afdae12b67ad05d1b6a806cfdb260dda84f2

See more details on using hashes here.

Provenance

The following attestation bundles were made for faststream_sqlbroker-0.1.0a4.tar.gz:

Publisher: release_pypi.yaml on faststream-community/faststream-sqlbroker

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file faststream_sqlbroker-0.1.0a4-py3-none-any.whl.

File metadata

File hashes

Hashes for faststream_sqlbroker-0.1.0a4-py3-none-any.whl
Algorithm Hash digest
SHA256 58d90740fcc376d75fb6824143d8c71282f6871baba052b823eccd558d5e587e
MD5 bb77ed603e7d59ddc054b3c28b00401a
BLAKE2b-256 94ca147212f5bad75b70a908a6df09fd76fb5d496a6b79a8fffffefb4d0e4071

See more details on using hashes here.

Provenance

The following attestation bundles were made for faststream_sqlbroker-0.1.0a4-py3-none-any.whl:

Publisher: release_pypi.yaml on faststream-community/faststream-sqlbroker

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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