-
Notifications
You must be signed in to change notification settings - Fork 22
feat: Use singleton JsonFactory instance #31 #32
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
84ae6f8
feat: Use singleton JsonFactory instance #31
georgestoupis f8804a4
chore: remove unused import
georgestoupis 3bbc5e9
test: add unit test for JsonFactoryProviderTest
georgestoupis cb71c26
fix: avoid race condition in JsonFactoryProvider
georgestoupis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
logback/src/main/java/com/newrelic/logging/logback/JsonFactoryProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package com.newrelic.logging.logback; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonFactory; | ||
|
|
||
| public class JsonFactoryProvider { | ||
| private static JsonFactory jsonFactory; | ||
|
|
||
| static JsonFactory getInstance() { | ||
| if (jsonFactory == null) { | ||
| jsonFactory = new JsonFactory(); | ||
| } | ||
| return jsonFactory; | ||
| } | ||
|
|
||
| private JsonFactoryProvider() { | ||
|
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
logback/src/test/java/com/newrelic/logging/logback/JsonFactoryProviderTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package com.newrelic.logging.logback; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonFactory; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertSame; | ||
|
|
||
| class JsonFactoryProviderTest { | ||
|
|
||
| @Test | ||
| void shouldAlwaysGetSameInstance(){ | ||
| JsonFactory instance1 = JsonFactoryProvider.getInstance(); | ||
| JsonFactory instance2 = JsonFactoryProvider.getInstance(); | ||
|
|
||
| assertNotNull(instance1); | ||
| assertNotNull(instance2); | ||
| assertSame(instance1, instance2); | ||
| } | ||
|
|
||
| } |
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.
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.
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 ofJsonFactoryaround.In this case, especially since
JsonFactoryhas no dependencies and is so easy to construct, I think it is easily remedied by changing the static field declaration to:and then you can remove the
if()in the getter. Should be safer and simpler that way.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.
Good catch! I'll change it accordingly.
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.
Done in cb71c26