varco-nats 2.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- varco_nats-2.0.0/.gitignore +209 -0
- varco_nats-2.0.0/PKG-INFO +262 -0
- varco_nats-2.0.0/README.md +236 -0
- varco_nats-2.0.0/pyproject.toml +72 -0
- varco_nats-2.0.0/tests/__init__.py +1 -0
- varco_nats-2.0.0/tests/fakes.py +368 -0
- varco_nats-2.0.0/tests/test_nats_bus.py +388 -0
- varco_nats-2.0.0/tests/test_nats_channel.py +173 -0
- varco_nats-2.0.0/tests/test_nats_config.py +100 -0
- varco_nats-2.0.0/tests/test_nats_connection.py +89 -0
- varco_nats-2.0.0/tests/test_nats_dlq.py +196 -0
- varco_nats-2.0.0/tests/test_nats_health.py +98 -0
- varco_nats-2.0.0/tests/test_nats_integration.py +154 -0
- varco_nats-2.0.0/varco_nats/__init__.py +95 -0
- varco_nats-2.0.0/varco_nats/bus.py +708 -0
- varco_nats-2.0.0/varco_nats/channel.py +460 -0
- varco_nats-2.0.0/varco_nats/config.py +299 -0
- varco_nats-2.0.0/varco_nats/connection.py +203 -0
- varco_nats-2.0.0/varco_nats/di.py +128 -0
- varco_nats-2.0.0/varco_nats/dlq.py +697 -0
- varco_nats-2.0.0/varco_nats/health.py +165 -0
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[codz]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py.cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
.pybuilder/
|
|
76
|
+
target/
|
|
77
|
+
|
|
78
|
+
# Jupyter Notebook
|
|
79
|
+
.ipynb_checkpoints
|
|
80
|
+
|
|
81
|
+
# IPython
|
|
82
|
+
profile_default/
|
|
83
|
+
ipython_config.py
|
|
84
|
+
|
|
85
|
+
# pyenv
|
|
86
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
87
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
88
|
+
.python-version
|
|
89
|
+
|
|
90
|
+
# pipenv
|
|
91
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
94
|
+
# install all needed dependencies.
|
|
95
|
+
#Pipfile.lock
|
|
96
|
+
|
|
97
|
+
# UV
|
|
98
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
99
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
100
|
+
# commonly ignored for libraries.
|
|
101
|
+
#uv.lock
|
|
102
|
+
|
|
103
|
+
# poetry
|
|
104
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
105
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
106
|
+
# commonly ignored for libraries.
|
|
107
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
108
|
+
#poetry.lock
|
|
109
|
+
#poetry.toml
|
|
110
|
+
|
|
111
|
+
# pdm
|
|
112
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
113
|
+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
|
114
|
+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
|
115
|
+
#pdm.lock
|
|
116
|
+
#pdm.toml
|
|
117
|
+
.pdm-python
|
|
118
|
+
.pdm-build/
|
|
119
|
+
|
|
120
|
+
# pixi
|
|
121
|
+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
|
122
|
+
#pixi.lock
|
|
123
|
+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
|
124
|
+
# in the .venv directory. It is recommended not to include this directory in version control.
|
|
125
|
+
.pixi
|
|
126
|
+
|
|
127
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
128
|
+
__pypackages__/
|
|
129
|
+
|
|
130
|
+
# Celery stuff
|
|
131
|
+
celerybeat-schedule
|
|
132
|
+
celerybeat.pid
|
|
133
|
+
|
|
134
|
+
# SageMath parsed files
|
|
135
|
+
*.sage.py
|
|
136
|
+
|
|
137
|
+
# Environments
|
|
138
|
+
.env
|
|
139
|
+
.envrc
|
|
140
|
+
.venv
|
|
141
|
+
env/
|
|
142
|
+
venv/
|
|
143
|
+
ENV/
|
|
144
|
+
env.bak/
|
|
145
|
+
venv.bak/
|
|
146
|
+
|
|
147
|
+
# Spyder project settings
|
|
148
|
+
.spyderproject
|
|
149
|
+
.spyproject
|
|
150
|
+
|
|
151
|
+
# Rope project settings
|
|
152
|
+
.ropeproject
|
|
153
|
+
|
|
154
|
+
# mkdocs documentation
|
|
155
|
+
/site
|
|
156
|
+
|
|
157
|
+
# mypy
|
|
158
|
+
.mypy_cache/
|
|
159
|
+
.dmypy.json
|
|
160
|
+
dmypy.json
|
|
161
|
+
|
|
162
|
+
# Pyre type checker
|
|
163
|
+
.pyre/
|
|
164
|
+
|
|
165
|
+
# pytype static type analyzer
|
|
166
|
+
.pytype/
|
|
167
|
+
|
|
168
|
+
# Cython debug symbols
|
|
169
|
+
cython_debug/
|
|
170
|
+
|
|
171
|
+
# PyCharm
|
|
172
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
173
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
174
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
175
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
176
|
+
#.idea/
|
|
177
|
+
|
|
178
|
+
# Abstra
|
|
179
|
+
# Abstra is an AI-powered process automation framework.
|
|
180
|
+
# Ignore directories containing user credentials, local state, and settings.
|
|
181
|
+
# Learn more at https://abstra.io/docs
|
|
182
|
+
.abstra/
|
|
183
|
+
|
|
184
|
+
# Visual Studio Code
|
|
185
|
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
|
186
|
+
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
|
187
|
+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
|
188
|
+
# you could uncomment the following to ignore the entire vscode folder
|
|
189
|
+
# .vscode/
|
|
190
|
+
|
|
191
|
+
# Ruff stuff:
|
|
192
|
+
.ruff_cache/
|
|
193
|
+
|
|
194
|
+
# PyPI configuration file
|
|
195
|
+
.pypirc
|
|
196
|
+
|
|
197
|
+
# Cursor
|
|
198
|
+
# Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
|
|
199
|
+
# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
|
|
200
|
+
# refer to https://docs.cursor.com/context/ignore-files
|
|
201
|
+
.cursorignore
|
|
202
|
+
.cursorindexingignore
|
|
203
|
+
|
|
204
|
+
todo*.txt
|
|
205
|
+
|
|
206
|
+
# Marimo
|
|
207
|
+
marimo/_static/
|
|
208
|
+
marimo/_lsp/
|
|
209
|
+
__marimo__/
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: varco-nats
|
|
3
|
+
Version: 2.0.0
|
|
4
|
+
Summary: NATS JetStream event bus backend for varco — NatsEventBus built on nats-py
|
|
5
|
+
Project-URL: Homepage, https://github.com/edoardoscarpaci/varco
|
|
6
|
+
Project-URL: Repository, https://github.com/edoardoscarpaci/varco/tree/main/varco_nats
|
|
7
|
+
Project-URL: Issues, https://github.com/edoardoscarpaci/varco/issues
|
|
8
|
+
Author-email: "edoardo.scarpaci" <edoardo.scarpaci@gmail.com>
|
|
9
|
+
License: Apache-2.0
|
|
10
|
+
Keywords: async,domain-events,event-bus,fastapi,jetstream,nats,nats-py,varco
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
|
|
17
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
18
|
+
Classifier: Topic :: System :: Networking
|
|
19
|
+
Classifier: Typing :: Typed
|
|
20
|
+
Requires-Python: >=3.12
|
|
21
|
+
Requires-Dist: nats-py>=2.7
|
|
22
|
+
Requires-Dist: providify>=1.0.0a0
|
|
23
|
+
Requires-Dist: pydantic-settings>=2.0
|
|
24
|
+
Requires-Dist: varco-core
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
|
|
27
|
+
# varco-nats
|
|
28
|
+
|
|
29
|
+
NATS **JetStream** event bus backend for [varco](https://github.com/edoardoscarpaci/varco) —
|
|
30
|
+
`NatsEventBus` built on [nats-py](https://github.com/nats-io/nats.py).
|
|
31
|
+
|
|
32
|
+
`varco_nats` implements `varco_core`'s `AbstractEventBus`, `ChannelManager`,
|
|
33
|
+
`AbstractDeadLetterQueue` and `HealthCheck` contracts on top of NATS JetStream —
|
|
34
|
+
the persistent, at-least-once layer of NATS (its analogue of Apache Kafka).
|
|
35
|
+
|
|
36
|
+
> **JetStream only.** Core NATS at-most-once pub/sub is intentionally not
|
|
37
|
+
> exposed. If you need fire-and-forget delivery, use `varco_redis`'s Pub/Sub bus.
|
|
38
|
+
|
|
39
|
+
---
|
|
40
|
+
|
|
41
|
+
## Installation
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
uv add varco-nats # or: pip install varco-nats
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Requires a running NATS server with **JetStream enabled** (`nats-server -js`).
|
|
48
|
+
|
|
49
|
+
---
|
|
50
|
+
|
|
51
|
+
## Quick start
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
from varco_nats import NatsEventBus, NatsEventBusSettings
|
|
55
|
+
from varco_core.event import BusEventProducer, EventConsumer, listen
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class OrderPlacedEvent(Event):
|
|
59
|
+
__event_type__ = "order.placed"
|
|
60
|
+
order_id: str
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
config = NatsEventBusSettings(
|
|
64
|
+
servers="nats://localhost:4222",
|
|
65
|
+
durable_name="order-service", # the JetStream analogue of a Kafka group_id
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
async with NatsEventBus(config) as bus:
|
|
69
|
+
class OrderConsumer(EventConsumer):
|
|
70
|
+
@listen(OrderPlacedEvent, channel="orders")
|
|
71
|
+
async def on_placed(self, event: OrderPlacedEvent) -> None:
|
|
72
|
+
print(f"Order placed: {event.order_id}")
|
|
73
|
+
|
|
74
|
+
OrderConsumer().register_to(bus)
|
|
75
|
+
|
|
76
|
+
producer = BusEventProducer(bus)
|
|
77
|
+
await producer._produce(OrderPlacedEvent(order_id="abc"), channel="orders")
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
---
|
|
81
|
+
|
|
82
|
+
## How channels map to NATS
|
|
83
|
+
|
|
84
|
+
Unlike Kafka — where each channel is a topic — NATS channels are **subjects
|
|
85
|
+
under a single JetStream stream's wildcard**:
|
|
86
|
+
|
|
87
|
+
| varco concept | NATS concept |
|
|
88
|
+
|---------------|--------------|
|
|
89
|
+
| stream (`stream_name`) | one JetStream stream capturing `{subject_prefix}.>` |
|
|
90
|
+
| channel `"orders"` | subject `{subject_prefix}.{channel_prefix}orders` |
|
|
91
|
+
| `durable_name` | base name for durable consumers (≈ Kafka consumer group) |
|
|
92
|
+
| `CHANNEL_ALL` | local-only filter — opens **no** consumer |
|
|
93
|
+
|
|
94
|
+
The bus creates the backing stream automatically on `start()` when
|
|
95
|
+
`auto_create_stream=True` (the default).
|
|
96
|
+
|
|
97
|
+
---
|
|
98
|
+
|
|
99
|
+
## Delivery semantics
|
|
100
|
+
|
|
101
|
+
`NatsEventBus` mirrors `KafkaEventBus`: JetStream redelivery is the
|
|
102
|
+
**broker-level** safety net, while **handler-level** retries are the job of
|
|
103
|
+
varco's `@listen(retry_policy=..., dlq=...)` machinery.
|
|
104
|
+
|
|
105
|
+
| `delivery_semantics` | Behaviour |
|
|
106
|
+
|----------------------|-----------|
|
|
107
|
+
| `at_most_once` | Message acked **before** dispatch. Crash → message lost. No duplicates. |
|
|
108
|
+
| `at_least_once` *(default)* | Message acked **after** dispatch. Crash before ack → JetStream redelivers. |
|
|
109
|
+
| `exactly_once` | As `at_least_once` + every publish carries `Nats-Msg-Id = event.event_id`, so JetStream drops producer-retry duplicates within `duplicate_window`. |
|
|
110
|
+
|
|
111
|
+
```python
|
|
112
|
+
from varco_nats import NatsDeliverySemantics
|
|
113
|
+
|
|
114
|
+
config = NatsEventBusSettings(
|
|
115
|
+
servers="nats://localhost:4222",
|
|
116
|
+
delivery_semantics=NatsDeliverySemantics.EXACTLY_ONCE,
|
|
117
|
+
)
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
---
|
|
121
|
+
|
|
122
|
+
## Configuration
|
|
123
|
+
|
|
124
|
+
All settings are read from environment variables with the `VARCO_NATS_` prefix:
|
|
125
|
+
|
|
126
|
+
```bash
|
|
127
|
+
VARCO_NATS_SERVERS=nats://nats.internal:4222
|
|
128
|
+
VARCO_NATS_STREAM_NAME=orders-events
|
|
129
|
+
VARCO_NATS_SUBJECT_PREFIX=orders
|
|
130
|
+
VARCO_NATS_DURABLE_NAME=order-service
|
|
131
|
+
VARCO_NATS_DELIVERY_SEMANTICS=at_least_once
|
|
132
|
+
VARCO_NATS_CHANNEL_PREFIX=prod.
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
```python
|
|
136
|
+
config = NatsEventBusSettings.from_env()
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
For structured connection/security config (TLS, user/password, token), use
|
|
140
|
+
`NatsConnectionSettings` with the `NATS_` prefix:
|
|
141
|
+
|
|
142
|
+
```bash
|
|
143
|
+
NATS_SERVERS=nats://nats1:4222,nats://nats2:4222
|
|
144
|
+
NATS_SSL__CA_CERT=/etc/ssl/nats-ca.pem
|
|
145
|
+
NATS_AUTH__TYPE=basic
|
|
146
|
+
NATS_AUTH__USERNAME=alice
|
|
147
|
+
NATS_AUTH__PASSWORD=secret
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
```python
|
|
151
|
+
from varco_nats import NatsConnectionSettings
|
|
152
|
+
|
|
153
|
+
conn = NatsConnectionSettings.from_env()
|
|
154
|
+
config = NatsEventBusSettings(connect_kwargs=conn.to_nats_kwargs())
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
---
|
|
158
|
+
|
|
159
|
+
## Stream management
|
|
160
|
+
|
|
161
|
+
`NatsStreamManager` administers the backing JetStream stream:
|
|
162
|
+
|
|
163
|
+
```python
|
|
164
|
+
from varco_nats import NatsStreamManager, NatsChannelManagerSettings
|
|
165
|
+
|
|
166
|
+
settings = NatsChannelManagerSettings(servers="nats://localhost:4222")
|
|
167
|
+
async with NatsStreamManager(settings) as manager:
|
|
168
|
+
await manager.declare_channel("orders") # ensures the backing stream
|
|
169
|
+
exists = await manager.channel_exists("orders") # has the subject any message?
|
|
170
|
+
channels = await manager.list_channels() # channels carrying messages
|
|
171
|
+
await manager.delete_channel("orders") # purge that channel's messages
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
---
|
|
175
|
+
|
|
176
|
+
## Dead letter queue
|
|
177
|
+
|
|
178
|
+
`NatsDLQ` stores exhausted events in a dedicated **WorkQueue-retention**
|
|
179
|
+
JetStream stream — so `count()` returns the *exact* pending-entry count:
|
|
180
|
+
|
|
181
|
+
```python
|
|
182
|
+
from varco_nats import NatsDLQ
|
|
183
|
+
|
|
184
|
+
async with NatsDLQ(settings=NatsEventBusSettings()) as dlq:
|
|
185
|
+
# Usually wired automatically via @listen(dlq=dlq):
|
|
186
|
+
class OrderConsumer(EventConsumer):
|
|
187
|
+
@listen(
|
|
188
|
+
OrderPlacedEvent,
|
|
189
|
+
channel="orders",
|
|
190
|
+
retry_policy=RetryPolicy(max_attempts=3),
|
|
191
|
+
dlq=dlq,
|
|
192
|
+
)
|
|
193
|
+
async def on_order(self, event: OrderPlacedEvent) -> None: ...
|
|
194
|
+
|
|
195
|
+
# Relay:
|
|
196
|
+
entries = await dlq.pop_batch(limit=10)
|
|
197
|
+
for entry in entries:
|
|
198
|
+
await alert_ops(entry)
|
|
199
|
+
await dlq.ack(entry.entry_id)
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
---
|
|
203
|
+
|
|
204
|
+
## Dependency injection (Providify)
|
|
205
|
+
|
|
206
|
+
```python
|
|
207
|
+
from varco_nats.di import bootstrap
|
|
208
|
+
from varco_core.event import AbstractEventBus
|
|
209
|
+
|
|
210
|
+
container = bootstrap() # scans varco_nats
|
|
211
|
+
bus = await container.aget(AbstractEventBus) # NatsEventBus singleton
|
|
212
|
+
# ...
|
|
213
|
+
await container.ashutdown() # stops the bus via @PreDestroy
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
Install the DLQ explicitly when needed:
|
|
217
|
+
|
|
218
|
+
```python
|
|
219
|
+
from varco_nats.dlq import NatsDLQConfiguration
|
|
220
|
+
from varco_core.event.dlq import AbstractDeadLetterQueue
|
|
221
|
+
|
|
222
|
+
await container.ainstall(NatsDLQConfiguration)
|
|
223
|
+
dlq = await container.aget(AbstractDeadLetterQueue)
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
---
|
|
227
|
+
|
|
228
|
+
## Running tests
|
|
229
|
+
|
|
230
|
+
```bash
|
|
231
|
+
# Unit tests — no broker required (nats-py is faked)
|
|
232
|
+
uv run pytest varco_nats/tests/
|
|
233
|
+
|
|
234
|
+
# Integration tests — require Docker (a real NATS server is started)
|
|
235
|
+
uv run pytest varco_nats/tests/ -m integration
|
|
236
|
+
# or
|
|
237
|
+
VARCO_RUN_INTEGRATION=1 uv run pytest varco_nats/tests/
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
---
|
|
241
|
+
|
|
242
|
+
## Migration 1.x → 2.0
|
|
243
|
+
|
|
244
|
+
The no-op `@Configuration` aliases (`NatsEventBusConfiguration`,
|
|
245
|
+
`NatsChannelManagerConfiguration`) were removed. Register the bus via scan:
|
|
246
|
+
|
|
247
|
+
```python
|
|
248
|
+
# Before (1.x)
|
|
249
|
+
await container.ainstall(NatsEventBusConfiguration)
|
|
250
|
+
|
|
251
|
+
# After (2.0)
|
|
252
|
+
from varco_nats.di import bootstrap
|
|
253
|
+
bootstrap(container) # or: container.scan("varco_nats", recursive=True)
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
The opt-in `NatsDLQConfiguration` is unchanged — still `await container.ainstall(...)`.
|
|
257
|
+
|
|
258
|
+
---
|
|
259
|
+
|
|
260
|
+
## License
|
|
261
|
+
|
|
262
|
+
Apache-2.0
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
# varco-nats
|
|
2
|
+
|
|
3
|
+
NATS **JetStream** event bus backend for [varco](https://github.com/edoardoscarpaci/varco) —
|
|
4
|
+
`NatsEventBus` built on [nats-py](https://github.com/nats-io/nats.py).
|
|
5
|
+
|
|
6
|
+
`varco_nats` implements `varco_core`'s `AbstractEventBus`, `ChannelManager`,
|
|
7
|
+
`AbstractDeadLetterQueue` and `HealthCheck` contracts on top of NATS JetStream —
|
|
8
|
+
the persistent, at-least-once layer of NATS (its analogue of Apache Kafka).
|
|
9
|
+
|
|
10
|
+
> **JetStream only.** Core NATS at-most-once pub/sub is intentionally not
|
|
11
|
+
> exposed. If you need fire-and-forget delivery, use `varco_redis`'s Pub/Sub bus.
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
uv add varco-nats # or: pip install varco-nats
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Requires a running NATS server with **JetStream enabled** (`nats-server -js`).
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
## Quick start
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
from varco_nats import NatsEventBus, NatsEventBusSettings
|
|
29
|
+
from varco_core.event import BusEventProducer, EventConsumer, listen
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class OrderPlacedEvent(Event):
|
|
33
|
+
__event_type__ = "order.placed"
|
|
34
|
+
order_id: str
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
config = NatsEventBusSettings(
|
|
38
|
+
servers="nats://localhost:4222",
|
|
39
|
+
durable_name="order-service", # the JetStream analogue of a Kafka group_id
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
async with NatsEventBus(config) as bus:
|
|
43
|
+
class OrderConsumer(EventConsumer):
|
|
44
|
+
@listen(OrderPlacedEvent, channel="orders")
|
|
45
|
+
async def on_placed(self, event: OrderPlacedEvent) -> None:
|
|
46
|
+
print(f"Order placed: {event.order_id}")
|
|
47
|
+
|
|
48
|
+
OrderConsumer().register_to(bus)
|
|
49
|
+
|
|
50
|
+
producer = BusEventProducer(bus)
|
|
51
|
+
await producer._produce(OrderPlacedEvent(order_id="abc"), channel="orders")
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
---
|
|
55
|
+
|
|
56
|
+
## How channels map to NATS
|
|
57
|
+
|
|
58
|
+
Unlike Kafka — where each channel is a topic — NATS channels are **subjects
|
|
59
|
+
under a single JetStream stream's wildcard**:
|
|
60
|
+
|
|
61
|
+
| varco concept | NATS concept |
|
|
62
|
+
|---------------|--------------|
|
|
63
|
+
| stream (`stream_name`) | one JetStream stream capturing `{subject_prefix}.>` |
|
|
64
|
+
| channel `"orders"` | subject `{subject_prefix}.{channel_prefix}orders` |
|
|
65
|
+
| `durable_name` | base name for durable consumers (≈ Kafka consumer group) |
|
|
66
|
+
| `CHANNEL_ALL` | local-only filter — opens **no** consumer |
|
|
67
|
+
|
|
68
|
+
The bus creates the backing stream automatically on `start()` when
|
|
69
|
+
`auto_create_stream=True` (the default).
|
|
70
|
+
|
|
71
|
+
---
|
|
72
|
+
|
|
73
|
+
## Delivery semantics
|
|
74
|
+
|
|
75
|
+
`NatsEventBus` mirrors `KafkaEventBus`: JetStream redelivery is the
|
|
76
|
+
**broker-level** safety net, while **handler-level** retries are the job of
|
|
77
|
+
varco's `@listen(retry_policy=..., dlq=...)` machinery.
|
|
78
|
+
|
|
79
|
+
| `delivery_semantics` | Behaviour |
|
|
80
|
+
|----------------------|-----------|
|
|
81
|
+
| `at_most_once` | Message acked **before** dispatch. Crash → message lost. No duplicates. |
|
|
82
|
+
| `at_least_once` *(default)* | Message acked **after** dispatch. Crash before ack → JetStream redelivers. |
|
|
83
|
+
| `exactly_once` | As `at_least_once` + every publish carries `Nats-Msg-Id = event.event_id`, so JetStream drops producer-retry duplicates within `duplicate_window`. |
|
|
84
|
+
|
|
85
|
+
```python
|
|
86
|
+
from varco_nats import NatsDeliverySemantics
|
|
87
|
+
|
|
88
|
+
config = NatsEventBusSettings(
|
|
89
|
+
servers="nats://localhost:4222",
|
|
90
|
+
delivery_semantics=NatsDeliverySemantics.EXACTLY_ONCE,
|
|
91
|
+
)
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
---
|
|
95
|
+
|
|
96
|
+
## Configuration
|
|
97
|
+
|
|
98
|
+
All settings are read from environment variables with the `VARCO_NATS_` prefix:
|
|
99
|
+
|
|
100
|
+
```bash
|
|
101
|
+
VARCO_NATS_SERVERS=nats://nats.internal:4222
|
|
102
|
+
VARCO_NATS_STREAM_NAME=orders-events
|
|
103
|
+
VARCO_NATS_SUBJECT_PREFIX=orders
|
|
104
|
+
VARCO_NATS_DURABLE_NAME=order-service
|
|
105
|
+
VARCO_NATS_DELIVERY_SEMANTICS=at_least_once
|
|
106
|
+
VARCO_NATS_CHANNEL_PREFIX=prod.
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
```python
|
|
110
|
+
config = NatsEventBusSettings.from_env()
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
For structured connection/security config (TLS, user/password, token), use
|
|
114
|
+
`NatsConnectionSettings` with the `NATS_` prefix:
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
NATS_SERVERS=nats://nats1:4222,nats://nats2:4222
|
|
118
|
+
NATS_SSL__CA_CERT=/etc/ssl/nats-ca.pem
|
|
119
|
+
NATS_AUTH__TYPE=basic
|
|
120
|
+
NATS_AUTH__USERNAME=alice
|
|
121
|
+
NATS_AUTH__PASSWORD=secret
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
```python
|
|
125
|
+
from varco_nats import NatsConnectionSettings
|
|
126
|
+
|
|
127
|
+
conn = NatsConnectionSettings.from_env()
|
|
128
|
+
config = NatsEventBusSettings(connect_kwargs=conn.to_nats_kwargs())
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
---
|
|
132
|
+
|
|
133
|
+
## Stream management
|
|
134
|
+
|
|
135
|
+
`NatsStreamManager` administers the backing JetStream stream:
|
|
136
|
+
|
|
137
|
+
```python
|
|
138
|
+
from varco_nats import NatsStreamManager, NatsChannelManagerSettings
|
|
139
|
+
|
|
140
|
+
settings = NatsChannelManagerSettings(servers="nats://localhost:4222")
|
|
141
|
+
async with NatsStreamManager(settings) as manager:
|
|
142
|
+
await manager.declare_channel("orders") # ensures the backing stream
|
|
143
|
+
exists = await manager.channel_exists("orders") # has the subject any message?
|
|
144
|
+
channels = await manager.list_channels() # channels carrying messages
|
|
145
|
+
await manager.delete_channel("orders") # purge that channel's messages
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
---
|
|
149
|
+
|
|
150
|
+
## Dead letter queue
|
|
151
|
+
|
|
152
|
+
`NatsDLQ` stores exhausted events in a dedicated **WorkQueue-retention**
|
|
153
|
+
JetStream stream — so `count()` returns the *exact* pending-entry count:
|
|
154
|
+
|
|
155
|
+
```python
|
|
156
|
+
from varco_nats import NatsDLQ
|
|
157
|
+
|
|
158
|
+
async with NatsDLQ(settings=NatsEventBusSettings()) as dlq:
|
|
159
|
+
# Usually wired automatically via @listen(dlq=dlq):
|
|
160
|
+
class OrderConsumer(EventConsumer):
|
|
161
|
+
@listen(
|
|
162
|
+
OrderPlacedEvent,
|
|
163
|
+
channel="orders",
|
|
164
|
+
retry_policy=RetryPolicy(max_attempts=3),
|
|
165
|
+
dlq=dlq,
|
|
166
|
+
)
|
|
167
|
+
async def on_order(self, event: OrderPlacedEvent) -> None: ...
|
|
168
|
+
|
|
169
|
+
# Relay:
|
|
170
|
+
entries = await dlq.pop_batch(limit=10)
|
|
171
|
+
for entry in entries:
|
|
172
|
+
await alert_ops(entry)
|
|
173
|
+
await dlq.ack(entry.entry_id)
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
---
|
|
177
|
+
|
|
178
|
+
## Dependency injection (Providify)
|
|
179
|
+
|
|
180
|
+
```python
|
|
181
|
+
from varco_nats.di import bootstrap
|
|
182
|
+
from varco_core.event import AbstractEventBus
|
|
183
|
+
|
|
184
|
+
container = bootstrap() # scans varco_nats
|
|
185
|
+
bus = await container.aget(AbstractEventBus) # NatsEventBus singleton
|
|
186
|
+
# ...
|
|
187
|
+
await container.ashutdown() # stops the bus via @PreDestroy
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
Install the DLQ explicitly when needed:
|
|
191
|
+
|
|
192
|
+
```python
|
|
193
|
+
from varco_nats.dlq import NatsDLQConfiguration
|
|
194
|
+
from varco_core.event.dlq import AbstractDeadLetterQueue
|
|
195
|
+
|
|
196
|
+
await container.ainstall(NatsDLQConfiguration)
|
|
197
|
+
dlq = await container.aget(AbstractDeadLetterQueue)
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
---
|
|
201
|
+
|
|
202
|
+
## Running tests
|
|
203
|
+
|
|
204
|
+
```bash
|
|
205
|
+
# Unit tests — no broker required (nats-py is faked)
|
|
206
|
+
uv run pytest varco_nats/tests/
|
|
207
|
+
|
|
208
|
+
# Integration tests — require Docker (a real NATS server is started)
|
|
209
|
+
uv run pytest varco_nats/tests/ -m integration
|
|
210
|
+
# or
|
|
211
|
+
VARCO_RUN_INTEGRATION=1 uv run pytest varco_nats/tests/
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
---
|
|
215
|
+
|
|
216
|
+
## Migration 1.x → 2.0
|
|
217
|
+
|
|
218
|
+
The no-op `@Configuration` aliases (`NatsEventBusConfiguration`,
|
|
219
|
+
`NatsChannelManagerConfiguration`) were removed. Register the bus via scan:
|
|
220
|
+
|
|
221
|
+
```python
|
|
222
|
+
# Before (1.x)
|
|
223
|
+
await container.ainstall(NatsEventBusConfiguration)
|
|
224
|
+
|
|
225
|
+
# After (2.0)
|
|
226
|
+
from varco_nats.di import bootstrap
|
|
227
|
+
bootstrap(container) # or: container.scan("varco_nats", recursive=True)
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
The opt-in `NatsDLQConfiguration` is unchanged — still `await container.ainstall(...)`.
|
|
231
|
+
|
|
232
|
+
---
|
|
233
|
+
|
|
234
|
+
## License
|
|
235
|
+
|
|
236
|
+
Apache-2.0
|