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

Replace synchronized blocks in retrofit2 with ReentrantLock(Improve) #5911

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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)) {
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 tried to make the use of CAS instead of using Reentrant Lock.
So, when the compare failed, enqueue returns with failure callback.

httpResponse = doCall(callFactory, request);
} else {
callback.onFailure(this, new IOException("Call state is not IDLE"));
Copy link
Contributor

Choose a reason for hiding this comment

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

Since entering this block is semantically equivalent to L190

Suggested change
callback.onFailure(this, new IOException("Call state is not IDLE"));
throw new IllegalStateException("executed already");

return;
}
httpResponse.subscribe(callFactory.subscriberFactory.create(this, callback, request));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ void write(byte[] source, int offset, int byteCount) {
if (byteCount == 0) {
return;
}
/*
* Once use okhttp4, the synchronized block could be replaced by Reentrant and Condition for Virtual Thread support.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is there any plan to use okhttp4?
If that is not too far in the future, I hope to work in this PR.