public static ISpanBuilder BeginSpan() { var traceReporter = Configuration.Reporter; if (traceReporter == null) { return(new FakeSpanBuilder()); } var pooledSpan = spanPool.AcquireHandle(); var contextScope = TraceContextScope.Begin(); return(new SpanBuilder(traceReporter, pooledSpan, contextScope, configuration)); }
private async Task <HttpActionStatus> ReadResponseBodyAsync(Request request, HttpWebRequestState state) { try { var contentLength = (int)state.Response.ContentLength; if (contentLength > 0) { state.BodyBuffer = new byte[contentLength]; var totalBytesRead = 0; // (iloktionov): Если буфер размером contentLength не попадет в LOH, можно передать его напрямую для работы с сокетом. // В противном случае лучше использовать небольшой промежуточный буфер из пула, т.к. ссылка на переданный сохранится надолго из-за Keep-Alive. if (contentLength < lohObjectSizeThreshold) { while (totalBytesRead < contentLength) { var bytesToRead = Math.Min(contentLength - totalBytesRead, preferredReadSize); var bytesRead = await state.ResponseStream.ReadAsync(state.BodyBuffer, totalBytesRead, bytesToRead).ConfigureAwait(false); if (bytesRead == 0) { break; } totalBytesRead += bytesRead; } } else { using (var bufferHandle = readBuffersPool.AcquireHandle()) { var buffer = bufferHandle.Resource; while (totalBytesRead < contentLength) { var bytesToRead = Math.Min(contentLength - totalBytesRead, buffer.Length); var bytesRead = await state.ResponseStream.ReadAsync(buffer, 0, bytesToRead).ConfigureAwait(false); if (bytesRead == 0) { break; } Buffer.BlockCopy(buffer, 0, state.BodyBuffer, totalBytesRead, bytesRead); totalBytesRead += bytesRead; } } } if (totalBytesRead < contentLength) { throw new EndOfStreamException($"Response stream ended prematurely. Read only {totalBytesRead} byte(s), but Content-Length specified {contentLength}."); } } else { state.BodyStream = new MemoryStream(); using (var bufferHandle = readBuffersPool.AcquireHandle()) { var buffer = bufferHandle.Resource; while (true) { var bytesRead = await state.ResponseStream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false); if (bytesRead == 0) { break; } state.BodyStream.Write(buffer, 0, bytesRead); } } } return(HttpActionStatus.Success); } catch (Exception error) { if (IsCancellationException(error)) { return(HttpActionStatus.RequestCanceled); } LogReceiveBodyFailure(request, error); return(HttpActionStatus.ReceiveFailure); } }