-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: NATS polling subscriber (#912)
* draft for pull_subscribe * task for fetch messages * handle messages with `asyncio.gather` * object and kv storage watch * fix: correct NATS pull subscriber * fix: correct `PullSub` stub file * fix: missed Optional in NATS `PullSub` stub file * fix: process NATS pull timeout * test: add PullSub tests * refactore: remove watch subscriber * chore: bump version * lint: fix mypy * docs: add NATS Pull page * docs: fill NATS PULL consumer page * lint: useless Optional * lint: ignore bandit * lint: fix typo * Update docs --------- Co-authored-by: Nikita Pastukhov <[email protected]> Co-authored-by: Davor Runje <[email protected]> Co-authored-by: Kumaran Rajendhiran <[email protected]>
- Loading branch information
1 parent
3bcf2d6
commit 427abf1
Showing
17 changed files
with
234 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Pull Subscriber | ||
|
||
## Overview | ||
|
||
**NATS JetStream** supports two various way to consume messages: [**Push** and **Pull**](https://docs.nats.io/using-nats/developer/develop_jetstream/consumers#push-and-pull-consumers){.external-link targer="_blank} consumers. | ||
|
||
The **Push** consumer is used by default to consume messages with the **FastStream**. It means that the **NATS** server delivers messages to your consumer as far as possible by itself. However, it also means that **NATS** should control all current consumer connections and increase server load. | ||
|
||
Thus, the **Pull** consumer is the recommended way to consume JetStream messages by the *NATS TEAM*. Using it, you simply ask **NATS** for new messages at some interval. It may sound a little less convenient than automatic message delivery, but it provides several advantages, such as: | ||
|
||
* Consumer scaling without a *queue group* | ||
* Handling messages in batches | ||
* Reducing **NATS** server load | ||
|
||
So, if you want to consume a large flow of messages without strict time limitations, the **Pull** consumer is the right choice for you. | ||
|
||
## FastStream Details | ||
|
||
The **Pull** consumer is just a regular *Stream* consumer, but with the `pull_sub` argument, which controls consuming messages with batch size and block interval. | ||
|
||
```python linenums="1" hl_lines="10-11" | ||
{!> docs_src/nats/js/pull_sub.py !} | ||
``` | ||
|
||
The batch size doesn't mean that your `msg` argument is a list of messages, but it means that you consume up to `10` messages for one request to **NATS** and call your handler for each message in an `asyncio.gather` pool. | ||
|
||
So, your subject will be processed much faster, without blocking for each message processing. However, if your subject has fewer than `10` messages, your request to **NATS** will be blocked for `timeout` (5 seconds by default) while trying to collect the required number of messages. Therefor, you should choose `batch_size` and `timeout` accurately to optimize your consumer efficiency. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from faststream import FastStream, Logger | ||
from faststream.nats import NatsBroker, PullSub | ||
|
||
broker = NatsBroker() | ||
app = FastStream(broker) | ||
|
||
|
||
@broker.subscriber( | ||
subject="test", | ||
stream="stream", | ||
pull_sub=PullSub(batch_size=10), | ||
) | ||
async def handle(msg, logger: Logger): | ||
logger.info(msg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from faststream import FastStream, Logger | ||
from faststream.nats import NatsBroker, PullSub | ||
|
||
broker = NatsBroker() | ||
app = FastStream(broker) | ||
|
||
|
||
@broker.subscriber( | ||
subject="test", | ||
stream="stream", | ||
pull_sub=PullSub(batch_size=10), | ||
) | ||
async def handle(msg, logger: Logger): | ||
logger.info(msg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from typing import Optional | ||
|
||
from pydantic import BaseModel, Field | ||
|
||
|
||
class PullSub(BaseModel): | ||
batch_size: int = Field(default=1) | ||
timeout: Optional[float] = Field(default=5.0) | ||
|
||
def __init__( | ||
self, | ||
batch_size: int = 1, | ||
timeout: Optional[float] = 5.0, | ||
) -> None: | ||
super().__init__( | ||
batch_size=batch_size, | ||
timeout=timeout, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.