Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Commit a97f6b3

Browse files
committed
Add IHttpConnectionFeature.ConnectionId.
1 parent f89c959 commit a97f6b3

File tree

15 files changed

+113
-117
lines changed

15 files changed

+113
-117
lines changed

src/Microsoft.AspNetCore.Server.Kestrel/Http/Connection.cs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Globalization;
56
using System.Net;
67
using System.Threading;
78
using Microsoft.AspNetCore.Server.Kestrel.Filter;
@@ -24,40 +25,36 @@ public class Connection : ConnectionContext, IConnectionControl
2425
private Frame _frame;
2526
private ConnectionFilterContext _filterContext;
2627
private LibuvStream _libuvStream;
27-
private readonly long _connectionId;
2828

2929
private readonly SocketInput _rawSocketInput;
3030
private readonly SocketOutput _rawSocketOutput;
3131

3232
private readonly object _stateLock = new object();
3333
private ConnectionState _connectionState;
3434

35-
private IPEndPoint _remoteEndPoint;
36-
private IPEndPoint _localEndPoint;
37-
3835
public Connection(ListenerContext context, UvStreamHandle socket) : base(context)
3936
{
4037
_socket = socket;
4138
ConnectionControl = this;
4239

43-
_connectionId = Interlocked.Increment(ref _lastConnectionId);
40+
ConnectionId = Interlocked.Increment(ref _lastConnectionId).ToString(CultureInfo.InvariantCulture);
4441

4542
_rawSocketInput = new SocketInput(Memory2, ThreadPool);
46-
_rawSocketOutput = new SocketOutput(Thread, _socket, Memory2, this, _connectionId, Log, ThreadPool, WriteReqPool);
43+
_rawSocketOutput = new SocketOutput(Thread, _socket, Memory2, this, ConnectionId, Log, ThreadPool, WriteReqPool);
4744
}
4845

4946
public void Start()
5047
{
51-
Log.ConnectionStart(_connectionId);
48+
Log.ConnectionStart(ConnectionId);
5249

5350
// Start socket prior to applying the ConnectionFilter
5451
_socket.ReadStart(_allocCallback, _readCallback, this);
5552

5653
var tcpHandle = _socket as UvTcpHandle;
5754
if (tcpHandle != null)
5855
{
59-
_remoteEndPoint = tcpHandle.GetPeerIPEndPoint();
60-
_localEndPoint = tcpHandle.GetSockIPEndPoint();
56+
RemoteEndPoint = tcpHandle.GetPeerIPEndPoint();
57+
LocalEndPoint = tcpHandle.GetSockIPEndPoint();
6158
}
6259

6360
// Don't initialize _frame until SocketInput and SocketOutput are set to their final values.
@@ -78,6 +75,7 @@ public void Start()
7875
Connection = _libuvStream,
7976
Address = ServerAddress
8077
};
78+
PrepareRequest = _filterContext.PrepareRequest;
8179

8280
try
8381
{
@@ -170,12 +168,12 @@ private void OnRead(UvStreamHandle handle, int status)
170168

171169
if (normalRead)
172170
{
173-
Log.ConnectionRead(_connectionId, readCount);
171+
Log.ConnectionRead(ConnectionId, readCount);
174172
}
175173
else
176174
{
177175
_socket.ReadStop();
178-
Log.ConnectionReadFin(_connectionId);
176+
Log.ConnectionReadFin(ConnectionId);
179177
}
180178

181179
Exception error = null;
@@ -194,18 +192,18 @@ private void OnRead(UvStreamHandle handle, int status)
194192

195193
private Frame CreateFrame()
196194
{
197-
return FrameFactory(this, _remoteEndPoint, _localEndPoint, _filterContext?.PrepareRequest);
195+
return FrameFactory(this);
198196
}
199197

200198
void IConnectionControl.Pause()
201199
{
202-
Log.ConnectionPause(_connectionId);
200+
Log.ConnectionPause(ConnectionId);
203201
_socket.ReadStop();
204202
}
205203

206204
void IConnectionControl.Resume()
207205
{
208-
Log.ConnectionResume(_connectionId);
206+
Log.ConnectionResume(ConnectionId);
209207
_socket.ReadStart(_allocCallback, _readCallback, this);
210208
}
211209

@@ -222,7 +220,7 @@ void IConnectionControl.End(ProduceEndType endType)
222220
}
223221
_connectionState = ConnectionState.Shutdown;
224222

225-
Log.ConnectionWriteFin(_connectionId);
223+
Log.ConnectionWriteFin(ConnectionId);
226224
_rawSocketOutput.End(endType);
227225
break;
228226
case ProduceEndType.ConnectionKeepAlive:
@@ -231,7 +229,7 @@ void IConnectionControl.End(ProduceEndType endType)
231229
return;
232230
}
233231

234-
Log.ConnectionKeepAlive(_connectionId);
232+
Log.ConnectionKeepAlive(ConnectionId);
235233
break;
236234
case ProduceEndType.SocketDisconnect:
237235
if (_connectionState == ConnectionState.Disconnected)
@@ -240,7 +238,7 @@ void IConnectionControl.End(ProduceEndType endType)
240238
}
241239
_connectionState = ConnectionState.Disconnected;
242240

243-
Log.ConnectionDisconnect(_connectionId);
241+
Log.ConnectionDisconnect(ConnectionId);
244242
_rawSocketOutput.End(endType);
245243
break;
246244
}

src/Microsoft.AspNetCore.Server.Kestrel/Http/ConnectionContext.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
5+
using System.Net;
6+
using Microsoft.AspNetCore.Http.Features;
7+
48
namespace Microsoft.AspNetCore.Server.Kestrel.Http
59
{
610
public class ConnectionContext : ListenerContext
@@ -18,10 +22,24 @@ public ConnectionContext(ConnectionContext context) : base(context)
1822
SocketInput = context.SocketInput;
1923
SocketOutput = context.SocketOutput;
2024
ConnectionControl = context.ConnectionControl;
25+
RemoteEndPoint = context.RemoteEndPoint;
26+
LocalEndPoint = context.LocalEndPoint;
27+
ConnectionId = context.ConnectionId;
28+
PrepareRequest = context.PrepareRequest;
2129
}
2230

2331
public SocketInput SocketInput { get; set; }
32+
2433
public ISocketOutput SocketOutput { get; set; }
34+
2535
public IConnectionControl ConnectionControl { get; set; }
36+
37+
public IPEndPoint RemoteEndPoint { get; set; }
38+
39+
public IPEndPoint LocalEndPoint { get; set; }
40+
41+
public string ConnectionId { get; set; }
42+
43+
public Action<IFeatureCollection> PrepareRequest { get; set; }
2644
}
2745
}

src/Microsoft.AspNetCore.Server.Kestrel/Http/Frame.FeatureCollection.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,8 @@ bool IHttpUpgradeFeature.IsUpgradableRequest
272272

273273
int IHttpConnectionFeature.LocalPort { get; set; }
274274

275+
string IHttpConnectionFeature.ConnectionId { get; set; }
276+
275277
object IFeatureCollection.this[Type key]
276278
{
277279
get { return FastFeatureGet(key); }

src/Microsoft.AspNetCore.Server.Kestrel/Http/Frame.cs

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -71,26 +71,11 @@ public abstract partial class Frame : FrameContext, IFrameControl
7171

7272
private HttpVersionType _httpVersion;
7373

74-
private readonly IPEndPoint _localEndPoint;
75-
private readonly IPEndPoint _remoteEndPoint;
76-
private readonly Action<IFeatureCollection> _prepareRequest;
77-
7874
private readonly string _pathBase;
7975

8076
public Frame(ConnectionContext context)
81-
: this(context, remoteEndPoint: null, localEndPoint: null, prepareRequest: null)
82-
{
83-
}
84-
85-
public Frame(ConnectionContext context,
86-
IPEndPoint remoteEndPoint,
87-
IPEndPoint localEndPoint,
88-
Action<IFeatureCollection> prepareRequest)
8977
: base(context)
9078
{
91-
_remoteEndPoint = remoteEndPoint;
92-
_localEndPoint = localEndPoint;
93-
_prepareRequest = prepareRequest;
9479
_pathBase = context.ServerAddress.PathBase;
9580
if (ReuseStreams)
9681
{
@@ -227,13 +212,15 @@ public void Reset()
227212
DuplexStream = null;
228213

229214
var httpConnectionFeature = this as IHttpConnectionFeature;
230-
httpConnectionFeature.RemoteIpAddress = _remoteEndPoint?.Address;
231-
httpConnectionFeature.RemotePort = _remoteEndPoint?.Port ?? 0;
215+
httpConnectionFeature.RemoteIpAddress = RemoteEndPoint?.Address;
216+
httpConnectionFeature.RemotePort = RemoteEndPoint?.Port ?? 0;
217+
218+
httpConnectionFeature.LocalIpAddress = LocalEndPoint?.Address;
219+
httpConnectionFeature.LocalPort = LocalEndPoint?.Port ?? 0;
232220

233-
httpConnectionFeature.LocalIpAddress = _localEndPoint?.Address;
234-
httpConnectionFeature.LocalPort = _localEndPoint?.Port ?? 0;
221+
httpConnectionFeature.ConnectionId = ConnectionId;
235222

236-
_prepareRequest?.Invoke(this);
223+
PrepareRequest?.Invoke(this);
237224

238225
_manuallySetRequestAbortToken = null;
239226
_abortedCts = null;

src/Microsoft.AspNetCore.Server.Kestrel/Http/FrameOfT.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,7 @@ public class Frame<TContext> : Frame
1717

1818
public Frame(IHttpApplication<TContext> application,
1919
ConnectionContext context)
20-
: this(application, context, remoteEndPoint: null, localEndPoint: null, prepareRequest: null)
21-
{
22-
}
23-
24-
public Frame(IHttpApplication<TContext> application,
25-
ConnectionContext context,
26-
IPEndPoint remoteEndPoint,
27-
IPEndPoint localEndPoint,
28-
Action<IFeatureCollection> prepareRequest)
29-
: base(context, remoteEndPoint, localEndPoint, prepareRequest)
20+
: base(context)
3021
{
3122
_application = application;
3223
}

src/Microsoft.AspNetCore.Server.Kestrel/Http/SocketOutput.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class SocketOutput : ISocketOutput
2626
private readonly KestrelThread _thread;
2727
private readonly UvStreamHandle _socket;
2828
private readonly Connection _connection;
29-
private readonly long _connectionId;
29+
private readonly string _connectionId;
3030
private readonly IKestrelTrace _log;
3131
private readonly IThreadPool _threadPool;
3232

@@ -58,7 +58,7 @@ public SocketOutput(
5858
UvStreamHandle socket,
5959
MemoryPool2 memory,
6060
Connection connection,
61-
long connectionId,
61+
string connectionId,
6262
IKestrelTrace log,
6363
IThreadPool threadPool,
6464
Queue<UvWriteReq> writeReqPool)

src/Microsoft.AspNetCore.Server.Kestrel/Infrastructure/IKestrelTrace.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,33 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Infrastructure
55
{
66
public interface IKestrelTrace : ILogger
77
{
8-
void ConnectionStart(long connectionId);
8+
void ConnectionStart(string connectionId);
99

10-
void ConnectionStop(long connectionId);
10+
void ConnectionStop(string connectionId);
1111

12-
void ConnectionRead(long connectionId, int count);
12+
void ConnectionRead(string connectionId, int count);
1313

14-
void ConnectionPause(long connectionId);
14+
void ConnectionPause(string connectionId);
1515

16-
void ConnectionResume(long connectionId);
16+
void ConnectionResume(string connectionId);
1717

18-
void ConnectionReadFin(long connectionId);
18+
void ConnectionReadFin(string connectionId);
1919

20-
void ConnectionWriteFin(long connectionId);
20+
void ConnectionWriteFin(string connectionId);
2121

22-
void ConnectionWroteFin(long connectionId, int status);
22+
void ConnectionWroteFin(string connectionId, int status);
2323

24-
void ConnectionKeepAlive(long connectionId);
24+
void ConnectionKeepAlive(string connectionId);
2525

26-
void ConnectionDisconnect(long connectionId);
26+
void ConnectionDisconnect(string connectionId);
2727

28-
void ConnectionWrite(long connectionId, int count);
28+
void ConnectionWrite(string connectionId, int count);
2929

30-
void ConnectionWriteCallback(long connectionId, int status);
30+
void ConnectionWriteCallback(string connectionId, int status);
3131

32-
void ConnectionError(long connectionId, Exception ex);
32+
void ConnectionError(string connectionId, Exception ex);
3333

34-
void ConnectionDisconnectedWrite(long connectionId, int count, Exception ex);
34+
void ConnectionDisconnectedWrite(string connectionId, int count, Exception ex);
3535

3636
void ApplicationError(Exception ex);
3737
}

0 commit comments

Comments
 (0)