Skip to content

Commit 07524ea

Browse files
authored
Refactor Routes initialization for thread-safety and performance (#186)
- Replace Dictionary with ConcurrentDictionary for EnableRoute and Routes - Combine two separate aggregations into single-pass processing - Use tuple structure (EnableRoute, Routes) to group related data during aggregation - Replace dictionary indexer assignments with TryAdd method calls - Update references from enableRoute to seed.EnableRoute within aggregation - Convert aggregation lambdas to static for performance optimization - Add System.Collections.Concurrent using statement
1 parent b073715 commit 07524ea

File tree

1 file changed

+35
-33
lines changed

1 file changed

+35
-33
lines changed

src/Devlead.Testing.MockHttp/Routes.cs

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Net;
44
using System.Text.Json.Serialization;
55
using Microsoft.Extensions.DependencyInjection;
6+
using System.Collections.Concurrent;
67

78
namespace Devlead.Testing.MockHttp;
89

@@ -77,40 +78,41 @@ static ImmutableArray<Route> GetRoutes()
7778
return _routes ??= ImmutableArray.Create(System.Text.Json.JsonSerializer.Deserialize<Route[]>(Resources<T>.GetString("Routes.json") ?? throw new ArgumentNullException("routesJson")) ?? throw new ArgumentNullException("routes"));
7879
}
7980
}
80-
var routes = GetRoutes();
81-
82-
83-
var enableRoute = routes
84-
.Aggregate(
85-
new Dictionary<(
86-
HttpMethod Method,
87-
string AbsoluteUri
88-
),
89-
Action<bool>
90-
>(),
91-
(seed, value) =>
92-
{
93-
void Enable(bool enabled) => value.Request.Disabled = !enabled;
94-
foreach (var method in value.Request.Methods)
95-
{
96-
seed[(method, value.Request.AbsoluteUri)] = Enable;
97-
}
9881

99-
return seed;
100-
},
101-
seed => seed.ToImmutableDictionary()
102-
);
82+
var routes = GetRoutes();
10383

10484
var result =
10585
routes
10686
.Aggregate(
107-
new Dictionary<(
108-
HttpMethod Method,
109-
string AbsoluteUri
110-
),
111-
Func<HttpRequestMessage, HttpResponseMessage>
112-
>(),
113-
(seed, value) =>
87+
(
88+
EnableRoute: routes
89+
.Aggregate(
90+
new ConcurrentDictionary<(
91+
HttpMethod Method,
92+
string AbsoluteUri
93+
),
94+
Action<bool>
95+
>(),
96+
static (seed, value) =>
97+
{
98+
void Enable(bool enabled) => value.Request.Disabled = !enabled;
99+
foreach (var method in value.Request.Methods)
100+
{
101+
seed.TryAdd((method, value.Request.AbsoluteUri), Enable);
102+
}
103+
104+
return seed;
105+
},
106+
seed => seed.ToImmutableDictionary()
107+
),
108+
Routes: new ConcurrentDictionary<(
109+
HttpMethod Method,
110+
string AbsoluteUri
111+
),
112+
Func<HttpRequestMessage, HttpResponseMessage>
113+
>()
114+
),
115+
static (seed, value) =>
114116
{
115117
static HttpResponseMessage AddHeaders(HttpResponseMessage response, Dictionary<string, string[]> headers)
116118
{
@@ -184,7 +186,7 @@ static HttpResponseMessage AddHeaders(HttpResponseMessage response, Dictionary<s
184186
{
185187
foreach (var enableRequest in response.EnableRequests)
186188
{
187-
if (enableRoute.TryGetValue((enableRequest.Method, enableRequest.AbsoluteUri), out var enable))
189+
if (seed.EnableRoute.TryGetValue((enableRequest.Method, enableRequest.AbsoluteUri), out var enable))
188190
{
189191
enable(true);
190192
}
@@ -195,7 +197,7 @@ static HttpResponseMessage AddHeaders(HttpResponseMessage response, Dictionary<s
195197
{
196198
foreach (var disableRequest in response.DisableRequests)
197199
{
198-
if (enableRoute.TryGetValue((disableRequest.Method, disableRequest.AbsoluteUri), out var enable))
200+
if (seed.EnableRoute.TryGetValue((disableRequest.Method, disableRequest.AbsoluteUri), out var enable))
199201
{
200202
enable(false);
201203
}
@@ -214,12 +216,12 @@ static HttpResponseMessage AddHeaders(HttpResponseMessage response, Dictionary<s
214216

215217
foreach (var method in value.Request.Methods)
216218
{
217-
seed[(method, value.Request.AbsoluteUri)] = responseFunc;
219+
seed.Routes.TryAdd((method, value.Request.AbsoluteUri), responseFunc);
218220
}
219221

220222
return seed;
221223
},
222-
seed => seed.ToImmutableDictionary()
224+
static seed => seed.Routes.ToImmutableDictionary()
223225
);
224226

225227
return result;

0 commit comments

Comments
 (0)