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

Commit f40e1f9

Browse files
committed
Make string cache configurable
1 parent b1a7712 commit f40e1f9

File tree

7 files changed

+78
-9
lines changed

7 files changed

+78
-9
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,23 +70,25 @@ public abstract partial class Frame : FrameContext, IFrameControl
7070
private readonly Action<IFeatureCollection> _prepareRequest;
7171

7272
private readonly string _pathBase;
73-
protected readonly IStringCache _stringCache = new StringCache();
73+
protected readonly IStringCache _stringCache;
7474

7575
public Frame(ConnectionContext context)
76-
: this(context, remoteEndPoint: null, localEndPoint: null, prepareRequest: null)
76+
: this(context, remoteEndPoint: null, localEndPoint: null, prepareRequest: null, stringCache: null)
7777
{
7878
}
7979

8080
public Frame(ConnectionContext context,
8181
IPEndPoint remoteEndPoint,
8282
IPEndPoint localEndPoint,
83-
Action<IFeatureCollection> prepareRequest)
83+
Action<IFeatureCollection> prepareRequest,
84+
IStringCache stringCache)
8485
: base(context)
8586
{
8687
_remoteEndPoint = remoteEndPoint;
8788
_localEndPoint = localEndPoint;
8889
_prepareRequest = prepareRequest;
8990
_pathBase = context.ServerAddress.PathBase;
91+
_stringCache = stringCache;
9092

9193
FrameControl = this;
9294
Reset();

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Threading.Tasks;
77
using Microsoft.AspNet.Hosting.Server;
88
using Microsoft.AspNet.Http.Features;
9+
using Microsoft.AspNet.Server.Kestrel.Infrastructure;
910
using Microsoft.Extensions.Logging;
1011

1112
namespace Microsoft.AspNet.Server.Kestrel.Http
@@ -16,16 +17,17 @@ public class Frame<TContext> : Frame
1617

1718
public Frame(IHttpApplication<TContext> application,
1819
ConnectionContext context)
19-
: this(application, context, remoteEndPoint: null, localEndPoint: null, prepareRequest: null)
20+
: this(application, context, remoteEndPoint: null, localEndPoint: null, prepareRequest: null, stringCache: null)
2021
{
2122
}
2223

2324
public Frame(IHttpApplication<TContext> application,
2425
ConnectionContext context,
2526
IPEndPoint remoteEndPoint,
2627
IPEndPoint localEndPoint,
27-
Action<IFeatureCollection> prepareRequest)
28-
: base(context, remoteEndPoint, localEndPoint, prepareRequest)
28+
Action<IFeatureCollection> prepareRequest,
29+
IStringCache stringCache)
30+
: base(context, remoteEndPoint, localEndPoint, prepareRequest, stringCache)
2931
{
3032
_application = application;
3133
}

src/Microsoft.AspNet.Server.Kestrel/IKestrelServerInformation.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ public interface IKestrelServerInformation
1111

1212
bool NoDelay { get; set; }
1313

14+
bool StringCacheOnConnection { get; set; }
15+
16+
int StringCacheMaxStrings { get; set; }
17+
18+
int StringCacheMaxStringLength { get; set; }
19+
1420
IConnectionFilter ConnectionFilter { get; set; }
1521
}
1622
}

src/Microsoft.AspNet.Server.Kestrel/KestrelServer.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,15 @@ public void Start<TContext>(IHttpApplication<TContext> application)
6060
{
6161
FrameFactory = (context, remoteEP, localEP, prepareRequest) =>
6262
{
63-
return new Frame<TContext>(application, context, remoteEP, localEP, prepareRequest);
63+
return new Frame<TContext>(
64+
application,
65+
context,
66+
remoteEP,
67+
localEP,
68+
prepareRequest,
69+
information.StringCacheOnConnection ?
70+
new StringCache(information.StringCacheMaxStrings, information.StringCacheMaxStringLength) :
71+
null);
6472
},
6573
AppLifetime = _applicationLifetime,
6674
Log = trace,

src/Microsoft.AspNet.Server.Kestrel/KestrelServerInformation.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public KestrelServerInformation(IConfiguration configuration, int threadCount)
1616
Addresses = GetAddresses(configuration);
1717
ThreadCount = threadCount;
1818
NoDelay = true;
19+
20+
ConfigureStringCache(configuration);
1921
}
2022

2123
public ICollection<string> Addresses { get; }
@@ -24,6 +26,12 @@ public KestrelServerInformation(IConfiguration configuration, int threadCount)
2426

2527
public bool NoDelay { get; set; }
2628

29+
public bool StringCacheOnConnection { get; set; }
30+
31+
public int StringCacheMaxStrings { get; set; }
32+
33+
public int StringCacheMaxStringLength { get; set; }
34+
2735
public IConnectionFilter ConnectionFilter { get; set; }
2836

2937
private static ICollection<string> GetAddresses(IConfiguration configuration)
@@ -39,5 +47,48 @@ private static ICollection<string> GetAddresses(IConfiguration configuration)
3947

4048
return addresses;
4149
}
50+
51+
private void ConfigureStringCache(IConfiguration configuration)
52+
{
53+
bool stringCacheOnConnection;
54+
if (bool.TryParse(configuration["server.stringCacheOnConnection"], out stringCacheOnConnection))
55+
{
56+
StringCacheOnConnection = stringCacheOnConnection;
57+
}
58+
else
59+
{
60+
StringCacheOnConnection = true;
61+
}
62+
int stringCacheMaxStrings;
63+
if (stringCacheOnConnection && int.TryParse(configuration["server.stringCacheMaxStrings"], out stringCacheMaxStrings))
64+
{
65+
if (stringCacheMaxStrings <= 0)
66+
{
67+
throw new ArgumentOutOfRangeException(nameof(stringCacheMaxStrings),
68+
stringCacheMaxStrings,
69+
"StringCacheMaxStrings must be positive.");
70+
}
71+
StringCacheMaxStrings = stringCacheMaxStrings;
72+
}
73+
else
74+
{
75+
StringCacheMaxStrings = 25;
76+
}
77+
int stringCacheMaxStringLength;
78+
if (stringCacheOnConnection && int.TryParse(configuration["server.stringCacheMaxStringLength"], out stringCacheMaxStringLength))
79+
{
80+
if (stringCacheMaxStringLength <= 0)
81+
{
82+
throw new ArgumentOutOfRangeException(nameof(stringCacheMaxStringLength),
83+
stringCacheMaxStringLength,
84+
"StringCacheMaxStringLength must be positive.");
85+
}
86+
StringCacheMaxStringLength = stringCacheMaxStringLength;
87+
}
88+
else
89+
{
90+
StringCacheMaxStringLength = 256;
91+
}
92+
}
4293
}
4394
}

test/Microsoft.AspNet.Server.KestrelTests/TestServer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void Create(RequestDelegate app, ServiceContext context, string serverAdd
3535
{
3636
context.FrameFactory = (connectionContext, remoteEP, localEP, prepareRequest) =>
3737
{
38-
return new Frame<HttpContext>(new DummyApplication(app), connectionContext, remoteEP, localEP, prepareRequest);
38+
return new Frame<HttpContext>(new DummyApplication(app), connectionContext, remoteEP, localEP, prepareRequest, stringCache: null);
3939
};
4040
_engine = new KestrelEngine(context);
4141
_engine.Start(1);

test/Microsoft.AspNet.Server.KestrelTests/TestServiceContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public RequestDelegate App
3131
_app = value;
3232
FrameFactory = (connectionContext, remoteEP, localEP, prepareRequest) =>
3333
{
34-
return new Frame<HttpContext>(new DummyApplication(_app), connectionContext, remoteEP, localEP, prepareRequest);
34+
return new Frame<HttpContext>(new DummyApplication(_app), connectionContext, remoteEP, localEP, prepareRequest, new StringCache(25, 256));
3535
};
3636
}
3737
}

0 commit comments

Comments
 (0)