apple / apple/swift-openapi-generator
Best practice for deduplicating in-flight requests
- Dominant language
- Swift
- Stars
- 2k
- Forks
- 182
- Avg merge
- 13h 28m
- Merged PRs (30d)
- 5
Description
### Motivation
Hello,
I wanted to ask how to approach with removing duplicated request in flight? I was wondering about creating dedicated Middleware that will catch all simultaneous calls, fire first one and on return share result with other callers. This might work fine, but I faced an issue where the first call is cancelled. This triggers cancel handler in URLSessionTransport that cancels request and returns nothing. This propagates to all callers waiting for the response.
### Proposed solution
In iOS 27 there will be shield mechanism https://developer.apple.com/documentation/swift/withtaskcancellationshield(operation:)
but I need to have compatibility with older iOS versions.
I thought about shared detached Task, but ClientMiddleware protocol assumes that next closure is non escaping, which would be required in order to pass it into a new task.
Other option I would see is to be able to control cancel behaviour in URLSessionTransport. A flag in configuration that determines if canceling task means that dataTask is cancelled/ operation checks for cancellation. Right now call is fired anyway by URLSession, but later checks in e. g. bidirectionalStreamingRequest:
```
try Task.checkCancellation()
return (try HTTPResponse(response), responseBody)
```
makes impossible to react in higher layers. I would like to have more freedom of controlling such behaviour. If task is cancelled, it would make sense to still have a response and its body to evaluate and maybe clean up.
### Alternatives considered
_No response_
### Additional information
_No response_
Contributor guide
Research direction
Start by reading the ClientMiddleware protocol and the cancellation path in URLSessionTransport, especially the behavior around bidirectionalStreamingRequest and Task.checkCancellation(). Compare the proposed cancellation-control and shared-task approaches with the existing nonescaping next closure. The issue does not define an accepted design or a specific test that would establish when the work is done.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- ios, swift
- Domain
- api, mobile-dev, networking
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Needs clarification
- Newbie friendliness
- 38/100