A middleware for the FastStream framework that provides deadline-propagation in Remote Procedure Call requests
Project description
faststream-deadline-propagation
A middleware for the FastStream framework that provides deadline-propagation in Remote Procedure Call requests. It ensures that messages are processed within a specified timeout period, raising an exception if the deadline is exceeded.
Features
- Deadline Processing Middleware: Ensures that message processing is completed within a specified deadline.
- Deadline Publishing Middleware: Adds a deadline header to messages being published, ensuring they are processed within the specified timeout.
- Customizable Header: Allows customization of the header used for deadlines.
- Exception Handling: Raises a
DeadlineOccurred
exception if the deadline is exceeded.
Example
import asyncio
from datetime import datetime, timedelta
from typing import Any
from faststream import FastStream
from faststream.nats import NatsBroker, NatsRouter
from faststream_deadline_propagation import (
DeadlineCountdown,
DeadlineProcessMiddleware,
DeadlinePublishMiddleware,
)
rpc_router = NatsRouter(
middlewares=(
DeadlineProcessMiddleware.make_middleware(),
# Your other middlewares here
)
)
broker = NatsBroker(
middlewares=(
# Your other middlewares here
DeadlinePublishMiddleware.make_middleware(),
)
)
@rpc_router.subscriber("something")
async def do_nothing(message: Any, countdown: DeadlineCountdown):
await asyncio.sleep(1)
# current timeout in seconds
current_timeout: float = countdown()
print(current_timeout)
broker.include_routers(rpc_router)
app = FastStream(broker)
@app.after_startup
async def publisher():
# You can specify a timeout and the DeadlinePublishMiddleware will automatically
# add the header
await broker.request(123, "something", timeout=3)
# Or explicitly specify the header with your deadline, in this case, the header value
# will not be overridden
await broker.request(
123,
"something",
timeout=3,
headers={"x-deadline": (datetime.now() + timedelta(seconds=1)).isoformat()},
)
To handle the DeadlineOccurred
error and serialize the response, you should use the
ExceptionMiddleware
built into FastStream.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file faststream_deadline_propagation-0.1.2.tar.gz
.
File metadata
- Download URL: faststream_deadline_propagation-0.1.2.tar.gz
- Upload date:
- Size: 3.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.5 Darwin/24.1.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | af7d2f8f4de65346c808b8863756c536fa0e7562a2b7014ca32d320f40eb0e50 |
|
MD5 | 2a8da8078d2798fc4f5c85ab5bd5f325 |
|
BLAKE2b-256 | 68b649a3b8f14e640f84846466d2a9896ff3657bd6ba5c1cef03dc85da9fe701 |
File details
Details for the file faststream_deadline_propagation-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: faststream_deadline_propagation-0.1.2-py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.5 Darwin/24.1.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7140c8b94290a44772969ba3d6537966d5f767fd3cfc4f9c1cfccaaada602c3b |
|
MD5 | 06d4c38b649769b80a2b3492fb8eb8f7 |
|
BLAKE2b-256 | f640fc07947663ec4863db8d228e665800f5a3548c210b8703467df3bf4082aa |