Skip to content

[automated] Merge branch 'release/3.1' => 'blazor-wasm' #17120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
14 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions NuGet.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
<configuration>
<packageSources>
<clear />
<!--Begin: Package sources managed by Dependency Flow automation. Do not edit the sources below.-->
<add key="darc-pub-dotnet-core-setup-7d57652" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/darc-pub-dotnet-core-setup-7d57652f/nuget/v3/index.json" />
<!--End: Package sources managed by Dependency Flow automation. Do not edit the sources above.-->
<add key="dotnet-core" value="https://dotnetfeed.blob.core.windows.net/dotnet-core/index.json" />
<add key="dotnet-tools" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json" />
<add key="aspnet-blazor" value="https://dotnetfeed.blob.core.windows.net/aspnet-blazor/index.json" />
Expand Down
17 changes: 12 additions & 5 deletions src/Components/Components/src/Routing/RouteConstraint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Globalization;

namespace Microsoft.AspNetCore.Components.Routing
{
internal abstract class RouteConstraint
{
private static readonly IDictionary<string, RouteConstraint> _cachedConstraints
= new Dictionary<string, RouteConstraint>();
// note: the things that prevent this cache from growing unbounded is that
// we're the only caller to this code path, and the fact that there are only
// 8 possible instances that we create.
//
// The values passed in here for parsing are always static text defined in route attributes.
private static readonly ConcurrentDictionary<string, RouteConstraint> _cachedConstraints
= new ConcurrentDictionary<string, RouteConstraint>();

public abstract bool Match(string pathSegment, out object convertedValue);

Expand All @@ -30,8 +35,10 @@ public static RouteConstraint Parse(string template, string segment, string cons
var newInstance = CreateRouteConstraint(constraint);
if (newInstance != null)
{
_cachedConstraints[constraint] = newInstance;
return newInstance;
// We've done to the work to create the constraint now, but it's possible
// we're competing with another thread. GetOrAdd can ensure only a single
// instance is returned so that any extra ones can be GC'ed.
return _cachedConstraints.GetOrAdd(constraint, newInstance);
}
else
{
Expand Down
36 changes: 36 additions & 0 deletions src/Components/Components/test/Routing/RouteConstraintTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Xunit;

namespace Microsoft.AspNetCore.Components.Routing
{
public class RouteConstraintTest
{
[Fact]
public void Parse_CreatesDifferentConstraints_ForDifferentKinds()
{
// Arrange
var original = RouteConstraint.Parse("ignore", "ignore", "int");

// Act
var another = RouteConstraint.Parse("ignore", "ignore", "guid");

// Assert
Assert.NotSame(original, another);
}

[Fact]
public void Parse_CachesCreatedConstraint_ForSameKind()
{
// Arrange
var original = RouteConstraint.Parse("ignore", "ignore", "int");

// Act
var another = RouteConstraint.Parse("ignore", "ignore", "int");

// Assert
Assert.Same(original, another);
}
}
}