feat(Event streams): add support for cancellation and IAsyncEnumerable#3543
Closed
tlecomte wants to merge 1 commit intoaws:main-stagingfrom
Closed
feat(Event streams): add support for cancellation and IAsyncEnumerable#3543tlecomte wants to merge 1 commit intoaws:main-stagingfrom
tlecomte wants to merge 1 commit intoaws:main-stagingfrom
Conversation
Author
|
The issue checklist mentions updating the documentation. I think that it may apply here, and I'm happy to help, but I would need a pointer to know where the source for the doc is maintained. |
1 task
d27cdac to
0887038
Compare
0887038 to
b60de19
Compare
Author
|
Removed the unrelated changes in BedrockRuntime.sln. |
Contributor
|
@tlecomte Thanks for the contribution, we just happened to receive another PR shortly after yours that also addresses the same problem you're running into. We'll review that PR instead as it targets the next major version of the SDK and it allows us to use newer features in the .NET Framework (your PR would need to account for .NET 3.5 as well, for example)." This means that this feature will be available in V4 of the SDK. |
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
Adjust
EnumerableEventStreamto add support for cancellation and forIAsyncEnumerable:IEnumerableEventStreaminherits fromIAsyncEnumerable. That means that we can useawait foreachon it, for example.EnumerableEventStreamimplements it with a newGetAsyncEnumeratormethod that mimics the existingGetEnumerator, but usesReadFromStreamAsyncso that it does not block the calling thread while waiting on the incoming network stream.For example, to process the response stream from Bedrock
ConverseStreamRequest, before this change we had to do:The problem above is that
foreach (var item in response.Stream.AsEnumerable())is blocking the current .NET thread while waiting for the events to arrive, so it could cause thread pool exhaustion.With the changes in this pull request, we can write instead:
Notice the
await foreachthat is possible now thatIEnumerableEventStreamimplementsIAsyncEnumerable. The benefit is that the .NET thread is not blocked.It is also easy to add support for cancellation. For example:
Motivation and Context
The semantics of AWS event streams map well to .NET
IAsyncEnumerable, but it's hard to build anIAsyncEnumerablefrom such a stream.EnumerableEventStreamdoes provide an asyncStartProcessingAsyncmethod, but only a synchronous enumerator. So the happy path with the current API is to use a synchronous enumerator, which has the downside of blocking the current .NET thread while enumerating the events. This can lead to the thread pool starvation.This addresses #3542
Testing
Added an integration test. To run it, the test code needs to be adjusted to comment out the
Ignoreattribute, give credentials and a region.Screenshots (if appropriate)
Types of changes
Checklist
License