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
199 200 201 202 203 204 205 206 207 208 209 210 211 | |
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
222 223 224 225 226 227 228 229 230 231 | |
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
233 234 235 236 237 238 239 240 241 242 | |
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
244 245 246 247 248 249 250 251 252 253 | |
publish
publish(stream: str, message: dict[str, Any]) -> str | None
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:
|
str | None
|
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
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 295 296 297 298 299 300 | |
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
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | |
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
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 | |
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
370 371 372 373 374 375 376 377 378 379 | |
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
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 473 474 475 476 | |
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
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 | |
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
516 517 518 519 520 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 | |
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
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 | |
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
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 | |
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
698 699 700 701 702 703 704 | |
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
718 719 720 721 722 723 724 725 726 727 728 729 730 731 | |
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
733 734 735 736 737 738 739 740 741 742 743 744 745 746 | |
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
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 | |
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
764 765 766 767 768 769 770 771 772 773 774 775 776 777 | |
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
779 780 781 782 783 784 785 786 787 788 789 790 791 | |
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
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 | |
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
835 836 837 838 839 840 841 842 843 844 845 846 | |
trim
trim(stream: str, maxlen: int) -> int
Trim a subscription stream to at most maxlen entries.
Optional maintenance hook called after each processed batch when a
subscription sets retention_maxlen. Brokers with a persistent
stream (e.g. Redis Streams) override this to cap the stream's size in a
way that never removes an entry a consumer group still needs. Brokers
without a persistent stream leave the default, which is a no-op.
| PARAMETER | DESCRIPTION |
|---|---|
stream
|
The stream to trim.
TYPE:
|
maxlen
|
The target maximum number of entries to keep.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
int
|
Number of messages trimmed (0 by default). |
Source code in src/protean/port/broker.py
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 | |
record_partition
record_partition(category: str, key: str) -> None
Record key in the maintained partition index for category.
Called on publish to {category}:{key} so the partitioned consumer
can discover the partition without scanning the keyspace (ADR-0028
decision 7). Idempotent: recording the same key twice is a no-op.
Source code in src/protean/port/broker.py
890 891 892 893 894 895 896 897 898 | |
partition_keys
partition_keys(category: str) -> set[str]
Return the set of live partition keys recorded for category.
Source code in src/protean/port/broker.py
900 901 902 903 | |
reap_partition
reap_partition(
category: str,
key: str,
min_idle_ms: int,
backfill_suffix: str | None = None,
) -> bool
Prune a cold partition from the index, atomically and race-safely.
Only reaps a partition when no consumer group on any of its streams
has pending entries (each stream is shared by every handler on the
category) and all of them have been idle for at least min_idle_ms
(ADR-0028 decision 7). When priority lanes are enabled, pass
backfill_suffix so the key's backfill lane
({category}:{key}:{backfill_suffix}) is checked and deleted alongside
the main stream; otherwise a lane with unconsumed work would be
stranded. Removes the index entry and the streams; the generation counter
is left in place so a re-created partition keeps a monotonic fence.
Returns True when the partition was reaped. The publish/reap race is
tolerated: a publisher that re-adds the key right after a reap is
re-discovered on the next cycle. Meant to be called by the partition's
current owner once it has drained the partition.
Source code in src/protean/port/broker.py
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 | |
acquire_partition_lease
acquire_partition_lease(
lease_key: str,
generation_key: str,
owner_id: str,
ttl_ms: int,
) -> int | None
Acquire the ownership lease for a partition, returning its generation.
Atomically (ADR-0028 decision 5): if the lease is unheld, increment the
durable generation counter, take the lease at that generation for
ttl_ms, and return the generation. If already held, return None.
Source code in src/protean/port/broker.py
931 932 933 934 935 936 937 938 939 940 941 942 943 | |
renew_partition_lease
renew_partition_lease(
lease_key: str, fence_token: str, ttl_ms: int
) -> bool
Renew a held lease, extending its TTL. Returns False if lost.
fence_token is {owner_id}:{generation}. The renew succeeds only
while the lease still holds that exact token, so an owner that lost the
lease (expiry, takeover) cannot resurrect it.
Source code in src/protean/port/broker.py
945 946 947 948 949 950 951 952 953 954 955 | |
release_partition_lease
release_partition_lease(
lease_key: str, fence_token: str
) -> bool
Release a held lease on graceful shutdown. Returns False if not held.
Source code in src/protean/port/broker.py
957 958 959 960 | |
read_partition_fenced
read_partition_fenced(
stream: str,
consumer_group: str,
consumer_name: str,
lease_key: str,
fence_token: str,
count: int = 1,
new_messages: bool = True,
) -> list[tuple[str, dict[str, Any]]]
Read from a partition stream, fenced by the ownership lease.
The lease check and the XREADGROUP are one atomic step (ADR-0028
decision 5): a caller that no longer holds the lease at fence_token
reads nothing and raises LeaseLostError rather than advancing
the partition. new_messages selects undelivered (>) vs this
consumer's already-delivered pending (0) entries. Non-blocking.
Source code in src/protean/port/broker.py
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 | |
ack_partition_fenced
ack_partition_fenced(
stream: str,
identifier: str,
consumer_group: str,
lease_key: str,
fence_token: str,
) -> bool
Ack a partition message, fenced by the ownership lease.
Like read_partition_fenced, the lease check and the XACK are
atomic: a fenced stale owner cannot ack out from under the new owner and
raises LeaseLostError.
Source code in src/protean/port/broker.py
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 | |
reclaim_partition_pending
reclaim_partition_pending(
stream: str,
consumer_group: str,
consumer_name: str,
min_idle_ms: int = 0,
count: int = 100,
) -> list[tuple[str, dict[str, Any]]]
Reclaim a dead owner's pending entries into consumer_name (XAUTOCLAIM).
On failover the new owner reclaims the previous owner's unacked entries
so none are lost or skipped (ADR-0028 decision 5, crash reclaim). Returns
the reclaimed (id, payload) entries, now owned by consumer_name.
Source code in src/protean/port/broker.py
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 | |
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
1029 1030 1031 1032 1033 1034 1035 1036 | |
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
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 | |