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
Regex improvements #1348
base: develop
Are you sure you want to change the base?
Regex improvements #1348
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's Ok
What if timeout happens? Timeout maybe not good practice here. |
Setting timeout is one of the necessary tasks of using regex. When a timeout occurs, it is best to run the entire project involved and not respond to others. |
Is there some reason for considering |
timeout is not customizable by the customer. Like many other settings. It was 100 ms when I adjusted it according to the tests I had. But we can decide here and change this time. We can set this time to 500 ms. If we do not receive an answer at this time, it is better not to involve the whole project in its implementation and let it respond to other requests. |
9ba1128
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
review done
Dear Mohsen, Why did you merge develop branch from your fork into the feature branch? Now there is no build at all and pipeline cannot run new build. Could you fix/update your fork develop branch please first? After that we will try to fix pipeline triggering by rebasing current feature branch onto develop one. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did Branch Develop on Branch as Rebase. Does this solve this problem?
Now I see develop and it has zero diff, but the branch is not default. ☝️ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
change something for build
bfdecf7
to
4b9516c
Compare
This reverts commit 4b9516c.
Feature branch has been rebased onto develop! Going to start code review... I guess, this PR is not related to an issue, right? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@EngRajabi Mohsen,
I see three major issues with current code:
TimeSpan.FromMilliseconds(100)
. The hardcode of100
is detected. Milliseconds constant should be a constant, and public static. Moving to some common options static class is highly desired.- No catching and processing of the
RegexMatchTimeoutException
object being generated by allRegex
objects. Atry-catch
constructions should be provided. - No unit tests which cover new behavior of
Regex
. So,RegexMatchTimeoutException
exception case should be tested.
Hope this doc will be helpful: Defining a Time-Out Value, to understand current issues.
@@ -107,8 +104,7 @@ private static bool AllRoutesForAggregateExist(FileAggregateRoute fileAggregateR | |||
|
|||
private static bool IsPlaceholderNotDuplicatedIn(string upstreamPathTemplate) | |||
{ | |||
var regExPlaceholder = new Regex("{[^}]+}"); | |||
var matches = regExPlaceholder.Matches(upstreamPathTemplate); | |||
var matches = _regExPlaceholder.Matches(upstreamPathTemplate); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No catching and processing of the RegexMatchTimeoutException
exception.
Will it result to unhandled exception by app domain?
Will it be caught by upper context?
@@ -38,7 +37,7 @@ List<PlaceholderNameAndValue> urlPathPlaceholderNameAndValues | |||
if (values.Data != null) | |||
{ | |||
// dynamic claim | |||
var match = Regex.Match(required.Value, @"^{(?<variable>.+)}$"); | |||
var match = _regexAuthorize.Match(required.Value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No catching and processing of the RegexMatchTimeoutException
exception.
Will it result to unhandled exception by app domain?
Will it be caught by upper context?
private static readonly Regex _secondsRegEx = new Regex("^[0-9]+s", RegexOptions.Compiled, TimeSpan.FromMilliseconds(100)); | ||
private static readonly Regex _minutesRegEx = new Regex("^[0-9]+m", RegexOptions.Compiled, TimeSpan.FromMilliseconds(100)); | ||
private static readonly Regex _hoursRegEx = new Regex("^[0-9]+h", RegexOptions.Compiled, TimeSpan.FromMilliseconds(100)); | ||
private static readonly Regex _daysRegEx = new Regex("^[0-9]+d", RegexOptions.Compiled, TimeSpan.FromMilliseconds(100)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hardcode has detected!
The 100
milliseconds constant should be public static.
return _secondsRegEx.Match(period).Success | ||
|| _minutesRegEx.Match(period).Success | ||
|| _hoursRegEx.Match(period).Success | ||
|| _daysRegEx.Match(period).Success; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No catching and processing of the RegexMatchTimeoutException
exception.
Will it result to unhandled exception by app domain?
Will it be caught by upper context?
[Obsolete("Please set BaseUrl in ocelot.json GlobalConfiguration.BaseUrl")] | ||
public static IConfigurationBuilder AddOcelotBaseUrl(this IConfigurationBuilder builder, string baseUrl) | ||
{ | ||
Regex.CacheSize += 100; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the default, current value of the cache in runtime?
So, why did you increase this property value?
var pathAndQuery = CreateServiceFabricUri(downstreamRequest, downstreamRoute, | ||
templatePlaceholderNameAndValues, response); | ||
|
||
//todo check this works again hope there is a test.. | ||
downstreamRequest.AbsolutePath = pathAndQuery.Path; | ||
downstreamRequest.Query = pathAndQuery.Query; | ||
downstreamRequest.AbsolutePath = pathAndQuery.Path; | ||
downstreamRequest.Query = pathAndQuery.Query; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Zero changes!
Revert to previous code!
@@ -51,7 +49,7 @@ public Response Remove(string key) | |||
=> _placeholders.Remove(key); | |||
|
|||
private static string CleanKey(string key) | |||
=> Regex.Replace(key, @"[{}]", string.Empty, RegexOptions.None); | |||
=> _regex.Replace(key, string.Empty); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No catching and processing of the RegexMatchTimeoutException
exception.
new Regex("$^", RegexOptions.Compiled | RegexOptions.Singleline) : | ||
new Regex(template, RegexOptions.Compiled | RegexOptions.Singleline); | ||
_reg : | ||
_regex.AddOrUpdate(template, new Regex(template, RegexOptions.Compiled | RegexOptions.Singleline, TimeSpan.FromMilliseconds(100)), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No catching and processing of the RegexMatchTimeoutException
exception.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are no unit tests for new approach of using Regex.
Time out should be tested!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regex Time out event should be tested!
@raman-m |
It is better to reply in own thread of each code review issue...
Thanks for improvements! Current behavior has been improved. We've done with that, but... Links to read
And, Could you start fixing code review issues please? |
@WeihanLi commented on Nov 20, 2020:
I wanna develop your idea further! 😉 The problem with the Regarding configuring of timeout value. Well... We could introduce one common configurable value for this option in Best Practices for Regular Expressions in .NET:
What about such kind of |
Proposed changes
Regex
improvements:Regex
class is in mutable)Links
Microsoft Docs: Best Practices for Regular Expressions in .NET | Microsoft Learn