Skip to main content

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


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.0.tar.gz.

File metadata

File hashes

Hashes for faststream_deadline_propagation-0.1.0.tar.gz
Algorithm Hash digest
SHA256 86658502266f82e35ee8f58c11ca7039a46c75dda0e136afd701b552cd6094b9
MD5 1dde5b6ffacd22e32bd8f9e25cfa5a31
BLAKE2b-256 7bb8e6f96452ef670bf74624bd0ae95b29bf3f4298a1db7b56a3e3770494a5d4

See more details on using hashes here.

File details

Details for the file faststream_deadline_propagation-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for faststream_deadline_propagation-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 da6f6c87c12b6e53f0d3e8424d67f9607fc9a23a1f71e1a9d1a7517517819ae1
MD5 d7b964ce5d676b8dcc7b1d084b9c9a92
BLAKE2b-256 e3f53137c082bbcebfaef433b042511081567c484793b5e1d2147de4ca7baae8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page