8000 docs: Added filtering technical information by 0xWEBMILK · Pull Request #2161 · ag2ai/faststream · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

docs: Added filtering technical information #2161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 29 commits into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
89136ca
Proofread docs
kumaranvpl Mar 17, 2025
a6510ef
docs: Added filtering technical information
0xWEBMILK Apr 6, 2025
5f786ff
Merge remote-tracking branch 'origin/main'
0xWEBMILK Apr 6, 2025
f1bc126
docs: generate API References
0xWEBMILK Apr 6, 2025
8804e21
Update release.md
Lancetnik Apr 6, 2025
d10ef42
docs: generate API References
Lancetnik Apr 6, 2025
16b6d1d
Removed publisher decorator method conflict
0xWEBMILK Apr 6, 2025
2411d6b
Merge remote-tracking branch 'origin/main'
0xWEBMILK Apr 6, 2025
a5e0c90
docs: generate API References
0xWEBMILK Apr 6, 2025
3a64844
Merge branch 'main' of github.com:0xWEBMILK/faststream into webmilk-main
kumaranvpl Apr 7, 2025
1ce4af9
Minor proofreading improvements
kumaranvpl Apr 7, 2025
7adae8e
Merge branch 'main' into webmilk-main
kumaranvpl Apr 7, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/docs/en/getting-started/publishing/decorator.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ It creates a structured DataPipeline unit with an input and output. The order of

:material-checkbox-marked:{.checked_mark} **Easy to use** - Publishing messages in **FastStream** is intuitive and requires minimal effort.

:material-checkbox-marked:{.checked_mark} **AsyncAPI support** - [```AsyncAPI```](../asyncapi/export.md#section{.css-styles}) is a specification for describing asynchronous APIs used in messaging applications. This method currently does not support this standard.
:material-checkbox-marked:{.checked_mark} **AsyncAPI support** - [```AsyncAPI```](../asyncapi/export.md#section{.css-styles}) is a specification for describing asynchronous APIs used in messaging applications.

:fontawesome-solid-square-xmark:{.x_mark} **No testing support** - This method lacks full [```Testing```](./test.md#section{.css-styles}) support.

Expand Down
45 changes: 45 additions & 0 deletions docs/docs/en/getting-started/subscription/filtering.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,48 @@ And this one will be delivered to the `default_handler`
```python hl_lines="2"
{!> docs_src/getting_started/subscription/redis/filter.py [ln:29.5,30.5,31.5,32.5] !}
```

---

## Technical Information

Let's break down how message filtering works in a subscription mechanism.

### Core Filtering Logic

Consider a simple example of a filter implementation:

```python
for handler in subscriber.handlers:
if await handler.filter(msg):
return await handler.process(msg)

raise HandlerNotFoundError
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may be wrong, but it seems to me that if the message has not been filtered by any handler, then the exception does not occur.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

```

This code selects the first suitable handler to process the message. This means the **default handler should be placed last** in the list. If no logical handlers match, the message must still be processed. For this, we need a special trash handler that defines the system's default behavior for such cases.

### Implementing the Default Handler

The default handler should be declared as follows:

```python
subscriber = broker.subscriber()

@subscriber(filter=...)
async def handler(): ...

@subscriber()
async def default_handler(): ...
```

Here, `@subscriber()` is equivalent to `@subscriber(filter=lambda _: True)`, meaning it **accepts all** messages. This ensures that no message goes unprocessed, even if no specific handler is found.

### Summary

- Handlers are checked in order, and the first matching one processes the message.
- The default handler must be **placed last** to ensure all messages are handled.
- `@subscriber()` without parameters acts as a universal handler, accepting everything.
- A trash handler must properly finalize the subscription and inform the broker about unnecessary data.

Properly managing subscribers allows for precise message processing control and prevents data loss.
Loading
0