-
Notifications
You must be signed in to change notification settings - Fork 5.1k
ClientModel: AsyncResultCollection<T> and SSE event collection implementation #43840
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
annelo-msft
merged 53 commits into
Azure:main
from
annelo-msft:clientmodel-sse-nodispose
May 14, 2024
Merged
Changes from all commits
Commits
Show all changes
53 commits
Select commit
Hold shift + click to select a range
54f822b
Add async and sync enumerable client results
annelo-msft d239e17
Implement IAsyncDisposable
annelo-msft 115295a
Merge remote-tracking branch 'upstream/main' into clientmodel-asyncen…
annelo-msft 3b12f1f
Remove constrant and disposable; update ClientResult so response can …
annelo-msft dbf18ad
rename and upate tests
annelo-msft a798fd9
Merge remote-tracking branch 'upstream/main' into clientmodel-asyncen…
annelo-msft 72951b9
Merge remote-tracking branch 'upstream/main' into clientmodel-asyncen…
annelo-msft 63e1351
initial addition of files from https://github.com/joseharriaga/openai…
annelo-msft f604598
make it build
annelo-msft d3bd6b8
hello world test
annelo-msft 3fca028
bootstrap more tests
annelo-msft 4ea146d
more internal tests
annelo-msft 262c78d
adding enumerator tests; haven't figured out the batch piece yet
annelo-msft 984499d
Make batch test pass
annelo-msft f26a89b
remove collection-event functionality and add tests for public type
annelo-msft 647f1a9
reshuffle
annelo-msft 3189fb3
Add mock convenience SSE type to give POC of lazy request sending
annelo-msft 1a0c861
add tests of delayed request
annelo-msft d5cbc8e
Add BinaryData factory method
annelo-msft 2d71562
Merge remote-tracking branch 'upstream/main' into clientmodel-sse-nod…
annelo-msft 0beaeea
remove funcs for creating enumerators
annelo-msft fc84db1
renames
annelo-msft 5a1bd13
postpone call to protocol method from convenience APIs
annelo-msft 75e3e8a
implement IAsyncDisposable correctly
annelo-msft 80e6ee5
initial pass over cancellation token
annelo-msft c27581d
Per FDG, throw OperationCanceledException if cancellation token is ca…
annelo-msft a04f543
remove factory method taking Func<T> and provide example of layering …
annelo-msft d6d4375
rename internal types and WIP adding reader tests
annelo-msft 6fa96cb
nits
annelo-msft 1fbd038
parameterize terminal event; TBD to provide virtual method on collect…
annelo-msft f577d2e
Merge remote-tracking branch 'upstream/main' into clientmodel-sse-nod…
annelo-msft f342162
WIP: nits
annelo-msft 4ac87a2
WIP: added concatenation of data lines per SSE spec
annelo-msft dab0852
updates and bug fixes
annelo-msft 1941f2c
add tests and update per SSE spec
annelo-msft dee2173
Merge remote-tracking branch 'upstream/main' into clientmodel-sse-nod…
annelo-msft 58ac2a3
WIP: refactor to reuse field processing across sync and async methods
annelo-msft 20a3b81
Merge remote-tracking branch 'upstream/main' into clientmodel-sse-nod…
annelo-msft 88d5da1
make look a little more like the BCL type proposal
annelo-msft d2776d8
simplify field implementation a bit
annelo-msft 2758377
cosmetic reworking of creating an event from a pending event
annelo-msft eae0caa
Remove factory method from public API; move MockSseClient to Tests.In…
annelo-msft b8f70f7
update API; reimplement mock client implementations without internal …
annelo-msft a592c2c
Add sync client result collection abstraction
annelo-msft 88de121
tidy up and add tests
annelo-msft 3bf665b
add default constructor to ClientResult
annelo-msft f87933e
more tidy-up
annelo-msft 43dbbc2
rename and add refdocs
annelo-msft caebaea
comments
annelo-msft 67392c1
Merge remote-tracking branch 'upstream/main' into clientmodel-sse-nod…
annelo-msft 727185c
pr fb
annelo-msft df2df9e
rework last event id and retry per BCL design shift
annelo-msft af84592
add CHANGELOG entry
annelo-msft 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
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
43 changes: 43 additions & 0 deletions
43
sdk/core/System.ClientModel/src/Convenience/AsyncResultCollectionOfT.cs
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,43 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.ClientModel.Primitives; | ||
| using System.Collections.Generic; | ||
| using System.Threading; | ||
|
|
||
| namespace System.ClientModel; | ||
|
|
||
| /// <summary> | ||
| /// Represents a collection of results returned from a cloud service operation. | ||
| /// </summary> | ||
| public abstract class AsyncResultCollection<T> : ClientResult, IAsyncEnumerable<T> | ||
| { | ||
| /// <summary> | ||
| /// Create a new instance of <see cref="AsyncResultCollection{T}"/>. | ||
| /// </summary> | ||
| /// <remarks>If no <see cref="PipelineResponse"/> is provided when the | ||
| /// <see cref="ClientResult"/> instance is created, it is expected that | ||
| /// a derived type will call <see cref="ClientResult.SetRawResponse(PipelineResponse)"/> | ||
| /// prior to a user calling <see cref="ClientResult.GetRawResponse"/>. | ||
| /// This constructor is indended for use by collection implementations that | ||
| /// postpone sending a request until <see cref="GetAsyncEnumerator(CancellationToken)"/> | ||
| /// is called. Such implementations will typically be returned from client | ||
| /// convenience methods so that callers of the methods don't need to | ||
| /// dispose the return value. </remarks> | ||
| protected internal AsyncResultCollection() : base() | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Create a new instance of <see cref="AsyncResultCollection{T}"/>. | ||
| /// </summary> | ||
| /// <param name="response">The <see cref="PipelineResponse"/> holding the | ||
| /// items in the collection, or the first set of the items in the collection. | ||
| /// </param> | ||
| protected internal AsyncResultCollection(PipelineResponse response) : base(response) | ||
| { | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public abstract IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default); | ||
| } |
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
45 changes: 45 additions & 0 deletions
45
sdk/core/System.ClientModel/src/Convenience/ResultCollectionOfT.cs
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,45 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.ClientModel.Primitives; | ||
| using System.Collections; | ||
| using System.Collections.Generic; | ||
|
|
||
| namespace System.ClientModel; | ||
|
|
||
| /// <summary> | ||
| /// Represents a collection of results returned from a cloud service operation. | ||
| /// </summary> | ||
| public abstract class ResultCollection<T> : ClientResult, IEnumerable<T> | ||
| { | ||
| /// <summary> | ||
| /// Create a new instance of <see cref="ResultCollection{T}"/>. | ||
| /// </summary> | ||
| /// <remarks>If no <see cref="PipelineResponse"/> is provided when the | ||
| /// <see cref="ClientResult"/> instance is created, it is expected that | ||
| /// a derived type will call <see cref="ClientResult.SetRawResponse(PipelineResponse)"/> | ||
| /// prior to a user calling <see cref="ClientResult.GetRawResponse"/>. | ||
| /// This constructor is indended for use by collection implementations that | ||
| /// postpone sending a request until <see cref="GetEnumerator()"/> | ||
| /// is called. Such implementations will typically be returned from client | ||
| /// convenience methods so that callers of the methods don't need to | ||
| /// dispose the return value. </remarks> | ||
| protected internal ResultCollection() : base() | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Create a new instance of <see cref="ResultCollection{T}"/>. | ||
| /// </summary> | ||
| /// <param name="response">The <see cref="PipelineResponse"/> holding the | ||
| /// items in the collection, or the first set of the items in the collection. | ||
| /// </param> | ||
| protected internal ResultCollection(PipelineResponse response) : base(response) | ||
| { | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public abstract IEnumerator<T> GetEnumerator(); | ||
|
|
||
| IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); | ||
| } |
82 changes: 82 additions & 0 deletions
82
sdk/core/System.ClientModel/src/Internal/SSE/AsyncServerSentEventEnumerable.cs
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,82 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace System.ClientModel.Internal; | ||
|
|
||
| /// <summary> | ||
| /// Represents a collection of SSE events that can be enumerated as a C# async stream. | ||
| /// </summary> | ||
| internal class AsyncServerSentEventEnumerable : IAsyncEnumerable<ServerSentEvent> | ||
| { | ||
| private readonly Stream _contentStream; | ||
|
|
||
| public AsyncServerSentEventEnumerable(Stream contentStream) | ||
| { | ||
| Argument.AssertNotNull(contentStream, nameof(contentStream)); | ||
|
|
||
| _contentStream = contentStream; | ||
|
|
||
| LastEventId = string.Empty; | ||
| ReconnectionInterval = Timeout.InfiniteTimeSpan; | ||
| } | ||
|
|
||
| public string LastEventId { get; private set; } | ||
|
|
||
| public TimeSpan ReconnectionInterval { get; private set; } | ||
|
|
||
| public IAsyncEnumerator<ServerSentEvent> GetAsyncEnumerator(CancellationToken cancellationToken = default) | ||
| { | ||
| return new AsyncServerSentEventEnumerator(_contentStream, this, cancellationToken); | ||
| } | ||
|
|
||
| private sealed class AsyncServerSentEventEnumerator : IAsyncEnumerator<ServerSentEvent> | ||
| { | ||
| private readonly ServerSentEventReader _reader; | ||
| private readonly AsyncServerSentEventEnumerable _enumerable; | ||
| private readonly CancellationToken _cancellationToken; | ||
|
|
||
| public ServerSentEvent Current { get; private set; } | ||
|
|
||
| public AsyncServerSentEventEnumerator(Stream contentStream, | ||
| AsyncServerSentEventEnumerable enumerable, | ||
| CancellationToken cancellationToken = default) | ||
| { | ||
| _reader = new(contentStream); | ||
| _enumerable = enumerable; | ||
| _cancellationToken = cancellationToken; | ||
| } | ||
|
|
||
| public async ValueTask<bool> MoveNextAsync() | ||
| { | ||
| ServerSentEvent? nextEvent = await _reader.TryGetNextEventAsync(_cancellationToken).ConfigureAwait(false); | ||
| _enumerable.LastEventId = _reader.LastEventId; | ||
| _enumerable.ReconnectionInterval = _reader.ReconnectionInterval; | ||
|
|
||
| if (nextEvent.HasValue) | ||
| { | ||
| Current = nextEvent.Value; | ||
| return true; | ||
| } | ||
|
|
||
| Current = default; | ||
| return false; | ||
| } | ||
|
|
||
| public ValueTask DisposeAsync() | ||
| { | ||
| // The creator of the enumerable has responsibility for disposing | ||
| // the content stream passed to the enumerable constructor. | ||
|
|
||
| #if NET6_0_OR_GREATER | ||
| return ValueTask.CompletedTask; | ||
| #else | ||
| return new ValueTask(); | ||
| #endif | ||
| } | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.