Skip to content

Commit

Permalink
Update Grpc.Net.Client to remove ValueTask usage (#2222)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK authored Aug 9, 2023
1 parent d305ee4 commit 4367702
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 74 deletions.
14 changes: 5 additions & 9 deletions src/Grpc.Net.Client/Internal/GrpcCall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,15 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Runtime.ExceptionServices;
using Grpc.Core;
using Grpc.Net.Client.Internal.Http;
using Grpc.Shared;
using Microsoft.Extensions.Logging;
using System.Runtime.ExceptionServices;
#if SUPPORT_LOAD_BALANCING
using Grpc.Net.Client.Balancer.Internal;
#endif

#if NETSTANDARD2_0 || NET462
using ValueTask = System.Threading.Tasks.Task;
#endif

namespace Grpc.Net.Client.Internal;

internal sealed partial class GrpcCall<TRequest, TResponse> : GrpcCall, IGrpcCall<TRequest, TResponse>
Expand Down Expand Up @@ -129,7 +125,7 @@ private HttpContent CreatePushUnaryContent(TRequest request)
? new PushUnaryContent<TRequest, TResponse>(request, WriteAsync)
: new WinHttpUnaryContent<TRequest, TResponse>(request, WriteAsync, this);

ValueTask WriteAsync(TRequest request, Stream stream)
Task WriteAsync(TRequest request, Stream stream)
{
return WriteMessageAsync(stream, request, Options);
}
Expand Down Expand Up @@ -1091,7 +1087,7 @@ private void DeadlineExceeded()
CancelCall(new Status(StatusCode.DeadlineExceeded, string.Empty));
}

internal ValueTask WriteMessageAsync(
internal Task WriteMessageAsync(
Stream stream,
ReadOnlyMemory<byte> message,
CancellationToken cancellationToken)
Expand All @@ -1103,7 +1099,7 @@ internal ValueTask WriteMessageAsync(
cancellationToken);
}

internal ValueTask WriteMessageAsync(
internal Task WriteMessageAsync(
Stream stream,
TRequest message,
CallOptions callOptions)
Expand Down Expand Up @@ -1140,7 +1136,7 @@ internal ValueTask WriteMessageAsync(
return message;
}

public Task WriteClientStreamAsync<TState>(Func<GrpcCall<TRequest, TResponse>, Stream, CallOptions, TState, ValueTask> writeFunc, TState state, CancellationToken cancellationToken)
public Task WriteClientStreamAsync<TState>(Func<GrpcCall<TRequest, TResponse>, Stream, CallOptions, TState, Task> writeFunc, TState state, CancellationToken cancellationToken)
{
return ClientStreamWriter!.WriteAsync(writeFunc, state, cancellationToken);
}
Expand Down
8 changes: 2 additions & 6 deletions src/Grpc.Net.Client/Internal/Http/PushStreamContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,14 @@

using System.Net;

#if NETSTANDARD2_0 || NET462
using ValueTask = System.Threading.Tasks.Task;
#endif

namespace Grpc.Net.Client.Internal.Http;

internal class PushStreamContent<TRequest, TResponse> : HttpContent
where TRequest : class
where TResponse : class
{
private readonly HttpContentClientStreamWriter<TRequest, TResponse> _streamWriter;
private readonly Func<Stream, ValueTask>? _startCallback;
private readonly Func<Stream, Task>? _startCallback;

public PushStreamContent(HttpContentClientStreamWriter<TRequest, TResponse> streamWriter)
{
Expand All @@ -39,7 +35,7 @@ public PushStreamContent(HttpContentClientStreamWriter<TRequest, TResponse> stre

public PushStreamContent(
HttpContentClientStreamWriter<TRequest, TResponse> streamWriter,
Func<Stream, ValueTask>? startCallback) : this(streamWriter)
Func<Stream, Task>? startCallback) : this(streamWriter)
{
_startCallback = startCallback;
}
Expand Down
14 changes: 4 additions & 10 deletions src/Grpc.Net.Client/Internal/Http/PushUnaryContent.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#region Copyright notice and license
#region Copyright notice and license

// Copyright 2019 The gRPC Authors
//
Expand All @@ -19,10 +19,6 @@
using System.Net;
using Grpc.Shared;

#if NETSTANDARD2_0 || NET462
using ValueTask = System.Threading.Tasks.Task;
#endif

namespace Grpc.Net.Client.Internal;

// TODO: Still need generic args?
Expand All @@ -31,9 +27,9 @@ internal class PushUnaryContent<TRequest, TResponse> : HttpContent
where TResponse : class
{
private readonly TRequest _request;
private readonly Func<TRequest, Stream, ValueTask> _startCallback;
private readonly Func<TRequest, Stream, Task> _startCallback;

public PushUnaryContent(TRequest request, Func<TRequest, Stream, ValueTask> startCallback)
public PushUnaryContent(TRequest request, Func<TRequest, Stream, Task> startCallback)
{
_request = request;
_startCallback = startCallback;
Expand All @@ -42,9 +38,7 @@ public PushUnaryContent(TRequest request, Func<TRequest, Stream, ValueTask> star

protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context)
{
#pragma warning disable CA2012 // Use ValueTasks correctly
var writeMessageTask = _startCallback(_request, stream);
#pragma warning restore CA2012 // Use ValueTasks correctly
if (writeMessageTask.IsCompletedSuccessfully())
{
if (GrpcEventSource.Log.IsEnabled())
Expand All @@ -57,7 +51,7 @@ protected override Task SerializeToStreamAsync(Stream stream, TransportContext?
return WriteMessageCore(writeMessageTask);
}

private static async Task WriteMessageCore(ValueTask writeMessageTask)
private static async Task WriteMessageCore(Task writeMessageTask)
{
await writeMessageTask.ConfigureAwait(false);
if (GrpcEventSource.Log.IsEnabled())
Expand Down
16 changes: 5 additions & 11 deletions src/Grpc.Net.Client/Internal/Http/WinHttpUnaryContent.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#region Copyright notice and license
#region Copyright notice and license

// Copyright 2019 The gRPC Authors
//
Expand All @@ -16,12 +16,8 @@

#endregion

using Grpc.Shared;
using System.Net;

#if NETSTANDARD2_0 || NET462
using ValueTask = System.Threading.Tasks.Task;
#endif
using Grpc.Shared;

namespace Grpc.Net.Client.Internal.Http;

Expand All @@ -36,10 +32,10 @@ internal class WinHttpUnaryContent<TRequest, TResponse> : HttpContent
where TResponse : class
{
private readonly TRequest _request;
private readonly Func<TRequest, Stream, ValueTask> _startCallback;
private readonly Func<TRequest, Stream, Task> _startCallback;
private readonly GrpcCall<TRequest, TResponse> _call;

public WinHttpUnaryContent(TRequest request, Func<TRequest, Stream, ValueTask> startCallback, GrpcCall<TRequest, TResponse> call)
public WinHttpUnaryContent(TRequest request, Func<TRequest, Stream, Task> startCallback, GrpcCall<TRequest, TResponse> call)
{
_request = request;
_startCallback = startCallback;
Expand All @@ -49,9 +45,7 @@ public WinHttpUnaryContent(TRequest request, Func<TRequest, Stream, ValueTask> s

protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context)
{
#pragma warning disable CA2012 // Use ValueTasks correctly
var writeMessageTask = _startCallback(_request, stream);
#pragma warning restore CA2012 // Use ValueTasks correctly
if (writeMessageTask.IsCompletedSuccessfully())
{
if (GrpcEventSource.Log.IsEnabled())
Expand All @@ -64,7 +58,7 @@ protected override Task SerializeToStreamAsync(Stream stream, TransportContext?
return WriteMessageCore(writeMessageTask);
}

private static async Task WriteMessageCore(ValueTask writeMessageTask)
private static async Task WriteMessageCore(Task writeMessageTask)
{
await writeMessageTask.ConfigureAwait(false);
if (GrpcEventSource.Log.IsEnabled())
Expand Down
10 changes: 3 additions & 7 deletions src/Grpc.Net.Client/Internal/HttpContentClientStreamWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@
using Grpc.Shared;
using Log = Grpc.Net.Client.Internal.ClientStreamWriterBaseLog;

#if NETSTANDARD2_0 || NET462
using ValueTask = System.Threading.Tasks.Task;
#endif

namespace Grpc.Net.Client.Internal;

[DebuggerDisplay("{DebuggerToString(),nq}")]
Expand Down Expand Up @@ -107,13 +103,13 @@ public override async Task WriteCoreAsync(TRequest message, CancellationToken ca
ctsRegistration?.Dispose();
}

static ValueTask WriteMessageToStream(GrpcCall<TRequest, TResponse> call, Stream writeStream, CallOptions callOptions, TRequest message)
static Task WriteMessageToStream(GrpcCall<TRequest, TResponse> call, Stream writeStream, CallOptions callOptions, TRequest message)
{
return call.WriteMessageAsync(writeStream, message, callOptions);
}
}

public Task WriteAsync<TState>(Func<GrpcCall<TRequest, TResponse>, Stream, CallOptions, TState, ValueTask> writeFunc, TState state, CancellationToken cancellationToken)
public Task WriteAsync<TState>(Func<GrpcCall<TRequest, TResponse>, Stream, CallOptions, TState, Task> writeFunc, TState state, CancellationToken cancellationToken)
{
_call.EnsureNotDisposed();

Expand Down Expand Up @@ -158,7 +154,7 @@ public Task WriteAsync<TState>(Func<GrpcCall<TRequest, TResponse>, Stream, CallO

public GrpcCall<TRequest, TResponse> Call => _call;

public async Task WriteAsyncCore<TState>(Func<GrpcCall<TRequest, TResponse>, Stream, CallOptions, TState, ValueTask> writeFunc, TState state, CancellationToken cancellationToken)
public async Task WriteAsyncCore<TState>(Func<GrpcCall<TRequest, TResponse>, Stream, CallOptions, TState, Task> writeFunc, TState state, CancellationToken cancellationToken)
{
try
{
Expand Down
6 changes: 1 addition & 5 deletions src/Grpc.Net.Client/Internal/IGrpcCall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@
using System.Diagnostics.CodeAnalysis;
using Grpc.Core;

#if NETSTANDARD2_0 || NET462
using ValueTask = System.Threading.Tasks.Task;
#endif

namespace Grpc.Net.Client.Internal;

internal interface IGrpcCall<TRequest, TResponse> : IDisposable, IMethod
Expand All @@ -43,7 +39,7 @@ internal interface IGrpcCall<TRequest, TResponse> : IDisposable, IMethod
void StartDuplexStreaming();

Task WriteClientStreamAsync<TState>(
Func<GrpcCall<TRequest, TResponse>, Stream, CallOptions, TState, ValueTask> writeFunc,
Func<GrpcCall<TRequest, TResponse>, Stream, CallOptions, TState, Task> writeFunc,
TState state,
CancellationToken cancellationToken);

Expand Down
18 changes: 5 additions & 13 deletions src/Grpc.Net.Client/Internal/Retry/RetryCallBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@
using Microsoft.Extensions.Logging;
using Log = Grpc.Net.Client.Internal.Retry.RetryCallBaseLog;

#if NETSTANDARD2_0 || NET462
using ValueTask = System.Threading.Tasks.Task;
#endif

namespace Grpc.Net.Client.Internal.Retry;

internal abstract partial class RetryCallBase<TRequest, TResponse> : IGrpcCall<TRequest, TResponse>
Expand Down Expand Up @@ -185,7 +181,7 @@ private HttpContent CreatePushUnaryContent(TRequest request, GrpcCall<TRequest,
? new PushUnaryContent<TRequest, TResponse>(request, WriteAsync)
: new WinHttpUnaryContent<TRequest, TResponse>(request, WriteAsync, call);

ValueTask WriteAsync(TRequest request, Stream stream)
Task WriteAsync(TRequest request, Stream stream)
{
return WriteNewMessage(call, stream, call.Options, request);
}
Expand All @@ -195,18 +191,14 @@ private PushStreamContent<TRequest, TResponse> CreatePushStreamContent(GrpcCall<
{
return new PushStreamContent<TRequest, TResponse>(clientStreamWriter, async requestStream =>
{
ValueTask writeTask;
Task writeTask;
lock (Lock)
{
Log.SendingBufferedMessages(Logger, BufferedMessages.Count);
if (BufferedMessages.Count == 0)
{
#if NETSTANDARD2_0 || NET462
writeTask = Task.CompletedTask;
#else
writeTask = default;
#endif
}
else
{
Expand All @@ -225,7 +217,7 @@ private PushStreamContent<TRequest, TResponse> CreatePushStreamContent(GrpcCall<
});
}

private async ValueTask WriteBufferedMessages(GrpcCall<TRequest, TResponse> call, Stream requestStream, ReadOnlyMemory<byte>[] bufferedMessages)
private async Task WriteBufferedMessages(GrpcCall<TRequest, TResponse> call, Stream requestStream, ReadOnlyMemory<byte>[] bufferedMessages)
{
for (var i = 0; i < bufferedMessages.Length; i++)
{
Expand Down Expand Up @@ -304,7 +296,7 @@ protected byte[] SerializePayload(GrpcCall<TRequest, TResponse> call, CallOption
}
}

protected async ValueTask WriteNewMessage(GrpcCall<TRequest, TResponse> call, Stream writeStream, CallOptions callOptions, TRequest message)
protected async Task WriteNewMessage(GrpcCall<TRequest, TResponse> call, Stream writeStream, CallOptions callOptions, TRequest message)
{
// Serialize current message and add to the buffer.
ReadOnlyMemory<byte> messageData;
Expand Down Expand Up @@ -402,7 +394,7 @@ protected void SetNewActiveCallUnsynchronized(IGrpcCall<TRequest, TResponse> cal
}
}

Task IGrpcCall<TRequest, TResponse>.WriteClientStreamAsync<TState>(Func<GrpcCall<TRequest, TResponse>, Stream, CallOptions, TState, ValueTask> writeFunc, TState state, CancellationToken cancellationTokens)
Task IGrpcCall<TRequest, TResponse>.WriteClientStreamAsync<TState>(Func<GrpcCall<TRequest, TResponse>, Stream, CallOptions, TState, Task> writeFunc, TState state, CancellationToken cancellationTokens)
{
throw new NotSupportedException();
}
Expand Down
6 changes: 1 addition & 5 deletions src/Grpc.Net.Client/Internal/Retry/StatusGrpcCall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@
using System.Diagnostics.CodeAnalysis;
using Grpc.Core;

#if NETSTANDARD2_0 || NET462
using ValueTask = System.Threading.Tasks.Task;
#endif

namespace Grpc.Net.Client.Internal.Retry;

internal sealed class StatusGrpcCall<TRequest, TResponse> : IGrpcCall<TRequest, TResponse>
Expand Down Expand Up @@ -100,7 +96,7 @@ public void StartUnary(TRequest request)
throw new NotSupportedException();
}

public Task WriteClientStreamAsync<TState>(Func<GrpcCall<TRequest, TResponse>, Stream, CallOptions, TState, ValueTask> writeFunc, TState state, CancellationToken cancellationToken)
public Task WriteClientStreamAsync<TState>(Func<GrpcCall<TRequest, TResponse>, Stream, CallOptions, TState, Task> writeFunc, TState state, CancellationToken cancellationToken)
{
return Task.FromException(new RpcException(_status));
}
Expand Down
8 changes: 2 additions & 6 deletions src/Grpc.Net.Client/Internal/StreamExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@
using Grpc.Shared;
using Microsoft.Extensions.Logging;

#if NETSTANDARD2_0 || NET462
using ValueTask = System.Threading.Tasks.Task;
#endif

namespace Grpc.Net.Client.Internal;

internal static partial class StreamExtensions
Expand Down Expand Up @@ -291,7 +287,7 @@ private static bool ReadCompressedFlag(byte flag)
}
}

public static async ValueTask WriteMessageAsync<TMessage>(
public static async Task WriteMessageAsync<TMessage>(
this Stream stream,
GrpcCall call,
TMessage message,
Expand Down Expand Up @@ -332,7 +328,7 @@ public static async ValueTask WriteMessageAsync<TMessage>(
}
}

public static async ValueTask WriteMessageAsync(
public static async Task WriteMessageAsync(
this Stream stream,
GrpcCall call,
ReadOnlyMemory<byte> data,
Expand Down
3 changes: 1 addition & 2 deletions test/FunctionalTests/Server/CompressionTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#region Copyright notice and license
#region Copyright notice and license

// Copyright 2019 The gRPC Authors
//
Expand Down Expand Up @@ -408,7 +408,6 @@ public async Task SendCompressedMessageWithoutEncodingHeader_ServerErrorResponse
return false;
});


var requestMessage = new HelloRequest
{
Name = "World"
Expand Down

0 comments on commit 4367702

Please sign in to comment.