Queue patterns#

Messaging patterns built on queues and handlers.

class DeadLetter#

Bases: MessageContract

A message that could not be delivered or processed, plus where it was going and why it failed.

It wraps the original message: id, type and payload are the original ones, and message keeps its class, e.g. a SignedMessage with its sender.

To redeliver it: channel.dispatch(dead_letter.connection_id, dead_letter.message).

Parameters:
  • connection_id (str) – Id of the connection the message was meant for.

  • message (MessageContract) – The original message.

  • error (str) – Description of the error, e.g. repr() of the exception.

__init__(connection_id, message, error)#
Parameters:
property connection_id: str#

Id of the connection the message was meant for.

property message: MessageContract#

The original message.

property error: str#

Description of the error.

property id: str | None#

Optional message identifier, e.g. the “id” field of an SSE event. None if the message has none.

property type: str#

Message type

property payload: dict | list | str | int | float | None#

Message payload

class DeadLetterHandler#

Bases: QueuingErrorHandler, ListenerErrorHandler

Dead Letter pattern: messages that could not be delivered or processed are pushed to a dead letter queue, wrapped in a DeadLetter.

A message is dead-lettered when:

Pop errors are not dead-lettered: no message is available when they occur.

Register the same instance for both kinds of errors:

dead_letters = DeadLetterHandler()
channel.add_queuing_error_handler(dead_letters)
channel.add_listener_error_handler(dead_letters)

or, for every channel of a repository, with queuing_error_handler() and listener_error_handler().

If pushing to the dead letter queue fails too, the error is logged and the message is lost: the handler never raises, so the original error is the one the channel re-raises or logs.

Parameters:

queue (Queue) – The dead letter queue. Defaults to an InMemoryQueue.

__init__(queue=None)#
Parameters:

queue (Queue[DeadLetter, DeadLetter] | None)

property queue: Queue[DeadLetter, DeadLetter]#

The dead letter queue, e.g. to inspect or redeliver its messages.

handle_push_error(connection_id, msg, exception)#

Called when pushing msg to the queue of connection connection_id fails. The exception is re-raised afterwards.

Parameters:
Return type:

None

handle_on_message_error(connection_id, msg, exception)#

Called when on_message() of connection connection_id fails. The exception is not re-raised: msg is delivered anyway.

Parameters:
Return type:

None

_dead_letter(connection_id, msg, exception)#

Pushes msg to the dead letter queue, wrapped in a DeadLetter. Override to change what is stored.

Parameters:
Return type:

None

class ReliableMessage#

Bases: MessageContract

A message popped from a ReliableQueue: the original message plus an id to acknowledge it with.

type and payload are the original ones, and message keeps its class.

Parameters:
__init__(message_id, message, deliveries=0)#
Parameters:
property id: str#

Id to acknowledge the message with. It replaces the original message’s id, if any.

property message: MessageContract#

The original message.

property deliveries: int#

How many times the message has been popped: more than 1 means it is a redelivery.

property type: str#

Message type

property payload: dict | list | str | int | float | None#

Message payload

class ReliableQueue#

Bases: Queue[MessageContract, ReliableMessage]

Reliable Queue pattern: a popped message isn’t removed, it is moved to an in-flight storage until it is acknowledged.

Delivery is at least once: a message acknowledged after its visibility timeout may already have been put back, and is delivered again. Re-queued messages go to the end of the queue.

It wraps another queue, that keeps the messages waiting to be popped, and a KvStorage for in-flight messages. With durable ones, unacknowledged messages survive a restart. It can be pickled if both can, so it can be persisted by a queue repository.

Parameters:
  • queue (Queue) – Queue of messages waiting to be popped. Defaults to an InMemoryQueue.

  • visibility_timeout (float | None) – Seconds a popped message can stay unacknowledged. None: until nack().

  • in_flight (KvStorage) – Storage of popped messages not yet acknowledged. Defaults to an InMemoryStorage.

__init__(queue=None, visibility_timeout=30.0, in_flight=None)#
Parameters:
push(message)#

Pushes message, wrapped in a ReliableMessage with a new id.

Parameters:

message (MessageContract)

Return type:

None

pop(timeout=0)#

Next message from the queue.

Parameters:

timeout (float) – Seconds to wait for a message. With 0 it returns immediately.

Return type:

ReliableMessage

Raises a NoMessagesException if no message is available, after waiting timeout seconds.

async wait_pop(timeout)#

Waits for the next message without blocking the event loop.

The default implementation runs pop() with timeout in a worker thread, so it takes one thread per waiting call. Override it with a native async wait when possible.

Parameters:

timeout (float) – Seconds to wait for a message.

Return type:

ReliableMessage

Raises a NoMessagesException if no message arrives within timeout.

ack(message_id)#

Acknowledges a popped message, that is removed for good.

Returns False if it isn’t in flight: unknown, already acknowledged, or put back in the queue.

Parameters:

message_id (str)

Return type:

bool

nack(message_id)#

Puts a popped message back in the queue at once.

Returns False if it isn’t in flight: unknown, already acknowledged, or put back in the queue.

Parameters:

message_id (str)

Return type:

bool

requeue_expired()#

Puts back in the queue messages not acknowledged within the visibility timeout. Returns how many.

Return type:

int

in_flight()#

Popped messages not yet acknowledged.

Return type:

list[ReliableMessage]