Skip to content

Leases and flow control#

The subscriber reads through a streaming pull and leases each message as it arrives: it extends the ack deadline of every message it holds, those waiting for a free handler included, until the handler settles it, up to max_lease. FlowControl bounds what the subscriber holds at once.

service.py
from faststream import FastStream

from faststream_gcp_pubsub import FlowControl, PubSubBroker

broker = PubSubBroker(project_id="my-project", graceful_timeout=30.0)
app = FastStream(broker)


@broker.subscriber(
    "orders-billing",
    topic="orders",
    flow_control=FlowControl(
        max_messages=100,
        max_bytes=10 * 1024 * 1024,
        ack_deadline=60,
        max_lease=1800.0,
        ack_batch_wait=0.05,
    ),
    max_workers=10,
)
async def bill(order: dict[str, int]) -> None: ...
Field Default Meaning
max_messages 1000 Deliveries held at once, unsettled. The stream is not read further while this many are held. At least 1.
max_bytes 100 MiB Payload bytes held at once. The service stops sending past it.
ack_deadline 60 s The length of a lease before it is extended, 10 to 600.
max_lease 3600 s The longest a delivery is held, counted from its arrival. Past it the lease is no longer extended and the service redelivers the message.
ack_batch_wait 0.05 s How long acks and nacks wait to be sent together.

Choose max_lease above the longest time a message can wait for a handler and be handled. A message held longer is redelivered.

Batched settlement#

Acks and nacks of one subscription are sent together: one request per ack_batch_wait, or per 1000 ids, which is the largest request the service takes. The handler does not wait for the request.

Exactly-once delivery#

On a subscription with enable_exactly_once_delivery=True the service guarantees that an acknowledged message is not delivered again. There ack() waits for the service's confirmation and raises when the service refuses it.

service.py
from faststream import AckPolicy, FastStream, Logger

from faststream_gcp_pubsub import PubSub, PubSubBroker, PubSubMessage

broker = PubSubBroker(project_id="my-project")
app = FastStream(broker)

billing = PubSub(
    "orders-billing",
    declare=True,
    enable_exactly_once_delivery=True,
)


@broker.subscriber(billing, topic="orders", ack_policy=AckPolicy.MANUAL)
async def bill(order: dict[str, int], msg: PubSubMessage, logger: Logger) -> None:
    try:
        # waits for the service to confirm the acknowledgement
        await msg.ack()
    except Exception:
        logger.exception("order %s was not confirmed", order["id"])
        raise

With the default policy the confirmation failure follows the usual path: the handler's message is nacked when the exception propagates.

Graceful shutdown#

On shutdown the subscriber stops reading, waits for the handlers in work up to the broker's graceful_timeout (15 seconds by default), sends the pending acks and returns the messages it still holds to the service, which delivers them to another consumer.

service.py
broker = PubSubBroker(project_id="my-project", graceful_timeout=30.0)