-
Notifications
You must be signed in to change notification settings - Fork 13
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
Update EvaluatorBuilder cache to be thread safe #62
Conversation
Update EvaluatorBuilder to add locking around cache misses to avoid concurrent modification to a HashMap. Migrate all of the code to build a new cache to an inner class to better separate code concerns.
} | ||
msgEval.append(new CelPrograms(compiledPrograms)); | ||
} | ||
private static class DescriptorCacheBuilder { |
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'd recommend viewing the rest of this ignoring whitespace - it is really just moving functions inside this inner static class.
resolver.resolveMessageConstraints(descriptor, EXTENSION_REGISTRY); | ||
if (msgConstraints.getDisabled()) { | ||
return; | ||
synchronized (this) { |
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.
This is the key change - if we have a cache miss we'll check again (in case another thread beat us in the meantime) and if that fails, we'll rebuild a brand new cache map from the current one and the descriptor.
@@ -54,8 +55,7 @@ public class EvaluatorBuilder { | |||
EXTENSION_REGISTRY.add(ValidateProto.oneof); | |||
} | |||
|
|||
private final Map<Descriptor, Evaluator> evaluatorMap = new HashMap<>(); | |||
private final ConstraintResolver resolver = new ConstraintResolver(); | |||
private volatile ImmutableMap<Descriptor, Evaluator> evaluatorCache = ImmutableMap.of(); |
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 don't have to use immutablemap here, but I thought it was a good indication to make it clear that this cache won't be modified - if we have a cache miss we'll create a new map.
Update EvaluatorBuilder to add locking around cache misses to avoid concurrent modification to a HashMap. Migrate all of the code to build a new cache to an inner class to better separate code concerns.
Fixes #50.