Add async flow.request.stream functions support - #8367
Open
hdk5 wants to merge 1 commit into
Open
Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
Message.streamtransformers currently run synchronously whileHttpStreamprocesses request or response body data. If a transformer performs blocking work, it blocks mitmproxy’s main asyncio event loop. This pauses not only the affected HTTP stream, but also unrelated connections handled by that loop.This PR adds explicit, opt-in concurrency for blocking stream transformers while preserving the existing synchronous fast path.
Existing transformers continue to work unchanged:
Blocking synchronous transformers can now opt into worker-thread execution:
Native asynchronous transformers are also supported:
This works for both
flow.request.streamandflow.response.stream.How asynchronous processing is integrated
The proxy layer command protocol now has a generic
Awaitcommand and correspondingAwaitCompletedevent. When an HTTP stream encounters an awaitable, it yields this blocking command and pauses only the layer that issued it.The connection handler schedules the awaitable as an asyncio task instead of awaiting it inline. This allows the current
HttpStreamto remain paused while unrelated streams and connections continue to be processed. Results and exceptions are returned through the normal command-completion mechanism.run_in_threadmitmproxy.script.run_in_threadconverts supported synchronous callables into regular asynchronous callables:asyncio.to_thread.next()call runs throughasyncio.to_thread, preserving incremental streaming and backpressure instead of consuming the complete generator in a worker thread.Development history
The stream-async-wip branch preserves the more detailed development history. It also contains several earlier designs that were rejected.
Checklist