Example #1
0
        /// <inheritdoc />
        public async Task <CopyFileResult> CopyToAsync(
            OperationContext context,
            ContentLocation sourceLocation,
            Stream destinationStream,
            CopyOptions options)
        {
            // Extract host and port from machine location
            (string host, int port) = ExtractHostInfo(sourceLocation.Machine);

            // Contact hard-coded port on source
            try
            {
                // ResourcePoolV2 may throw TimeoutException if the connection fails.
                // Wrapping this error and converting it to an "error code".

                return(await _clientCache.UseWithInvalidationAsync(context, host, port, async (nestedContext, clientWrapper) =>
                {
                    var result = await clientWrapper.Value.CopyToAsync(nestedContext, sourceLocation.Hash, destinationStream, options);
                    InvalidateResourceIfNeeded(nestedContext, options, result, clientWrapper);
                    return result;
                }));
            }
            catch (ResultPropagationException e)
            {
                if (e.Result.Exception != null)
                {
                    return(GrpcCopyClient.CreateResultFromException(e.Result.Exception));
                }

                return(new CopyFileResult(CopyResultCode.Unknown, e.Result));
            }
            catch (Exception e)
            {
                return(new CopyFileResult(CopyResultCode.Unknown, e));
            }
        }