|
2 | 2 | // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
3 | 3 |
|
4 | 4 | using System;
|
| 5 | +using System.Collections; |
5 | 6 | using System.Collections.Generic;
|
6 | 7 | using System.Diagnostics;
|
| 8 | +using System.Globalization; |
7 | 9 | using System.IO;
|
8 | 10 | using System.Threading;
|
9 | 11 | using System.Threading.Tasks;
|
@@ -79,72 +81,75 @@ public FrameConnection(FrameConnectionContext context)
|
79 | 81 |
|
80 | 82 | public void StartRequestProcessing<TContext>(IHttpApplication<TContext> application)
|
81 | 83 | {
|
82 |
| - _lifetimeTask = ProcessRequestsAsync<TContext>(application); |
| 84 | + _lifetimeTask = ProcessRequestsAsync(application); |
83 | 85 | }
|
84 | 86 |
|
85 | 87 | private async Task ProcessRequestsAsync<TContext>(IHttpApplication<TContext> application)
|
86 | 88 | {
|
87 |
| - try |
| 89 | + using (BeginConnectionScope()) |
88 | 90 | {
|
89 |
| - Log.ConnectionStart(ConnectionId); |
90 |
| - KestrelEventSource.Log.ConnectionStart(this, _context.ConnectionInformation); |
91 |
| - |
92 |
| - AdaptedPipeline adaptedPipeline = null; |
93 |
| - var adaptedPipelineTask = Task.CompletedTask; |
94 |
| - var input = _context.Input.Reader; |
95 |
| - var output = _context.Output; |
96 |
| - |
97 |
| - if (_context.ConnectionAdapters.Count > 0) |
| 91 | + try |
98 | 92 | {
|
99 |
| - adaptedPipeline = new AdaptedPipeline(input, |
100 |
| - output, |
101 |
| - PipeFactory.Create(AdaptedInputPipeOptions), |
102 |
| - PipeFactory.Create(AdaptedOutputPipeOptions), |
103 |
| - Log); |
104 |
| - |
105 |
| - input = adaptedPipeline.Input.Reader; |
106 |
| - output = adaptedPipeline.Output; |
107 |
| - } |
| 93 | + Log.ConnectionStart(ConnectionId); |
| 94 | + KestrelEventSource.Log.ConnectionStart(this, _context.ConnectionInformation); |
108 | 95 |
|
109 |
| - // _frame must be initialized before adding the connection to the connection manager |
110 |
| - CreateFrame(application, input, output); |
| 96 | + AdaptedPipeline adaptedPipeline = null; |
| 97 | + var adaptedPipelineTask = Task.CompletedTask; |
| 98 | + var input = _context.Input.Reader; |
| 99 | + var output = _context.Output; |
111 | 100 |
|
112 |
| - // Do this before the first await so we don't yield control to the transport until we've |
113 |
| - // added the connection to the connection manager |
114 |
| - _context.ServiceContext.ConnectionManager.AddConnection(_context.FrameConnectionId, this); |
115 |
| - _lastTimestamp = _context.ServiceContext.SystemClock.UtcNow.Ticks; |
| 101 | + if (_context.ConnectionAdapters.Count > 0) |
| 102 | + { |
| 103 | + adaptedPipeline = new AdaptedPipeline(input, |
| 104 | + output, |
| 105 | + PipeFactory.Create(AdaptedInputPipeOptions), |
| 106 | + PipeFactory.Create(AdaptedOutputPipeOptions), |
| 107 | + Log); |
| 108 | + |
| 109 | + input = adaptedPipeline.Input.Reader; |
| 110 | + output = adaptedPipeline.Output; |
| 111 | + } |
116 | 112 |
|
117 |
| - if (adaptedPipeline != null) |
118 |
| - { |
119 |
| - // Stream can be null here and run async will close the connection in that case |
120 |
| - var stream = await ApplyConnectionAdaptersAsync(); |
121 |
| - adaptedPipelineTask = adaptedPipeline.RunAsync(stream); |
122 |
| - } |
| 113 | + // _frame must be initialized before adding the connection to the connection manager |
| 114 | + CreateFrame(application, input, output); |
123 | 115 |
|
124 |
| - await _frame.ProcessRequestsAsync(); |
125 |
| - await adaptedPipelineTask; |
126 |
| - await _socketClosedTcs.Task; |
127 |
| - } |
128 |
| - catch (Exception ex) |
129 |
| - { |
130 |
| - Log.LogError(0, ex, $"Unexpected exception in {nameof(FrameConnection)}.{nameof(ProcessRequestsAsync)}."); |
131 |
| - } |
132 |
| - finally |
133 |
| - { |
134 |
| - _context.ServiceContext.ConnectionManager.RemoveConnection(_context.FrameConnectionId); |
135 |
| - DisposeAdaptedConnections(); |
| 116 | + // Do this before the first await so we don't yield control to the transport until we've |
| 117 | + // added the connection to the connection manager |
| 118 | + _context.ServiceContext.ConnectionManager.AddConnection(_context.FrameConnectionId, this); |
| 119 | + _lastTimestamp = _context.ServiceContext.SystemClock.UtcNow.Ticks; |
136 | 120 |
|
137 |
| - if (_frame.WasUpgraded) |
138 |
| - { |
139 |
| - _context.ServiceContext.ConnectionManager.UpgradedConnectionCount.ReleaseOne(); |
| 121 | + if (adaptedPipeline != null) |
| 122 | + { |
| 123 | + // Stream can be null here and run async will close the connection in that case |
| 124 | + var stream = await ApplyConnectionAdaptersAsync(); |
| 125 | + adaptedPipelineTask = adaptedPipeline.RunAsync(stream); |
| 126 | + } |
| 127 | + |
| 128 | + await _frame.ProcessRequestsAsync(); |
| 129 | + await adaptedPipelineTask; |
| 130 | + await _socketClosedTcs.Task; |
140 | 131 | }
|
141 |
| - else |
| 132 | + catch (Exception ex) |
142 | 133 | {
|
143 |
| - _context.ServiceContext.ConnectionManager.NormalConnectionCount.ReleaseOne(); |
| 134 | + Log.LogError(0, ex, $"Unexpected exception in {nameof(FrameConnection)}.{nameof(ProcessRequestsAsync)}."); |
144 | 135 | }
|
| 136 | + finally |
| 137 | + { |
| 138 | + _context.ServiceContext.ConnectionManager.RemoveConnection(_context.FrameConnectionId); |
| 139 | + DisposeAdaptedConnections(); |
| 140 | + |
| 141 | + if (_frame.WasUpgraded) |
| 142 | + { |
| 143 | + _context.ServiceContext.ConnectionManager.UpgradedConnectionCount.ReleaseOne(); |
| 144 | + } |
| 145 | + else |
| 146 | + { |
| 147 | + _context.ServiceContext.ConnectionManager.NormalConnectionCount.ReleaseOne(); |
| 148 | + } |
145 | 149 |
|
146 |
| - Log.ConnectionStop(ConnectionId); |
147 |
| - KestrelEventSource.Log.ConnectionStop(this); |
| 150 | + Log.ConnectionStop(ConnectionId); |
| 151 | + KestrelEventSource.Log.ConnectionStop(this); |
| 152 | + } |
148 | 153 | }
|
149 | 154 | }
|
150 | 155 |
|
@@ -454,5 +459,15 @@ public void StopTimingWrite()
|
454 | 459 | _writeTimingWrites--;
|
455 | 460 | }
|
456 | 461 | }
|
| 462 | + |
| 463 | + private IDisposable BeginConnectionScope() |
| 464 | + { |
| 465 | + if (Log.IsEnabled(LogLevel.Critical)) |
| 466 | + { |
| 467 | + return Log.BeginScope(new ConnectionLogScope(ConnectionId)); |
| 468 | + } |
| 469 | + |
| 470 | + return null; |
| 471 | + } |
457 | 472 | }
|
458 | 473 | }
|
0 commit comments