BaseBroker
Message broker interface. All broker adapters (Inline, Redis Stream, Redis PubSub, etc.) implement this contract.
See Broker Adapters for concrete adapter configuration.
Base class for all broker implementations.
Provides shared behavior (UoW integration, connection recovery, capability
checks, subscriber registration) via the Template Method pattern. Subclasses
implement the abstract _underscore methods for broker-specific logic.
Source code in src/protean/port/broker.py
125 126 127 128 129 130 131 132 133 134 135 136 137 | |
capabilities
abstractmethod
property
capabilities: BrokerCapabilities
Return the capabilities of this broker implementation.
| RETURNS | DESCRIPTION |
|---|---|
BrokerCapabilities
|
The capabilities supported by this broker
TYPE:
|
has_capability
has_capability(capability: BrokerCapabilities) -> bool
Check if broker has a specific capability.
| PARAMETER | DESCRIPTION |
|---|---|
capability
|
The capability to check for
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if the broker has the capability, False otherwise
TYPE:
|
Source code in src/protean/port/broker.py
148 149 150 151 152 153 154 155 156 157 | |
has_all_capabilities
has_all_capabilities(
capabilities: BrokerCapabilities,
) -> bool
Check if broker has all the specified capabilities.
| PARAMETER | DESCRIPTION |
|---|---|
capabilities
|
The capabilities to check for
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if the broker has all capabilities, False otherwise
TYPE:
|
Source code in src/protean/port/broker.py
159 160 161 162 163 164 165 166 167 168 | |
has_any_capability
has_any_capability(
capabilities: BrokerCapabilities,
) -> bool
Check if broker has any of the specified capabilities.
| PARAMETER | DESCRIPTION |
|---|---|
capabilities
|
The capabilities to check for
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if the broker has any of the capabilities, False otherwise
TYPE:
|
Source code in src/protean/port/broker.py
170 171 172 173 174 175 176 177 178 179 | |
publish
publish(
stream: str, message: dict[str, Any]
) -> Optional[str]
Publish a message to the broker.
| PARAMETER | DESCRIPTION |
|---|---|
stream
|
The stream to which the message should be published
TYPE:
|
message
|
The message payload to be published
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The identifier of the message. The content of the identifier is broker-specific.
TYPE:
|
Optional[str]
|
All brokers are guaranteed to provide message identifiers. |
| RAISES | DESCRIPTION |
|---|---|
ValidationError
|
If message is an empty dict |
Source code in src/protean/port/broker.py
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | |
ping
ping() -> bool
Test broker connectivity.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if broker is reachable and responsive, False otherwise
TYPE:
|
Source code in src/protean/port/broker.py
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | |
health_stats
health_stats() -> dict[str, Any]
Get comprehensive health statistics for the broker.
| RETURNS | DESCRIPTION |
|---|---|
dict
|
Health statistics with the following structure: { 'status': 'healthy' | 'degraded' | 'unhealthy', 'connected': bool, 'last_ping_ms': float | None, 'uptime_seconds': float, 'details': dict # Broker-specific details }
TYPE:
|
Source code in src/protean/port/broker.py
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | |
ensure_connection
ensure_connection() -> bool
Ensure broker connection is healthy, attempt reconnection if needed.
This method can be called explicitly or is triggered automatically when connection-related exceptions are encountered.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if connection is healthy/restored, False otherwise
TYPE:
|
Source code in src/protean/port/broker.py
296 297 298 299 300 301 302 303 304 305 | |
get_next
get_next(
stream: str, consumer_group: str
) -> tuple[str, dict[str, Any]] | None
Retrieve the next message to process from broker.
| PARAMETER | DESCRIPTION |
|---|---|
stream
|
The stream from which to retrieve the message
TYPE:
|
consumer_group
|
The consumer group identifier
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[str, dict[str, Any]] | None
|
tuple[str, dict] | None: A tuple of (identifier, message) or None if no messages available |
Source code in src/protean/port/broker.py
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 | |
read
read(
stream: str, consumer_group: str, no_of_messages: int
) -> list[tuple[str, dict[str, Any]]]
Read messages from the broker.
| PARAMETER | DESCRIPTION |
|---|---|
stream
|
The stream from which to read messages
TYPE:
|
consumer_group
|
The consumer group identifier
TYPE:
|
no_of_messages
|
The number of messages to read
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[tuple[str, dict[str, Any]]]
|
list[tuple[str, dict]]: The list of (identifier, message) tuples |
Source code in src/protean/port/broker.py
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 | |
ack
ack(
stream: str, identifier: str, consumer_group: str
) -> bool
Acknowledge successful processing of a message.
| PARAMETER | DESCRIPTION |
|---|---|
stream
|
The stream from which the message was received
TYPE:
|
identifier
|
The unique identifier of the message to acknowledge
TYPE:
|
consumer_group
|
The consumer group that processed the message
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if the message was successfully acknowledged, False otherwise
TYPE:
|
Source code in src/protean/port/broker.py
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 | |
nack
nack(
stream: str, identifier: str, consumer_group: str
) -> bool
Negative acknowledge - mark message for reprocessing.
| PARAMETER | DESCRIPTION |
|---|---|
stream
|
The stream from which the message was received
TYPE:
|
identifier
|
The unique identifier of the message to nack
TYPE:
|
consumer_group
|
The consumer group that failed to process the message
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if the message was successfully marked for reprocessing, False otherwise
TYPE:
|
Source code in src/protean/port/broker.py
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 | |
read_blocking
read_blocking(
stream: str,
consumer_group: str,
consumer_name: str,
timeout_ms: int = 5000,
count: int = 1,
) -> list[tuple[str, dict[str, Any]]]
Read messages from the broker using blocking mode.
This is an optional method that brokers can implement to support efficient blocking reads for stream-based subscriptions.
| PARAMETER | DESCRIPTION |
|---|---|
stream
|
The stream from which to read messages
TYPE:
|
consumer_group
|
The consumer group identifier
TYPE:
|
consumer_name
|
The unique consumer name within the group
TYPE:
|
timeout_ms
|
Timeout in milliseconds to wait for messages (0 = block indefinitely)
TYPE:
|
count
|
Maximum number of messages to read
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[tuple[str, dict[str, Any]]]
|
list[tuple[str, dict]]: The list of (identifier, message) tuples |
Source code in src/protean/port/broker.py
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 | |
info
info() -> dict[str, Any]
Get information about consumer groups and consumers in each group.
| RETURNS | DESCRIPTION |
|---|---|
dict
|
Information about consumer groups and their consumers
TYPE:
|
Source code in src/protean/port/broker.py
624 625 626 627 628 629 630 | |
dlq_list
dlq_list(
dlq_streams: list[str], limit: int = 100
) -> list[DLQEntry]
List DLQ messages across specified DLQ streams.
| PARAMETER | DESCRIPTION |
|---|---|
dlq_streams
|
List of DLQ stream names to query.
TYPE:
|
limit
|
Maximum total messages to return.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[DLQEntry]
|
List of DLQEntry objects sorted by failure time (newest first). |
Source code in src/protean/port/broker.py
644 645 646 647 648 649 650 651 652 653 654 655 656 657 | |
dlq_inspect
dlq_inspect(
dlq_stream: str, dlq_id: str
) -> DLQEntry | None
Inspect a specific DLQ message by its DLQ entry ID.
| PARAMETER | DESCRIPTION |
|---|---|
dlq_stream
|
The DLQ stream to look in.
TYPE:
|
dlq_id
|
The entry identifier within the DLQ stream.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DLQEntry | None
|
DLQEntry if found, None otherwise. |
Source code in src/protean/port/broker.py
659 660 661 662 663 664 665 666 667 668 669 670 671 672 | |
dlq_replay
dlq_replay(
dlq_stream: str, dlq_id: str, target_stream: str
) -> bool
Replay a single DLQ message back to its original stream.
| PARAMETER | DESCRIPTION |
|---|---|
dlq_stream
|
The DLQ stream the message is in.
TYPE:
|
dlq_id
|
The entry identifier within the DLQ stream.
TYPE:
|
target_stream
|
The stream to re-publish the message to.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if replayed successfully, False otherwise. |
Source code in src/protean/port/broker.py
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 | |
dlq_replay_all
dlq_replay_all(dlq_stream: str, target_stream: str) -> int
Replay all DLQ messages from a stream back to the original stream.
| PARAMETER | DESCRIPTION |
|---|---|
dlq_stream
|
The DLQ stream to drain.
TYPE:
|
target_stream
|
The stream to re-publish messages to.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
int
|
Number of messages replayed. |
Source code in src/protean/port/broker.py
690 691 692 693 694 695 696 697 698 699 700 701 702 703 | |
dlq_purge
dlq_purge(dlq_stream: str) -> int
Purge all messages from a DLQ stream.
| PARAMETER | DESCRIPTION |
|---|---|
dlq_stream
|
The DLQ stream to purge.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
int
|
Number of messages purged. |
Source code in src/protean/port/broker.py
705 706 707 708 709 710 711 712 713 714 715 716 717 | |
dlq_trim
dlq_trim(dlq_stream: str, min_id: str) -> int
Trim DLQ messages older than min_id (time-based trimming).
This is an optional method used by the DLQ maintenance task to remove messages that have exceeded their retention period. Brokers that don't support time-based trimming can leave the default implementation which returns 0.
| PARAMETER | DESCRIPTION |
|---|---|
dlq_stream
|
The DLQ stream to trim.
TYPE:
|
min_id
|
Cutoff identifier (format varies by broker). Messages older than this cutoff will be removed.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
int
|
Number of messages trimmed. |
Source code in src/protean/port/broker.py
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 | |
dlq_depth
dlq_depth(dlq_stream: str) -> int
Return the current number of messages in a DLQ stream.
Brokers that don't support depth queries return 0.
| PARAMETER | DESCRIPTION |
|---|---|
dlq_stream
|
The DLQ stream to query.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
int
|
Number of messages in the DLQ stream. |
Source code in src/protean/port/broker.py
761 762 763 764 765 766 767 768 769 770 771 772 | |
close
close() -> None
Close the broker and release all connections.
Subclasses that hold external resources (connection pools, sockets, etc.) should override this to perform cleanup. The default implementation is a no-op so that adapters without external resources (e.g. the inline broker) work without changes.
Source code in src/protean/port/broker.py
774 775 776 777 778 779 780 781 | |
register
register(subscriber_cls: Type[BaseSubscriber]) -> None
Register a subscriber to this broker against its stream.
| PARAMETER | DESCRIPTION |
|---|---|
subscriber_cls
|
The subscriber class connected to the stream.
TYPE:
|
Source code in src/protean/port/broker.py
783 784 785 786 787 788 789 790 791 792 793 794 795 | |