-
Notifications
You must be signed in to change notification settings - Fork 780
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Call Complete on SubchannelCallTracker only after HttpContent has bee…
…n disposed
- Loading branch information
Showing
2 changed files
with
82 additions
and
6 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
71 changes: 71 additions & 0 deletions
71
src/Grpc.Net.Client/Balancer/Internal/HttpContentWrapper.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,71 @@ | ||
using System.Net; | ||
|
||
namespace Grpc.Net.Client.Balancer.Internal; | ||
|
||
internal sealed class HttpContentWrapper : HttpContent | ||
{ | ||
private readonly HttpContent _inner; | ||
private readonly Action _disposeAction; | ||
private bool _disposed; | ||
|
||
public HttpContentWrapper(HttpContent inner, Action disposeAction) | ||
{ | ||
_inner = inner; | ||
_disposeAction = disposeAction; | ||
|
||
foreach (var kvp in inner.Headers) | ||
{ | ||
Headers.TryAddWithoutValidation(kvp.Key, kvp.Value.ToArray()); | ||
} | ||
} | ||
|
||
#if NET5_0_OR_GREATER | ||
|
||
protected override void SerializeToStream(Stream stream, TransportContext? context, CancellationToken cancellationToken) | ||
{ | ||
using var content = _inner.ReadAsStream(cancellationToken); | ||
content.CopyTo(stream); | ||
} | ||
|
||
protected override async Task SerializeToStreamAsync(Stream stream, TransportContext? context, CancellationToken cancellationToken) | ||
{ | ||
var content = await _inner.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false); | ||
await using (content.ConfigureAwait(false)) | ||
{ | ||
await content.CopyToAsync(stream, cancellationToken).ConfigureAwait(false); | ||
} | ||
} | ||
|
||
#endif | ||
|
||
protected override async Task SerializeToStreamAsync(Stream stream, TransportContext? context) | ||
{ | ||
var content = await _inner.ReadAsStreamAsync().ConfigureAwait(false); | ||
#if NET5_0_OR_GREATER | ||
await using (content.ConfigureAwait(false)) | ||
#else | ||
using (content) | ||
#endif | ||
{ | ||
await content.CopyToAsync(stream).ConfigureAwait(false); | ||
} | ||
} | ||
|
||
protected override bool TryComputeLength(out long length) | ||
{ | ||
length = 0; | ||
return false; | ||
} | ||
|
||
protected override void Dispose(bool disposing) | ||
{ | ||
base.Dispose(disposing); | ||
|
||
if (disposing && !_disposed) | ||
{ | ||
_disposeAction(); | ||
_inner.Dispose(); | ||
_disposed = true; | ||
} | ||
} | ||
} |