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

Commit e55d528

Browse files
committed
Merge DefaultHttpRequestIdentifierFeature and HttpRequestIdentifierFeature
1 parent 911bd5e commit e55d528

File tree

5 files changed

+62
-72
lines changed

5 files changed

+62
-72
lines changed

Diff for: src/Microsoft.AspNet.Http/DefaultHttpContext.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ private IHttpRequestIdentifierFeature RequestIdentifierFeature
123123
return FeatureHelpers.GetOrCreate<IHttpRequestIdentifierFeature>(
124124
this,
125125
_features,
126-
() => new DefaultHttpRequestIdentifierFeature());
126+
() => new HttpRequestIdentifierFeature());
127127
}
128128
set
129129
{

Diff for: src/Microsoft.AspNet.Http/DefaultHttpRequestIdentifierFeature.cs

-64
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,64 @@
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.Threading;
6+
47
namespace Microsoft.AspNet.Http.Features.Internal
58
{
69
public class HttpRequestIdentifierFeature : IHttpRequestIdentifierFeature
710
{
8-
public virtual string TraceIdentifier { get; set; }
11+
// Base64 encoding - but in ascii sort order for easy text based sorting
12+
private static readonly string _encode32Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
13+
// Seed the _requestId for this application instance with
14+
// the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001
15+
// for a roughly increasing _requestId over restarts
16+
private static long _requestId = DateTime.UtcNow.Ticks;
17+
18+
private string _id = null;
19+
20+
public string TraceIdentifier
21+
{
22+
get
23+
{
24+
// Don't incur the cost of generating the request ID until it's asked for
25+
if (_id == null)
26+
{
27+
_id = GenerateRequestId(Interlocked.Increment(ref _requestId));
28+
}
29+
return _id;
30+
}
31+
set
32+
{
33+
_id = value;
34+
}
35+
}
36+
37+
private static unsafe string GenerateRequestId(long id)
38+
{
39+
// The following routine is ~310% faster than calling long.ToString() on x64
40+
// and ~600% faster than calling long.ToString() on x86 in tight loops of 1 million+ iterations
41+
// See: https://github.com/aspnet/Hosting/pull/385
42+
43+
// stackalloc to allocate array on stack rather than heap
44+
char* charBuffer = stackalloc char[13];
45+
46+
charBuffer[0] = _encode32Chars[(int)(id >> 60) & 31];
47+
charBuffer[1] = _encode32Chars[(int)(id >> 55) & 31];
48+
charBuffer[2] = _encode32Chars[(int)(id >> 50) & 31];
49+
charBuffer[3] = _encode32Chars[(int)(id >> 45) & 31];
50+
charBuffer[4] = _encode32Chars[(int)(id >> 40) & 31];
51+
charBuffer[5] = _encode32Chars[(int)(id >> 35) & 31];
52+
charBuffer[6] = _encode32Chars[(int)(id >> 30) & 31];
53+
charBuffer[7] = _encode32Chars[(int)(id >> 25) & 31];
54+
charBuffer[8] = _encode32Chars[(int)(id >> 20) & 31];
55+
charBuffer[9] = _encode32Chars[(int)(id >> 15) & 31];
56+
charBuffer[10] = _encode32Chars[(int)(id >> 10) & 31];
57+
charBuffer[11] = _encode32Chars[(int)(id >> 5) & 31];
58+
charBuffer[12] = _encode32Chars[(int)id & 31];
59+
60+
// string ctor overload that takes char*
61+
return new string(charBuffer, 0, 13);
62+
}
963
}
1064
}

Diff for: src/Microsoft.AspNet.Http/project.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"url": "git://github.com/aspnet/httpabstractions"
77
},
88
"compilationOptions": {
9-
"warningsAsErrors": true,
10-
"allowUnsafe": true
9+
"warningsAsErrors": true,
10+
"allowUnsafe": true
1111
},
1212
"dependencies": {
1313
"Microsoft.AspNet.Http.Abstractions": "1.0.0-*",

Diff for: test/Microsoft.AspNet.Http.Tests/DeafultHttpRequestIdentifierFeatureTests.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class DeafultHttpRequestIdentifierFeatureTests
1111
[Fact]
1212
public void TraceIdentifier_ReturnsId()
1313
{
14-
var feature = new DefaultHttpRequestIdentifierFeature();
14+
var feature = new HttpRequestIdentifierFeature();
1515

1616
var id = feature.TraceIdentifier;
1717

@@ -21,7 +21,7 @@ public void TraceIdentifier_ReturnsId()
2121
[Fact]
2222
public void TraceIdentifier_ReturnsStableId()
2323
{
24-
var feature = new DefaultHttpRequestIdentifierFeature();
24+
var feature = new HttpRequestIdentifierFeature();
2525

2626
var id1 = feature.TraceIdentifier;
2727
var id2 = feature.TraceIdentifier;
@@ -32,8 +32,8 @@ public void TraceIdentifier_ReturnsStableId()
3232
[Fact]
3333
public void TraceIdentifier_ReturnsUniqueIdForDifferentInstances()
3434
{
35-
var feature1 = new DefaultHttpRequestIdentifierFeature();
36-
var feature2 = new DefaultHttpRequestIdentifierFeature();
35+
var feature1 = new HttpRequestIdentifierFeature();
36+
var feature2 = new HttpRequestIdentifierFeature();
3737

3838
var id1 = feature1.TraceIdentifier;
3939
var id2 = feature2.TraceIdentifier;

0 commit comments

Comments
 (0)