Skip to content

Commit

Permalink
chore: Use HttpJsonClientCallImpl in main branch
Browse files Browse the repository at this point in the history
  • Loading branch information
lqiu96 committed Jan 17, 2024
1 parent 7f04a7f commit e6f7219
Showing 1 changed file with 9 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import java.util.concurrent.CancellationException;
import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;

Expand Down Expand Up @@ -122,11 +121,6 @@ final class HttpJsonClientCallImpl<RequestT, ResponseT>
@GuardedBy("lock")
private volatile boolean closed;

// Store the timeout future created by the deadline schedule executor. The future
// can be cancelled if a response has been received before the timeout.
@GuardedBy("lock")
private ScheduledFuture<?> timeoutFuture;

HttpJsonClientCallImpl(
ApiMethodDescriptor<RequestT, ResponseT> methodDescriptor,
String endpoint,
Expand Down Expand Up @@ -173,20 +167,16 @@ public void start(Listener<ResponseT> responseListener, HttpJsonMetadata request
Preconditions.checkState(this.listener == null, "The call is already started");
this.listener = responseListener;
this.requestHeaders = requestHeaders;
}

// Use the timeout duration value instead of calculating the future Instant
// Only schedule the deadline if the RPC timeout has been set in the RetrySettings
Duration timeout = callOptions.getTimeout();
if (timeout != null) {
// The future timeout value is guaranteed to not be a negative value as the
// RetryAlgorithm will not retry
long timeoutMs = timeout.toMillis();
// Assign the scheduled future so that it can be cancelled if the timeout task
// is not needed (response received prior to timeout)
timeoutFuture =
this.deadlineCancellationExecutor.schedule(
this::timeout, timeoutMs, TimeUnit.MILLISECONDS);
}
// Use the timeout duration value instead of calculating the future Instant
// Only schedule the deadline if the RPC timeout has been set in the RetrySettings
Duration timeout = callOptions.getTimeout();
if (timeout != null) {
// The future timeout value is guaranteed to not be a negative value as the
// RetryAlgorithm will not retry
long timeoutMs = timeout.toMillis();
this.deadlineCancellationExecutor.schedule(this::timeout, timeoutMs, TimeUnit.MILLISECONDS);
}
}

Expand Down Expand Up @@ -440,14 +430,6 @@ private void close(
return;
}
closed = true;

// Cancel the timeout future if there is a timeout task created
if (timeoutFuture != null) {
// timeout() invokes close(), but cancelling a completed task should no-op
timeoutFuture.cancel(true);
timeoutFuture = null;
}

// Best effort task cancellation (to not be confused with task's thread interruption).
// If the task is in blocking I/O waiting for the server response, it will keep waiting for
// the response from the server, but once response is received the task will exit silently.
Expand Down

0 comments on commit e6f7219

Please sign in to comment.