Skip to content

Conversation

@georgestoupis
Copy link
Contributor

Re: issue #31

@CLAassistant
Copy link

CLAassistant commented Oct 3, 2020

CLA assistant check
All committers have signed the CLA.

Copy link
Contributor

@breedx-nr breedx-nr left a comment

Choose a reason for hiding this comment

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

Thanks @gstoupis for this contribution! This is a nice start.

I left a comment about the race condition in the singleton, but I think it's an easy change.

My instinct would have been to just add a field of JsonFactory to the NewRelicJsonLayout, but this approach (pure singleton) should be fine too.

Thanks!

private static JsonFactory jsonFactory;

static JsonFactory getInstance() {
if (jsonFactory == null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

So it might be a bit pedantic and may not seriously impact real-world applications, but technically there is a race condition here.

Imagine 10 threads, all calling getInstance() at the same time. It is possible for all 10 to pass the null check, then they all create instances and assign them and return them. For some period of time, there will be more than 1 instance of JsonFactory around.

In this case, especially since JsonFactory has no dependencies and is so easy to construct, I think it is easily remedied by changing the static field declaration to:

    private final static JsonFactory jsonFactory = new JsonFactory();

and then you can remove the if() in the getter. Should be safer and simpler that way.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch! I'll change it accordingly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in cb71c26

@georgestoupis
Copy link
Contributor Author

Thanks @gstoupis for this contribution! This is a nice start.

I left a comment about the race condition in the singleton, but I think it's an easy change.

My instinct would have been to just add a field of JsonFactory to the NewRelicJsonLayout, but this approach (pure singleton) should be fine too.

Thanks!

Thanks for the review @breedx-nr! You're right, having it inside the NewRelicJsonLayout would be simpler. However, I took a look around other packages as well, and saw that they had the same logic (a new JsonFactory() in similar places each time).

So I thought that creating a new class and having it provide the singleton JsonFactory could be utilized by these other packages well.

Of course, if you think it'd be better, I can revert the changes and just add a field of JsonFactory to the NewRelicJsonLayout, as you propose. Otherwise, I'll fix the race condition you mentioned above, and leave the rest as is.

What do you think?

@breedx-nr
Copy link
Contributor

I think that your approach is solid and the ability to reuse in other packages is forward-looking, so let's go with that.
Thanks! 🥂

Copy link
Contributor

@breedx-nr breedx-nr left a comment

Choose a reason for hiding this comment

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

Thanks again!

@breedx-nr breedx-nr merged commit 964b73e into newrelic:main Oct 6, 2020
@georgestoupis georgestoupis deleted the issue-31 branch October 6, 2020 20:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants