mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 15:18:14 +08:00
Checkin for caching the template matching for significant route finder performance improvements (#728)
This commit is contained in:

committed by
Marcelo Castagna

parent
ac211886f1
commit
9bbb6364f2
@ -28,7 +28,7 @@ namespace Ocelot.DownstreamRouteFinder.Finder
|
||||
|
||||
foreach (var reRoute in applicableReRoutes)
|
||||
{
|
||||
var urlMatch = _urlMatcher.Match(upstreamUrlPath, upstreamQueryString, reRoute.UpstreamTemplatePattern.Template, reRoute.UpstreamTemplatePattern.ContainsQueryString);
|
||||
var urlMatch = _urlMatcher.Match(upstreamUrlPath, upstreamQueryString, reRoute.UpstreamTemplatePattern);
|
||||
|
||||
if (urlMatch.Data.Match)
|
||||
{
|
||||
|
@ -1,9 +1,10 @@
|
||||
using Ocelot.Responses;
|
||||
|
||||
namespace Ocelot.DownstreamRouteFinder.UrlMatcher
|
||||
{
|
||||
public interface IUrlPathToUrlTemplateMatcher
|
||||
{
|
||||
Response<UrlMatch> Match(string upstreamUrlPath, string upstreamQueryString, string upstreamUrlPathTemplate, bool containsQueryString);
|
||||
}
|
||||
using Ocelot.Responses;
|
||||
using Ocelot.Values;
|
||||
|
||||
namespace Ocelot.DownstreamRouteFinder.UrlMatcher
|
||||
{
|
||||
public interface IUrlPathToUrlTemplateMatcher
|
||||
{
|
||||
Response<UrlMatch> Match(string upstreamUrlPath, string upstreamQueryString, UpstreamPathTemplate pathTemplate);
|
||||
}
|
||||
}
|
||||
|
@ -1,24 +1,23 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using Ocelot.Responses;
|
||||
|
||||
namespace Ocelot.DownstreamRouteFinder.UrlMatcher
|
||||
{
|
||||
public class RegExUrlMatcher : IUrlPathToUrlTemplateMatcher
|
||||
{
|
||||
public Response<UrlMatch> Match(string upstreamUrlPath, string upstreamQueryString, string upstreamUrlPathTemplate, bool containsQueryString)
|
||||
{
|
||||
var regex = new Regex(upstreamUrlPathTemplate);
|
||||
|
||||
if (!containsQueryString)
|
||||
{
|
||||
return regex.IsMatch(upstreamUrlPath)
|
||||
? new OkResponse<UrlMatch>(new UrlMatch(true))
|
||||
: new OkResponse<UrlMatch>(new UrlMatch(false));
|
||||
}
|
||||
|
||||
return regex.IsMatch($"{upstreamUrlPath}{upstreamQueryString}")
|
||||
? new OkResponse<UrlMatch>(new UrlMatch(true))
|
||||
: new OkResponse<UrlMatch>(new UrlMatch(false));
|
||||
}
|
||||
}
|
||||
}
|
||||
using System.Text.RegularExpressions;
|
||||
using Ocelot.Responses;
|
||||
using Ocelot.Values;
|
||||
|
||||
namespace Ocelot.DownstreamRouteFinder.UrlMatcher
|
||||
{
|
||||
public class RegExUrlMatcher : IUrlPathToUrlTemplateMatcher
|
||||
{
|
||||
public Response<UrlMatch> Match(string upstreamUrlPath, string upstreamQueryString, UpstreamPathTemplate pathTemplate)
|
||||
{
|
||||
if (!pathTemplate.ContainsQueryString)
|
||||
{
|
||||
return pathTemplate.Pattern.IsMatch(upstreamUrlPath)
|
||||
? new OkResponse<UrlMatch>(new UrlMatch(true))
|
||||
: new OkResponse<UrlMatch>(new UrlMatch(false));
|
||||
}
|
||||
|
||||
return pathTemplate.Pattern.IsMatch($"{upstreamUrlPath}{upstreamQueryString}")
|
||||
? new OkResponse<UrlMatch>(new UrlMatch(true))
|
||||
: new OkResponse<UrlMatch>(new UrlMatch(false));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,21 +1,28 @@
|
||||
namespace Ocelot.Values
|
||||
{
|
||||
public class UpstreamPathTemplate
|
||||
{
|
||||
public UpstreamPathTemplate(string template, int priority, bool containsQueryString, string originalValue)
|
||||
{
|
||||
Template = template;
|
||||
Priority = priority;
|
||||
ContainsQueryString = containsQueryString;
|
||||
OriginalValue = originalValue;
|
||||
}
|
||||
|
||||
public string Template { get; }
|
||||
|
||||
public int Priority { get; }
|
||||
|
||||
public bool ContainsQueryString { get; }
|
||||
|
||||
public string OriginalValue { get; }
|
||||
}
|
||||
}
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Ocelot.Values
|
||||
{
|
||||
public class UpstreamPathTemplate
|
||||
{
|
||||
public UpstreamPathTemplate(string template, int priority, bool containsQueryString, string originalValue)
|
||||
{
|
||||
Template = template;
|
||||
Priority = priority;
|
||||
ContainsQueryString = containsQueryString;
|
||||
OriginalValue = originalValue;
|
||||
Pattern = template == null ?
|
||||
new Regex("$^", RegexOptions.Compiled | RegexOptions.Singleline) :
|
||||
new Regex(template, RegexOptions.Compiled | RegexOptions.Singleline);
|
||||
}
|
||||
|
||||
public string Template { get; }
|
||||
|
||||
public int Priority { get; }
|
||||
|
||||
public bool ContainsQueryString { get; }
|
||||
|
||||
public string OriginalValue { get; }
|
||||
|
||||
public Regex Pattern { get; }
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user