redis-stream-queue 0.1.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.
@@ -0,0 +1,15 @@
1
+ .venv/
2
+ __pycache__/
3
+ *.py[cod]
4
+ *.egg-info/
5
+ dist/
6
+ build/
7
+ .pytest_cache/
8
+ htmlcov/
9
+ .coverage
10
+ examples/*.env
11
+ .claude
12
+ **/__pycache__
13
+ *.pyc
14
+ .idea
15
+ .vsc
@@ -0,0 +1,26 @@
1
+ <component name="ProjectRunConfigurationManager">
2
+ <configuration default="false" name="basic_worker" type="PythonConfigurationType" factoryName="Python" nameIsGenerated="true">
3
+ <module name="redis-stream-queue" />
4
+ <option name="ENV_FILES" value="" />
5
+ <option name="INTERPRETER_OPTIONS" value="" />
6
+ <option name="PARENT_ENVS" value="true" />
7
+ <envs>
8
+ <env name="PYTHONUNBUFFERED" value="1" />
9
+ </envs>
10
+ <option name="SDK_HOME" value="" />
11
+ <option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/examples" />
12
+ <option name="IS_MODULE_SDK" value="true" />
13
+ <option name="ADD_CONTENT_ROOTS" value="true" />
14
+ <option name="ADD_SOURCE_ROOTS" value="true" />
15
+ <EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
16
+ <option name="RUN_TOOL" value="" />
17
+ <option name="SCRIPT_NAME" value="$PROJECT_DIR$/examples/basic_worker.py" />
18
+ <option name="PARAMETERS" value="" />
19
+ <option name="SHOW_COMMAND_LINE" value="false" />
20
+ <option name="EMULATE_TERMINAL" value="false" />
21
+ <option name="MODULE_MODE" value="false" />
22
+ <option name="REDIRECT_INPUT" value="false" />
23
+ <option name="INPUT_FILE" value="" />
24
+ <method v="2" />
25
+ </configuration>
26
+ </component>
@@ -0,0 +1,65 @@
1
+ # CLAUDE.md
2
+
3
+ This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
4
+
5
+ ## Commands
6
+
7
+ ```bash
8
+ # Install (editable + dev deps)
9
+ pip install -e ".[dev]"
10
+
11
+ # Run all tests (no real Redis needed — uses fakeredis)
12
+ pytest tests/
13
+
14
+ # Single file
15
+ pytest tests/test_consumer.py
16
+
17
+ # Single test
18
+ pytest tests/test_consumer.py::test_poison_pill_goes_to_dlq -v -s
19
+
20
+ # With coverage
21
+ pytest tests/ --cov=src/redis_stream_queue --cov-report=term-missing
22
+
23
+ # Build distribution
24
+ hatch build
25
+
26
+ # Run example (requires Redis on localhost:6379)
27
+ python examples/basic_worker.py
28
+ ```
29
+
30
+ ## Architecture
31
+
32
+ Single-package library (`src/redis_stream_queue/`) with these layers:
33
+
34
+ **`client.py` — `StreamClient`**
35
+ Thin wrapper over `redis.asyncio`. Owns the connection pool. Exposes raw Redis Stream commands (`XADD`, `XREADGROUP`, `XAUTOCLAIM`, `XACK`, `XPENDING`, `XRANGE`, `XGROUP CREATE`). All other classes go through this. Factory methods: `from_cluster()`, `from_url()`.
36
+
37
+ **`group.py` — `ConsumerConfig` + `ConsumerGroup`**
38
+ `ConsumerConfig` is a dataclass holding all tuning knobs. `ConsumerGroup` holds a class-level `_known: set` registry to avoid redundant `XGROUP CREATE` calls across iterations — entries are removed on `NOGROUP` error so the next `ensure()` re-creates the group. Also handles `stats()`, `health_check()`, `pending_details()`.
39
+
40
+ **`consumer.py` — `StreamConsumer`**
41
+ The main loop (`run()` → `run_once()`). Four-step iteration: ensure group → XREADGROUP → XAUTOCLAIM cursor loop → poison-pill sweep. Maintains a `weakref.WeakSet` class-level `_instances` registry for `all_metrics()`. Tracks `ConsumerMetrics` via `_TpsTracker` (sliding 60 s window).
42
+
43
+ **`producer.py` — `StreamProducer`**
44
+ `push(data)` → serialize → `XADD MAXLEN`. Same weakref registry pattern as consumer for `all_metrics()`. Tracks `ProducerMetrics`.
45
+
46
+ **`retry.py` — `RetryHandler`**
47
+ Called by `StreamConsumer._process_batch()` for decode errors (immediate DLQ + XACK, no retry) and by `handle_poison_pills()` for messages exceeding `max_deliveries` (XPENDING_RANGE → XRANGE per ID → `dlq_handler` → batched XACK).
48
+
49
+ **`message.py`**
50
+ Data classes only: `StreamMessage`, `PendingEntry`, `StreamStats`, `ConsumerInfo`, `ConsumerMetrics`, `ProducerMetrics`, `_TpsTracker`.
51
+
52
+ **`serializers.py`**
53
+ Protocol `Serializer` + `JsonSerializer` (default), `MsgpackSerializer` (optional dep), `PickleSerializer`. Decode failures always go to DLQ — never retried.
54
+
55
+ ## Testing
56
+
57
+ Tests use `fakeredis` — no real Redis instance required. Two `autouse` fixtures in `conftest.py` reset class-level state between tests: `ConsumerGroup._known` and the `WeakSet` instance registries on both `StreamConsumer` and `StreamProducer`. The `make_client` fixture monkeypatches `_get_client()` to return a `FakeRedis` instance.
58
+
59
+ ## Key invariants
60
+
61
+ - `handler` must return `list[str]` of ACKed IDs. Returning `None` logs a warning and skips ACK; returning `[]` leaves all messages in PEL for retry.
62
+ - Serializer must match between producer and consumer — mismatches become decode errors routed to DLQ.
63
+ - Redis Cluster requires stream and DLQ stream to share a hash tag (e.g. `{orders}_main` / `{orders}_dlq`).
64
+ - `all_metrics()` is in-process only — it does not aggregate across pods.
65
+ - `min_idle_claim_ms` should be ≥ 2× maximum handler latency to avoid premature cross-pod reclaim.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Doan Vu Van
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.