Share routing rules among all requests #211
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Small performance improvement plus it very likely closes #166 (it solved it for us at least).
By placing the
Rules
creation inside the lambda, the previous implementation required to read the routing rules config file for each request, adding unnecessary latency. More importantly, it was not thread-safe, as therulesEngine
andruleFactory
were shared among all requests, only theRules
creation wasn't.The
ruleFactory
is of classMVELRuleFactory
, which is stateful - it hasRuleDefinitionReader reader
andParserContext parserContext
as private members. Creating theRules
changes that state, so it should not be shared between concurrent threads.Rules
OTOH is a read-only class after it's created (it's just iterated over by theDefaultRulesEngine
). Given that the rules are the same for all requests, and that theRules
usage is thread-safe, it makes sense to share it across all of them.Since deploying this our profiling tool stopped showing the RoutingGroupSelector (which was consuming around 1-2% before) and we stopped seeing the error described in #166 .