-
Notifications
You must be signed in to change notification settings - Fork 915
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
Replace synchronized blocks in retrofit2 with ReentrantLock(Improve) #5911
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -171,16 +171,6 @@ public Request request() { | |||||
return request; | ||||||
} | ||||||
|
||||||
private synchronized HttpResponse createRequest() { | ||||||
if (httpResponse != null) { | ||||||
throw new IllegalStateException("executed already"); | ||||||
} | ||||||
executionStateUpdater.compareAndSet(this, ExecutionState.IDLE, ExecutionState.RUNNING); | ||||||
final HttpResponse newResponse = doCall(callFactory, request); | ||||||
httpResponse = newResponse; | ||||||
return newResponse; | ||||||
} | ||||||
|
||||||
@Override | ||||||
public Response execute() throws IOException { | ||||||
final CompletableCallback completableCallback = new CompletableCallback(); | ||||||
|
@@ -196,7 +186,18 @@ public Response execute() throws IOException { | |||||
|
||||||
@Override | ||||||
public void enqueue(Callback callback) { | ||||||
createRequest().subscribe(callFactory.subscriberFactory.create(this, callback, request)); | ||||||
if (httpResponse != null) { | ||||||
throw new IllegalStateException("executed already"); | ||||||
} | ||||||
|
||||||
// Run atomically | ||||||
if (executionStateUpdater.compareAndSet(this, ExecutionState.IDLE, ExecutionState.RUNNING)) { | ||||||
httpResponse = doCall(callFactory, request); | ||||||
} else { | ||||||
callback.onFailure(this, new IOException("Call state is not IDLE")); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since entering this block is semantically equivalent to L190
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jrhee17 |
||||||
return; | ||||||
} | ||||||
httpResponse.subscribe(callFactory.subscriberFactory.create(this, callback, request)); | ||||||
} | ||||||
|
||||||
@Override | ||||||
|
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 tried to make the use of CAS instead of using Reentrant Lock.
So, when the compare failed,
enqueue
returns with failure callback.