Skip to content
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

Cancel on inbound close #627

Closed
wants to merge 10 commits into from
Closed

Cancel on inbound close #627

wants to merge 10 commits into from

Conversation

adam-fowler
Copy link
Member

@adam-fowler adam-fowler commented Dec 5, 2024

Add support for cancelling a request when the inbound is closed.

try await request.cancelOnInboundClose { request in
    try await doThingThatTakesALongTime()
}

There are limitations

  • request.cancelOnInboundClose consumes the request body
  • It takes an escaping closure so cannot be used in a middleware to call the next middleware in the chain.

The way it works. It runs a task group with one task reading the request inbound stream, and the other running the closure. To avoid the request inbound stream being iterated twice a new version of the request is constructed with a new inbound stream that is being fed by the first of the tasks.

If the request inbound stream hits a second http head (ie the next request) then that is stored in the request iteration state attached to Request.

@adam-fowler adam-fowler requested a review from Joannis as a code owner December 5, 2024 10:55
@adam-fowler adam-fowler marked this pull request as draft December 5, 2024 10:55
@@ -60,10 +65,100 @@ public struct Request: Sendable {
self.body = .init(buffer: byteBuffer)
return byteBuffer
}

public func cancelOnInboundClose<Value: Sendable>(_ process: @escaping @Sendable (Request) async throws -> Value) async throws -> Value {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be part of the (Core)RequestContext instead

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Server needs access to this object and it knows nothing about RequestContexts so not sure how we can do this.

@adam-fowler
Copy link
Member Author

Need #629 to really make this useful

Copy link

github-actions bot commented Dec 6, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants