core: Don't pre-compute DEADLINE_EXCEEDED message for delayed calls · grpc/grpc-java@6ff8eca
@@ -96,15 +96,13 @@ private boolean isAbeforeB(@Nullable Deadline a, @Nullable Deadline b) {
9696private ScheduledFuture<?> scheduleDeadlineIfNeeded(
9797ScheduledExecutorService scheduler, @Nullable Deadline deadline) {
9898Deadline contextDeadline = context.getDeadline();
99-if (deadline == null && contextDeadline == null) {
100-return null;
101- }
102-long remainingNanos = Long.MAX_VALUE;
103-if (deadline != null) {
99+String deadlineName;
100+long remainingNanos;
101+if (deadline != null && isAbeforeB(deadline, contextDeadline)) {
102+deadlineName = "CallOptions";
104103remainingNanos = deadline.timeRemaining(NANOSECONDS);
105- }
106-107-if (contextDeadline != null && contextDeadline.timeRemaining(NANOSECONDS) < remainingNanos) {
104+ } else if (contextDeadline != null) {
105+deadlineName = "Context";
108106remainingNanos = contextDeadline.timeRemaining(NANOSECONDS);
109107if (logger.isLoggable(Level.FINE)) {
110108StringBuilder builder =
@@ -121,29 +119,29 @@ private ScheduledFuture<?> scheduleDeadlineIfNeeded(
121119 }
122120logger.fine(builder.toString());
123121 }
124- }
125-126-long seconds = Math.abs(remainingNanos) / TimeUnit.SECONDS.toNanos(1);
127-long nanos = Math.abs(remainingNanos) % TimeUnit.SECONDS.toNanos(1);
128-final StringBuilder buf = new StringBuilder();
129-String deadlineName = isAbeforeB(contextDeadline, deadline) ? "Context" : "CallOptions";
130-if (remainingNanos < 0) {
131-buf.append("ClientCall started after ");
132-buf.append(deadlineName);
133-buf.append(" deadline was exceeded. Deadline has been exceeded for ");
134122 } else {
135-buf.append("Deadline ");
136-buf.append(deadlineName);
137-buf.append(" will be exceeded in ");
123+return null;
138124 }
139-buf.append(seconds);
140-buf.append(String.format(Locale.US, ".%09d", nanos));
141-buf.append("s. ");
142125143126/* Cancels the call if deadline exceeded prior to the real call being set. */
144127class DeadlineExceededRunnable implements Runnable {
145128@Override
146129public void run() {
130+long seconds = Math.abs(remainingNanos) / TimeUnit.SECONDS.toNanos(1);
131+long nanos = Math.abs(remainingNanos) % TimeUnit.SECONDS.toNanos(1);
132+StringBuilder buf = new StringBuilder();
133+if (remainingNanos < 0) {
134+buf.append("ClientCall started after ");
135+buf.append(deadlineName);
136+buf.append(" deadline was exceeded. Deadline has been exceeded for ");
137+ } else {
138+buf.append("Deadline ");
139+buf.append(deadlineName);
140+buf.append(" was exceeded after ");
141+ }
142+buf.append(seconds);
143+buf.append(String.format(Locale.US, ".%09d", nanos));
144+buf.append("s");
147145cancel(
148146Status.DEADLINE_EXCEEDED.withDescription(buf.toString()),
149147// We should not cancel the call if the realCall is set because there could be a