-
Notifications
You must be signed in to change notification settings - Fork 13
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
Implement StreamResponse
#110
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -118,6 +118,7 @@ private async ValueTask<TResponse> RequestCoreAsync<TResponse>(bool isAsync, Req | |
responseMessage = client.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead, cancellationToken).GetAwaiter().GetResult(); | ||
#endif | ||
|
||
receive = responseMessage; | ||
statusCode = (int)responseMessage.StatusCode; | ||
} | ||
|
||
|
@@ -152,8 +153,11 @@ private async ValueTask<TResponse> RequestCoreAsync<TResponse>(bool isAsync, Req | |
{ | ||
ex = e; | ||
} | ||
using (receive) | ||
using (responseStream ??= Stream.Null) | ||
|
||
var isStreamResponse = typeof(TResponse) == typeof(StreamResponse); | ||
|
||
using (isStreamResponse ? DiagnosticSources.SingletonDisposable : receive) | ||
using (isStreamResponse ? Stream.Null : responseStream ??= Stream.Null) | ||
{ | ||
TResponse response; | ||
|
||
|
@@ -165,6 +169,10 @@ private async ValueTask<TResponse> RequestCoreAsync<TResponse>(bool isAsync, Req | |
response = requestData.ConnectionSettings.ProductRegistration.ResponseBuilder.ToResponse<TResponse> | ||
(requestData, ex, statusCode, responseHeaders, responseStream, mimeType, contentLength, threadPoolStats, tcpStats); | ||
|
||
// Defer disposal of the response message | ||
if (response is StreamResponse sr) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ugly but i think its better to pass If an exception happens in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's fine for me as well! The whole thing is a little bit "spaghetti" unfortunately due to the fact that the |
||
sr.Finalizer = () => receive.Dispose(); | ||
|
||
if (!OpenTelemetry.CurrentSpanIsElasticTransportOwnedAndHasListeners || (!(Activity.Current?.IsAllDataRequested ?? false))) | ||
return response; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Licensed to Elasticsearch B.V under one or more agreements. | ||
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
// See the LICENSE file in the project root for more information | ||
|
||
using System; | ||
using System.IO; | ||
|
||
namespace Elastic.Transport; | ||
|
||
/// <summary> | ||
/// A response that exposes the response <see cref="TransportResponse{T}.Body"/> as <see cref="Stream"/>. | ||
/// <para> | ||
/// Must be disposed after use. | ||
/// </para> | ||
/// </summary> | ||
public sealed class StreamResponse : | ||
TransportResponse<Stream>, | ||
IDisposable | ||
{ | ||
internal Action? Finalizer { get; set; } | ||
|
||
/// <summary> | ||
/// The MIME type of the response, if present. | ||
/// </summary> | ||
public string MimeType { get; } | ||
|
||
/// <inheritdoc cref="StreamResponse"/> | ||
public StreamResponse(Stream body, string? mimeType) | ||
{ | ||
Body = body; | ||
MimeType = mimeType ?? string.Empty; | ||
} | ||
|
||
/// <inheritdoc cref="IDisposable.Dispose"/> | ||
public void Dispose() | ||
{ | ||
Body.Dispose(); | ||
Finalizer?.Invoke(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍