Skip to content
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

Make the FieldQueryMapEncoder encoder thread safe #1369

Merged
merged 3 commits into from Apr 22, 2021

Conversation

Budlee
Copy link
Contributor

@Budlee Budlee commented Feb 24, 2021

Change the caching map to be concurrent and only
insert items that are missing to the map.
Fixes #1257

@@ -56,7 +61,7 @@ private ObjectParamMetadata getMetadata(Class<?> objectType) {
ObjectParamMetadata metadata = classToMetadata.get(objectType);
if (metadata == null) {
metadata = ObjectParamMetadata.parseObjectType(objectType);
classToMetadata.put(objectType, metadata);
classToMetadata.putIfAbsent(objectType, metadata);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this whole method be replaced with:

return classToMetadata.computeIfAbsent(objectType, ObjectParamMetadata::parseObjectType);

???

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it could,
I wasn’t sure on the refactor culture. Some like to make as few changes as possible.

happy to add this in

Copy link
Contributor Author

@Budlee Budlee Feb 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated,
the other change I thought was to the core of this loop to be the following, please let me know if its okay as I woulduse a pair:

return metadata.objectFields.stream()
            .map(this::pairFieldAndOptionalValue)
            .filter(pair -> pair.right.isPresent())
            .collect(Collectors.toMap(pair -> createKeyFromField(pair.left), pair -> pair.right));

Its much smaller (personally I think it reads better), however, I understand it is not to everyones taste.
Some do not like the use of pair and also pairFieldAndOptionalValue would have a hidden side effect where if the field.get(object); fails will throw then EncodeException.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rage-shadowman what are thoughts on this refactor?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it all depends on how field.get fails. The current loop is vulnerable to that now. I don't have any issue with catching there and wrapping in an EncodeException, that fits with the Encoder interface, so it's not a hidden side-effect, so long as you add some comments around the check.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're asking my opinion, I think that any time you simplify the code without breaking anything, you make it better. I think this PR looks more simplified.

As far as that stream usage goes, it looks more difficult to read at-a-glance to me, but the original isn't that easy to read at-a-glance either, and I tend to be biased against all but the simplest of streams usage.

As long as your stream doesn't modify anything from the input metadata, or from one of the earlier map()d records, then there aren't any side effects. Throwing a runtime exception due to an unexpected illegal state sounds reasonable.

Change the caching map to be concurrent and only
insert items that are missing to the map.
Fixes OpenFeign#1257
@kdavisk6 kdavisk6 added the feedback provided Feedback has been provided to the author label Mar 7, 2021
@Budlee
Copy link
Contributor Author

Budlee commented Mar 11, 2021

@rage-shadowman and @kdavisk6 I have updated this now,
Personally I think it is more readible, however, can revert it back if required.

thanks

@kdavisk6
Copy link
Member

@Budlee Done is better than perfect here.

@kdavisk6 kdavisk6 added ready to merge Will be merged if no other member ask for changes and removed feedback provided Feedback has been provided to the author labels Mar 12, 2021
@Budlee
Copy link
Contributor Author

Budlee commented Mar 26, 2021

@kdavisk6 , when does this get merged?

@kdavisk6 kdavisk6 merged commit b53a535 into OpenFeign:master Apr 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready to merge Will be merged if no other member ask for changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FieldQueryMapEncoder may not be thread-safe
3 participants