-
Notifications
You must be signed in to change notification settings - Fork 73
[RHIDP-11647] Add Interrupt POST Endpoint for /v1/streaming_query #1176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c2b99cb
add streaming_query/interrupt for stopping in-flight requests
Jdubrick 58f6916
address coderabbit review comments
Jdubrick d1922cd
fix import ordering
Jdubrick 71ac9a1
add type param to asyncio task
Jdubrick c614a1f
add request_id to context
Jdubrick 3d38111
move to Singleton pattern and expand response codes
Jdubrick 1c47a01
update tests, avoid global registry in tests
Jdubrick 4f4d69f
address coderabbit concerns
Jdubrick a98a4d2
clear test registry with dedicated user ids and request ids
Jdubrick 15b82a1
fix formatting issues
Jdubrick File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| """Endpoint for interrupting in-progress streaming query requests.""" | ||
|
|
||
| from typing import Annotated, Any | ||
|
|
||
| from fastapi import APIRouter, Depends, HTTPException | ||
|
|
||
| from authentication import get_auth_dependency | ||
| from authentication.interface import AuthTuple | ||
| from authorization.middleware import authorize | ||
| from models.config import Action | ||
| from models.requests import StreamingInterruptRequest | ||
| from models.responses import ( | ||
| ForbiddenResponse, | ||
| NotFoundResponse, | ||
| StreamingInterruptResponse, | ||
| UnauthorizedResponse, | ||
| ) | ||
| from utils.stream_interrupts import ( | ||
| CancelStreamResult, | ||
| StreamInterruptRegistry, | ||
| get_stream_interrupt_registry, | ||
| ) | ||
|
|
||
| router = APIRouter(tags=["streaming_query_interrupt"]) | ||
|
|
||
| stream_interrupt_responses: dict[int | str, dict[str, Any]] = { | ||
| 200: StreamingInterruptResponse.openapi_response(), | ||
| 401: UnauthorizedResponse.openapi_response( | ||
| examples=["missing header", "missing token"] | ||
| ), | ||
| 403: ForbiddenResponse.openapi_response(examples=["endpoint"]), | ||
| 404: NotFoundResponse.openapi_response(examples=["streaming request"]), | ||
| } | ||
Jdubrick marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| @router.post( | ||
| "/streaming_query/interrupt", | ||
| responses=stream_interrupt_responses, | ||
| summary="Streaming Query Interrupt Endpoint Handler", | ||
| ) | ||
| @authorize(Action.STREAMING_QUERY) | ||
| async def stream_interrupt_endpoint_handler( | ||
| interrupt_request: StreamingInterruptRequest, | ||
| auth: Annotated[AuthTuple, Depends(get_auth_dependency())], | ||
| registry: Annotated[ | ||
| StreamInterruptRegistry, Depends(get_stream_interrupt_registry) | ||
| ], | ||
| ) -> StreamingInterruptResponse: | ||
| """Interrupt an in-progress streaming query by request identifier. | ||
|
|
||
| Parameters: | ||
| interrupt_request: Request payload containing the stream request ID. | ||
| auth: Auth context tuple resolved from the authentication dependency. | ||
| registry: Stream interrupt registry dependency used to cancel streams. | ||
|
|
||
| Returns: | ||
| StreamingInterruptResponse: Confirmation payload when interruption succeeds. | ||
|
|
||
| Raises: | ||
| HTTPException: If no active stream for the given request ID can be interrupted. | ||
| """ | ||
| user_id, _, _, _ = auth | ||
| request_id = interrupt_request.request_id | ||
| cancel_result = registry.cancel_stream(request_id, user_id) | ||
| if cancel_result == CancelStreamResult.NOT_FOUND: | ||
| response = NotFoundResponse( | ||
| resource="streaming request", | ||
| resource_id=request_id, | ||
| ) | ||
| raise HTTPException(**response.model_dump()) | ||
| if cancel_result == CancelStreamResult.FORBIDDEN: | ||
| response = ForbiddenResponse( | ||
| response="User does not have permission to interrupt this streaming request", | ||
| cause=( | ||
| f"User {user_id} does not own streaming request " | ||
| f"with ID {request_id}" | ||
| ), | ||
| ) | ||
| raise HTTPException(**response.model_dump()) | ||
| if cancel_result == CancelStreamResult.ALREADY_DONE: | ||
| return StreamingInterruptResponse( | ||
| request_id=request_id, | ||
| interrupted=False, | ||
| message="Streaming request already completed; nothing to interrupt", | ||
| ) | ||
|
|
||
| return StreamingInterruptResponse( | ||
| request_id=request_id, | ||
| interrupted=True, | ||
| message="Streaming request interrupted", | ||
| ) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You have to extend
examplesof this response model to make it work properly (seemodels/responses.NotFoundResponse).