# 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
```python
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`](https://faststream.airt.ai/latest/getting-started/middlewares/exception/)
built into FastStream.
Raw data
{
"_id": null,
"home_page": "https://github.com/ulbwa/faststream-deadline-propagation",
"name": "faststream-deadline-propagation",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.11",
"maintainer_email": null,
"keywords": null,
"author": "\u0443\u043b\u044c\u0431\u0430",
"author_email": "ulbwa@icloud.com",
"download_url": "https://files.pythonhosted.org/packages/68/b6/49a3b8f14e640f84846466d2a9896ff3657bd6ba5c1cef03dc85da9fe701/faststream_deadline_propagation-0.1.2.tar.gz",
"platform": null,
"description": "# faststream-deadline-propagation\n\nA middleware for the FastStream framework that provides deadline-propagation in Remote Procedure\nCall requests. It ensures that messages are processed within a specified timeout period, raising\nan exception if the deadline is exceeded.\n\n## Features\n\n- **Deadline Processing Middleware**: Ensures that message processing is completed within a \nspecified deadline.\n- **Deadline Publishing Middleware**: Adds a deadline header to messages being published, \nensuring they are processed within the specified timeout.\n- **Customizable Header**: Allows customization of the header used for deadlines.\n- **Exception Handling**: Raises a `DeadlineOccurred` exception if the deadline is exceeded.\n\n## Example\n\n```python\nimport asyncio\nfrom datetime import datetime, timedelta\nfrom typing import Any\n\nfrom faststream import FastStream\nfrom faststream.nats import NatsBroker, NatsRouter\n\nfrom faststream_deadline_propagation import (\n DeadlineCountdown,\n DeadlineProcessMiddleware,\n DeadlinePublishMiddleware,\n)\n\nrpc_router = NatsRouter(\n middlewares=(\n DeadlineProcessMiddleware.make_middleware(),\n # Your other middlewares here\n )\n)\nbroker = NatsBroker(\n middlewares=(\n # Your other middlewares here\n DeadlinePublishMiddleware.make_middleware(),\n )\n)\n\n\n@rpc_router.subscriber(\"something\")\nasync def do_nothing(message: Any, countdown: DeadlineCountdown):\n await asyncio.sleep(1)\n\n # current timeout in seconds\n current_timeout: float = countdown()\n print(current_timeout)\n\n\nbroker.include_routers(rpc_router)\napp = FastStream(broker)\n\n\n@app.after_startup\nasync def publisher():\n # You can specify a timeout and the DeadlinePublishMiddleware will automatically\n # add the header\n await broker.request(123, \"something\", timeout=3)\n\n # Or explicitly specify the header with your deadline, in this case, the header value\n # will not be overridden\n await broker.request(\n 123,\n \"something\",\n timeout=3,\n headers={\"x-deadline\": (datetime.now() + timedelta(seconds=1)).isoformat()},\n )\n```\n\nTo handle the `DeadlineOccurred` error and serialize the response, you should use the \n[`ExceptionMiddleware`](https://faststream.airt.ai/latest/getting-started/middlewares/exception/) \nbuilt into FastStream.\n",
"bugtrack_url": null,
"license": null,
"summary": "A middleware for the FastStream framework that provides deadline-propagation in Remote Procedure Call requests",
"version": "0.1.2",
"project_urls": {
"Homepage": "https://github.com/ulbwa/faststream-deadline-propagation",
"Repository": "https://github.com/ulbwa/faststream-deadline-propagation"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "f640fc07947663ec4863db8d228e665800f5a3548c210b8703467df3bf4082aa",
"md5": "06d4c38b649769b80a2b3492fb8eb8f7",
"sha256": "7140c8b94290a44772969ba3d6537966d5f767fd3cfc4f9c1cfccaaada602c3b"
},
"downloads": -1,
"filename": "faststream_deadline_propagation-0.1.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "06d4c38b649769b80a2b3492fb8eb8f7",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.11",
"size": 6087,
"upload_time": "2024-09-14T15:41:06",
"upload_time_iso_8601": "2024-09-14T15:41:06.816482Z",
"url": "https://files.pythonhosted.org/packages/f6/40/fc07947663ec4863db8d228e665800f5a3548c210b8703467df3bf4082aa/faststream_deadline_propagation-0.1.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "68b649a3b8f14e640f84846466d2a9896ff3657bd6ba5c1cef03dc85da9fe701",
"md5": "2a8da8078d2798fc4f5c85ab5bd5f325",
"sha256": "af7d2f8f4de65346c808b8863756c536fa0e7562a2b7014ca32d320f40eb0e50"
},
"downloads": -1,
"filename": "faststream_deadline_propagation-0.1.2.tar.gz",
"has_sig": false,
"md5_digest": "2a8da8078d2798fc4f5c85ab5bd5f325",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.11",
"size": 3528,
"upload_time": "2024-09-14T15:41:08",
"upload_time_iso_8601": "2024-09-14T15:41:08.408496Z",
"url": "https://files.pythonhosted.org/packages/68/b6/49a3b8f14e640f84846466d2a9896ff3657bd6ba5c1cef03dc85da9fe701/faststream_deadline_propagation-0.1.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-09-14 15:41:08",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "ulbwa",
"github_project": "faststream-deadline-propagation",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "faststream-deadline-propagation"
}