-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Null check bug fixes and tweak accessibility of properties on `Transp…
…ortResponse` (#144) `DefaultResponseFactory` was throwing if the response stream was null. This can occur when an exception is thrown when sending the request (e.g., `HttpRequestException`), for example, when the `HttpClient` cannot connect to the endpoint. Rather than throwing a null exception here, we still want to return a response with the original exception attached. In `StreamResponse`, we must safety-check that any linked disposables are not null before attempting to dispose of them. The final change in `TransportResponse` is a tweak for the ingest work. The `BulkStreamingResponse` was initially derived from the `StreamResponse` to share behaviour. However, this causes the `Body` property (stream) to be present on the derived type. As we are handling stream reading internally, this is unnecessary and could produce weird behaviour if the consumer tries to access the stream directly. Instead, `BulkStreamingResponse` derives directly from `TransportResponse`, overriding `LeaveOpen` and handling `LinkedDisposables` in its own `Dispose` method. This means we could potentially seal `StreamResponse` again. However, it might still be helpful for consumers to derive responses from this for advanced scenarios, with the base class doing the right thing during disposal. I am open to thoughts on whether that's likely to happen. @flobernd, were you deriving from this in the client?
- Loading branch information
1 parent
efe6b75
commit 1bd2db0
Showing
6 changed files
with
98 additions
and
55 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
70 changes: 70 additions & 0 deletions
70
src/Elastic.Transport/Responses/Special/StreamResponseBase.cs
This file contains 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,70 @@ | ||
// 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; | ||
using Elastic.Transport.Extensions; | ||
|
||
namespace Elastic.Transport; | ||
|
||
/// <summary> | ||
/// A base class for implementing responses that access the raw response stream. | ||
/// </summary> | ||
public abstract class StreamResponseBase : TransportResponse, IDisposable | ||
{ | ||
/// <inheritdoc/> | ||
protected internal override bool LeaveOpen => true; | ||
|
||
/// <summary> | ||
/// The raw response stream from the HTTP layer. | ||
/// </summary> | ||
/// <remarks> | ||
/// <b>MUST</b> be disposed to release the underlying HTTP connection for reuse. | ||
/// </remarks> | ||
protected Stream Stream { get; } | ||
|
||
/// <summary> | ||
/// Indicates that the response has been disposed and it is not longer safe to access the stream. | ||
/// </summary> | ||
protected bool Disposed { get; private set; } | ||
|
||
/// <inheritdoc cref="StreamResponseBase"/> | ||
public StreamResponseBase(Stream responseStream) | ||
{ | ||
responseStream.ThrowIfNull(nameof(responseStream)); | ||
Stream = responseStream; | ||
} | ||
|
||
/// <summary> | ||
/// Disposes the underlying stream. | ||
/// </summary> | ||
/// <param name="disposing"></param> | ||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (!Disposed) | ||
{ | ||
if (disposing) | ||
{ | ||
Stream?.Dispose(); | ||
|
||
if (LinkedDisposables is not null) | ||
{ | ||
foreach (var disposable in LinkedDisposables) | ||
disposable?.Dispose(); | ||
} | ||
} | ||
|
||
Disposed = true; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Disposes the underlying stream. | ||
/// </summary> | ||
public void Dispose() | ||
{ | ||
Dispose(disposing: true); | ||
GC.SuppressFinalize(this); | ||
} | ||
} |
This file contains 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