Skip to content

Streaming Interop Followup Items #33916

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

Merged
merged 7 commits into from
Jul 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions src/Components/Server/src/Circuits/RemoteJSDataStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ public static async ValueTask<RemoteJSDataStream> CreateRemoteJSDataStreamAsync(
RemoteJSRuntime runtime,
IJSStreamReference jsStreamReference,
long totalLength,
long maxBufferSize,
long maximumIncomingBytes,
TimeSpan jsInteropDefaultCallTimeout,
long pauseIncomingBytesThreshold = -1,
long resumeIncomingBytesThreshold = -1,
CancellationToken cancellationToken = default)
{
// Enforce minimum 1 kb, maximum 50 kb, SignalR message size.
Expand All @@ -54,7 +55,7 @@ public static async ValueTask<RemoteJSDataStream> CreateRemoteJSDataStreamAsync(
throw new ArgumentException($"SignalR MaximumIncomingBytes must be at least 1 kb.");

var streamId = runtime.RemoteJSDataStreamNextInstanceId++;
var remoteJSDataStream = new RemoteJSDataStream(runtime, streamId, totalLength, maxBufferSize, jsInteropDefaultCallTimeout, cancellationToken);
var remoteJSDataStream = new RemoteJSDataStream(runtime, streamId, totalLength, jsInteropDefaultCallTimeout, pauseIncomingBytesThreshold, resumeIncomingBytesThreshold, cancellationToken);
await runtime.InvokeVoidAsync("Blazor._internal.sendJSDataStream", jsStreamReference, streamId, chunkSize);
return remoteJSDataStream;
}
Expand All @@ -63,8 +64,9 @@ private RemoteJSDataStream(
RemoteJSRuntime runtime,
long streamId,
long totalLength,
long maxBufferSize,
TimeSpan jsInteropDefaultCallTimeout,
long pauseIncomingBytesThreshold,
long resumeIncomingBytesThreshold,
CancellationToken cancellationToken)
{
_runtime = runtime;
Expand All @@ -78,10 +80,16 @@ private RemoteJSDataStream(

_runtime.RemoteJSDataStreamInstances.Add(_streamId, this);

_pipe = new Pipe(new PipeOptions(pauseWriterThreshold: maxBufferSize, resumeWriterThreshold: maxBufferSize / 2));
_pipe = new Pipe(new PipeOptions(pauseWriterThreshold: pauseIncomingBytesThreshold, resumeWriterThreshold: resumeIncomingBytesThreshold));
_pipeReaderStream = _pipe.Reader.AsStream();
PipeReader = _pipe.Reader;
}

/// <summary>
/// Gets a <see cref="PipeReader"/> to directly read data sent by the JavaScript client.
/// </summary>
public PipeReader PipeReader { get; }

private async Task<bool> ReceiveData(long chunkId, byte[] chunk, string error)
{
try
Expand Down Expand Up @@ -199,13 +207,23 @@ private async Task ThrowOnTimeout()
if (!_disposed && (DateTimeOffset.UtcNow >= _lastDataReceivedTime.Add(_jsInteropDefaultCallTimeout)))
{
// Dispose of the stream if a chunk isn't received within the jsInteropDefaultCallTimeout.
var timeoutException = new TimeoutException("Did not receive any data in the alloted time.");
var timeoutException = new TimeoutException("Did not receive any data in the allotted time.");
await CompletePipeAndDisposeStream(timeoutException);
_runtime.RaiseUnhandledException(timeoutException);
}
}

internal async Task CompletePipeAndDisposeStream(Exception? ex = null)
/// <summary>
/// For testing purposes only.
///
/// Triggers the timeout on the next check.
/// </summary>
internal void InvalidateLastDataReceivedTimeForTimeout()
{
_lastDataReceivedTime = _lastDataReceivedTime.Subtract(_jsInteropDefaultCallTimeout);
}

private async Task CompletePipeAndDisposeStream(Exception? ex = null)
{
await _pipe.Writer.CompleteAsync(ex);
Dispose(true);
Expand Down
4 changes: 2 additions & 2 deletions src/Components/Server/src/Circuits/RemoteJSRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ public void MarkPermanentlyDisconnected()
_clientProxy = null;
}

protected override async Task<Stream> ReadJSDataAsStreamAsync(IJSStreamReference jsStreamReference, long totalLength, long maxBufferSize, CancellationToken cancellationToken)
=> await RemoteJSDataStream.CreateRemoteJSDataStreamAsync(this, jsStreamReference, totalLength, maxBufferSize, _maximumIncomingBytes, _options.JSInteropDefaultCallTimeout, cancellationToken);
protected override async Task<Stream> ReadJSDataAsStreamAsync(IJSStreamReference jsStreamReference, long totalLength, long pauseIncomingBytesThreshold = -1, long resumeIncomingBytesThreshold = -1, CancellationToken cancellationToken = default)
=> await RemoteJSDataStream.CreateRemoteJSDataStreamAsync(this, jsStreamReference, totalLength, _maximumIncomingBytes, _options.JSInteropDefaultCallTimeout, pauseIncomingBytesThreshold, resumeIncomingBytesThreshold, cancellationToken);

public static class Log
{
Expand Down
Loading