Queue patterns#
Messaging patterns built on queues and handlers.
- class DeadLetter#
Bases:
MessageContractA message that could not be delivered or processed, plus where it was going and why it failed.
It wraps the original message:
id,typeandpayloadare the original ones, andmessagekeeps its class, e.g. aSignedMessagewith 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:
connection_id (str)
message (MessageContract)
error (str)
- property message: MessageContract#
The original message.
- class DeadLetterHandler#
Bases:
QueuingErrorHandler,ListenerErrorHandlerDead 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:
pushing it to a listener’s queue fails, see
handle_push_error();the listener’s
on_message()fails, seehandle_on_message_error().
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()andlistener_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:
connection_id (str)
msg (MessageContract)
exception (Exception)
- 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:
connection_id (str)
msg (MessageContract)
exception (Exception)
- 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:
connection_id (str)
msg (MessageContract)
exception (Exception)
- Return type:
None
- class ReliableMessage#
Bases:
MessageContractA message popped from a
ReliableQueue: the original message plus an id to acknowledge it with.typeandpayloadare the original ones, andmessagekeeps its class.- Parameters:
message_id (str) – Id to pass to
ReliableQueue.ack()orReliableQueue.nack().message (MessageContract) – The original message.
deliveries (int) – How many times the message has been popped, this time included.
- __init__(message_id, message, deliveries=0)#
- Parameters:
message_id (str)
message (MessageContract)
deliveries (int)
- property id: str#
Id to acknowledge the message with. It replaces the original message’s id, if any.
- property message: MessageContract#
The original message.
- 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.
pop()andwait_pop()return aReliableMessage;ack()removes it for good, once it has been handled, e.g. when the client confirms it got it;nack()puts it back in the queue at once;messages not acknowledged within visibility_timeout seconds are put back in the queue by
requeue_expired(), thatpop()andwait_pop()call before popping.
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
KvStoragefor 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:
queue (Queue[ReliableMessage, ReliableMessage] | None)
visibility_timeout (float | None)
in_flight (KvStorage | None)
- push(message)#
Pushes message, wrapped in a
ReliableMessagewith 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:
Raises a
NoMessagesExceptionif 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:
Raises a
NoMessagesExceptionif 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.
- 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.
- requeue_expired()#
Puts back in the queue messages not acknowledged within the visibility timeout. Returns how many.
- Return type:
- in_flight()#
Popped messages not yet acknowledged.
- Return type: