allow cancelling MoveNext operations

pull/12822/head
Jan Tattermusch 7 years ago
parent 8e2e9a6d91
commit bb872c02d9
  1. 5
      src/csharp/Grpc.Core/IAsyncStreamReader.cs
  2. 20
      src/csharp/Grpc.Core/Internal/ClientResponseStream.cs
  3. 11
      src/csharp/Grpc.Core/Internal/ServerRequestStream.cs

@ -41,6 +41,11 @@ namespace Grpc.Core
/// (<c>MoveNext</c> will return <c>false</c>) and the <c>CancellationToken</c>
/// associated with the call will be cancelled to signal the failure.
/// </para>
/// <para>
/// <c>MoveNext()</c> operations can be cancelled via a cancellation token. Cancelling
/// an individual read operation has the same effect as cancelling the entire call
/// (which will also result in the read operation returning prematurely).
/// </para>
/// </summary>
/// <typeparam name="T">The message type.</typeparam>
public interface IAsyncStreamReader<T> : IAsyncEnumerator<T>

@ -49,19 +49,19 @@ namespace Grpc.Core.Internal
public async Task<bool> MoveNext(CancellationToken token)
{
if (token != CancellationToken.None)
var cancellationTokenRegistration = token.CanBeCanceled ? token.Register(() => call.Cancel()) : (IDisposable) null;
using (cancellationTokenRegistration)
{
throw new InvalidOperationException("Cancellation of individual reads is not supported.");
}
var result = await call.ReadMessageAsync().ConfigureAwait(false);
this.current = result;
var result = await call.ReadMessageAsync().ConfigureAwait(false);
this.current = result;
if (result == null)
{
await call.StreamingResponseCallFinishedTask.ConfigureAwait(false);
return false;
if (result == null)
{
await call.StreamingResponseCallFinishedTask.ConfigureAwait(false);
return false;
}
return true;
}
return true;
}
public void Dispose()

@ -49,13 +49,14 @@ namespace Grpc.Core.Internal
public async Task<bool> MoveNext(CancellationToken token)
{
if (token != CancellationToken.None)
var cancellationTokenRegistration = token.CanBeCanceled ? token.Register(() => call.Cancel()) : (IDisposable) null;
using (cancellationTokenRegistration)
{
throw new InvalidOperationException("Cancellation of individual reads is not supported.");
var result = await call.ReadMessageAsync().ConfigureAwait(false);
this.current = result;
return result != null;
}
var result = await call.ReadMessageAsync().ConfigureAwait(false);
this.current = result;
return result != null;
}
public void Dispose()

Loading…
Cancel
Save