/** * Create a deadline that will expire at the specified offset from the current system clock. * @param duration A non-negative duration. * @param units The time unit for the duration. * @return A new deadline. */ public static Deadline after(long duration, TimeUnit units) { return after(duration, units, SYSTEM_TICKER); }
@Override public String toString() { return timeRemaining(TimeUnit.NANOSECONDS) + " ns from now"; }
/** * Return the minimum deadline of {@code this} or an other deadline. * @param other deadline to compare with {@code this}. */ public Deadline minimum(Deadline other) { assert this.ticker == other.ticker : "Tickers don't match"; return isBefore(other) ? this : other; }
/** * Create a new deadline that is offset from {@code this}. */ // TODO(ejona): This method can cause deadlines to grow too far apart. For example: // Deadline.after(100 * 365, DAYS).offset(100 * 365, DAYS) would be less than // Deadline.after(-100 * 365, DAYS) public Deadline offset(long offset, TimeUnit units) { // May already be expired if (offset == 0) { return this; } return new Deadline(ticker, deadlineNanos, units.toNanos(offset), isExpired()); }
static Deadline after(long duration, TimeUnit units, Ticker ticker) { checkNotNull(units, "units"); return new Deadline(ticker, units.toNanos(duration), true); }
protected <ReqT,R> ListenableFuture<R> fuCall(MethodDescriptor<ReqT,R> method, ReqT request, CallOptions callOptions, long timeoutMs) { if(timeoutMs <= 0L) timeoutMs = defaultTimeoutMs; if(timeoutMs > 0L) { Deadline deadline = callOptions.getDeadline(); Deadline timeoutDeadline = Deadline.after(timeoutMs, MILLISECONDS); if(deadline == null || timeoutDeadline.isBefore(deadline)) { callOptions = callOptions.withDeadline(timeoutDeadline); } else if(deadline.isExpired()) { return Futures.immediateFailedFuture( Status.DEADLINE_EXCEEDED.asRuntimeException()); } } final CallOptions callOpts = callOptions; return sendViaEventLoop && !isEventThread.satisfied() ? Futures.submitAsync(() -> fuCall(method, request, callOpts), ses) : fuCall(method, request, callOpts); }
boolean reauth; if((!backoff && attempt > 0) || (deadline != null && deadline.isExpired()) || (!(reauth = reauthIfRequired(t, baseCallOpts)) && !retry.retry(t, request))) return Futures.immediateFailedFuture(t); if(deadline != null && deadline.timeRemaining(MILLISECONDS) < delayMs) { return Futures.immediateFailedFuture(t);
super(parent, parent.keyValueEntries); Deadline parentDeadline = parent.getDeadline(); if (parentDeadline != null && parentDeadline.compareTo(deadline) <= 0) { if (!deadline.isExpired()) { pendingDeadline = deadline.runOnExpiration(new Runnable() { @Override public void run() {
boolean deadlineExceeded = effectiveDeadline != null && effectiveDeadline.isExpired(); if (!deadlineExceeded) { logIfContextNarrowedTimeout(
/** * Schedule a task to be run when the deadline expires. * @param task to run on expiration * @param scheduler used to execute the task * @return {@link ScheduledFuture} which can be used to cancel execution of the task */ public ScheduledFuture<?> runOnExpiration(Runnable task, ScheduledExecutorService scheduler) { checkNotNull(task, "task"); checkNotNull(scheduler, "scheduler"); return scheduler.schedule(task, deadlineNanos - ticker.read(), TimeUnit.NANOSECONDS); }
super(parent, parent.keyValueEntries); Deadline parentDeadline = parent.getDeadline(); if (parentDeadline != null && parentDeadline.compareTo(deadline) <= 0) { if (!deadline.isExpired()) { pendingDeadline = deadline.runOnExpiration(new Runnable() { @Override public void run() {
/** * Create a new deadline that is offset from {@code this}. */ // TODO(ejona): This method can cause deadlines to grow too far apart. For example: // Deadline.after(100 * 365, DAYS).offset(100 * 365, DAYS) would be less than // Deadline.after(-100 * 365, DAYS) public Deadline offset(long offset, TimeUnit units) { // May already be expired if (offset == 0) { return this; } return new Deadline(ticker, deadlineNanos, units.toNanos(offset), isExpired()); }
static Deadline after(long duration, TimeUnit units, Ticker ticker) { checkNotNull(units, "units"); return new Deadline(ticker, units.toNanos(duration), true); }
boolean deadlineExceeded = effectiveDeadline != null && effectiveDeadline.isExpired(); if (!deadlineExceeded) { updateTimeoutHeaders(effectiveDeadline, callOptions.getDeadline(),
/** * Schedule a task to be run when the deadline expires. * @param task to run on expiration * @param scheduler used to execute the task * @return {@link ScheduledFuture} which can be used to cancel execution of the task */ public ScheduledFuture<?> runOnExpiration(Runnable task, ScheduledExecutorService scheduler) { checkNotNull(task, "task"); checkNotNull(scheduler, "scheduler"); return scheduler.schedule(task, deadlineNanos - ticker.read(), TimeUnit.NANOSECONDS); }
private static void logIfContextNarrowedTimeout( Deadline effectiveDeadline, @Nullable Deadline outerCallDeadline, @Nullable Deadline callDeadline) { if (!log.isLoggable(Level.FINE) || effectiveDeadline == null || outerCallDeadline != effectiveDeadline) { return; } long effectiveTimeout = max(0, effectiveDeadline.timeRemaining(TimeUnit.NANOSECONDS)); StringBuilder builder = new StringBuilder(String.format( "Call timeout set to '%d' ns, due to context deadline.", effectiveTimeout)); if (callDeadline == null) { builder.append(" Explicit call timeout was not set."); } else { long callTimeout = callDeadline.timeRemaining(TimeUnit.NANOSECONDS); builder.append(String.format(" Explicit call timeout was '%d' ns.", callTimeout)); } log.fine(builder.toString()); }
/** * Create a deadline that will expire at the specified offset from the current system clock. * @param duration A non-negative duration. * @param units The time unit for the duration. * @return A new deadline. */ public static Deadline after(long duration, TimeUnit units) { return after(duration, units, SYSTEM_TICKER); }