001/*
002 * Copyright (C) 2006 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.util.concurrent;
016
017import com.google.common.annotations.Beta;
018import com.google.common.annotations.GwtIncompatible;
019import com.google.errorprone.annotations.CanIgnoreReturnValue;
020import java.util.concurrent.Callable;
021import java.util.concurrent.ExecutionException;
022import java.util.concurrent.TimeUnit;
023import java.util.concurrent.TimeoutException;
024
025/**
026 * Imposes a time limit on method calls.
027 *
028 * @author Kevin Bourrillion
029 * @author Jens Nyman
030 * @since 1.0
031 */
032@Beta
033@GwtIncompatible
034public interface TimeLimiter {
035
036  /**
037   * Returns an instance of {@code interfaceType} that delegates all method calls to the
038   * {@code target} object, enforcing the specified time limit on each call. This time-limited
039   * delegation is also performed for calls to {@link Object#equals}, {@link Object#hashCode}, and
040   * {@link Object#toString}.
041   *
042   * <p>If the target method call finishes before the limit is reached, the return value or
043   * exception is propagated to the caller exactly as-is. If, on the other hand, the time limit is
044   * reached, the proxy will attempt to abort the call to the target, and will throw an
045   * {@link UncheckedTimeoutException} to the caller.
046   *
047   * <p>It is important to note that the primary purpose of the proxy object is to return control to
048   * the caller when the timeout elapses; aborting the target method call is of secondary concern.
049   * The particular nature and strength of the guarantees made by the proxy is
050   * implementation-dependent. However, it is important that each of the methods on the target
051   * object behaves appropriately when its thread is interrupted.
052   *
053   * <p>For example, to return the value of {@code target.someMethod()}, but substitute {@code
054   * DEFAULT_VALUE} if this method call takes over 50 ms, you can use this code:
055   *
056   * <pre>
057   *   TimeLimiter limiter = . . .;
058   *   TargetType proxy = limiter.newProxy(
059   *       target, TargetType.class, 50, TimeUnit.MILLISECONDS);
060   *   try {
061   *     return proxy.someMethod();
062   *   } catch (UncheckedTimeoutException e) {
063   *     return DEFAULT_VALUE;
064   *   }
065   * </pre>
066   *
067   * @param target the object to proxy
068   * @param interfaceType the interface you wish the returned proxy to implement
069   * @param timeoutDuration with timeoutUnit, the maximum length of time that callers are willing to
070   *     wait on each method call to the proxy
071   * @param timeoutUnit with timeoutDuration, the maximum length of time that callers are willing to
072   *     wait on each method call to the proxy
073   * @return a time-limiting proxy
074   * @throws IllegalArgumentException if {@code interfaceType} is a regular class, enum, or
075   *     annotation type, rather than an interface
076   */
077  <T> T newProxy(T target, Class<T> interfaceType, long timeoutDuration, TimeUnit timeoutUnit);
078
079  /**
080   * Invokes a specified Callable, timing out after the specified time limit. If the target method
081   * call finished before the limit is reached, the return value or exception is propagated to the
082   * caller exactly as-is. If, on the other hand, the time limit is reached, we attempt to abort the
083   * call to the target, and throw an {@link UncheckedTimeoutException} to the caller.
084   *
085   * @param callable the Callable to execute
086   * @param timeoutDuration with timeoutUnit, the maximum length of time to wait
087   * @param timeoutUnit with timeoutDuration, the maximum length of time to wait
088   * @param interruptible whether to respond to thread interruption by aborting the operation and
089   *     throwing InterruptedException; if false, the operation is allowed to complete or time out,
090   *     and the current thread's interrupt status is re-asserted.
091   * @return the result returned by the Callable
092   * @throws InterruptedException if {@code interruptible} is true and our thread is interrupted
093   *     during execution
094   * @throws UncheckedTimeoutException if the time limit is reached
095   * @deprecated Use one of the other {@code call[Uninterruptibly]WithTimeout()} or {@code
096   *     run[Uninterruptibly]WithTimeout()} methods. This method is scheduled to be removed in Guava
097   *     23.0.
098   */
099  @Deprecated
100  @CanIgnoreReturnValue
101  <T> T callWithTimeout(
102      Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit, boolean interruptible)
103      throws Exception;
104
105  /**
106   * Invokes a specified Callable, timing out after the specified time limit. If the target method
107   * call finishes before the limit is reached, the return value or a wrapped exception is
108   * propagated. If, on the other hand, the time limit is reached, we attempt to abort the call to
109   * the target, and throw a {@link TimeoutException} to the caller.
110   *
111   * @param callable the Callable to execute
112   * @param timeoutDuration with timeoutUnit, the maximum length of time to wait
113   * @param timeoutUnit with timeoutDuration, the maximum length of time to wait
114   * @return the result returned by the Callable
115   * @throws TimeoutException if the time limit is reached
116   * @throws InterruptedException if the current thread was interrupted during execution
117   * @throws ExecutionException if {@code callable} throws a checked exception
118   * @throws UncheckedExecutionException if {@code callable} throws a {@code RuntimeException}
119   * @throws ExecutionError if {@code callable} throws an {@code Error}
120   * @since 22.0
121   */
122  <T> T callWithTimeout(Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit)
123      throws TimeoutException, InterruptedException, ExecutionException;
124
125  /**
126   * Invokes a specified Callable, timing out after the specified time limit. If the target method
127   * call finishes before the limit is reached, the return value or a wrapped exception is
128   * propagated. If, on the other hand, the time limit is reached, we attempt to abort the call to
129   * the target, and throw a {@link TimeoutException} to the caller.
130   *
131   * <p>The difference with {@link #callWithTimeout(Callable, long, TimeUnit)} is that this method
132   * will ignore interrupts on the current thread.
133   *
134   * @param callable the Callable to execute
135   * @param timeoutDuration with timeoutUnit, the maximum length of time to wait
136   * @param timeoutUnit with timeoutDuration, the maximum length of time to wait
137   * @return the result returned by the Callable
138   * @throws TimeoutException if the time limit is reached
139   * @throws ExecutionException if {@code callable} throws a checked exception
140   * @throws UncheckedExecutionException if {@code callable} throws a {@code RuntimeException}
141   * @throws ExecutionError if {@code callable} throws an {@code Error}
142   * @since 22.0
143   */
144  <T> T callUninterruptiblyWithTimeout(
145      Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit)
146      throws TimeoutException, ExecutionException;
147
148  /**
149   * Invokes a specified Runnable, timing out after the specified time limit. If the target method
150   * run finishes before the limit is reached, this method returns or a wrapped exception is
151   * propagated. If, on the other hand, the time limit is reached, we attempt to abort the run, and
152   * throw a {@link TimeoutException} to the caller.
153   *
154   * @param runnable the Runnable to execute
155   * @param timeoutDuration with timeoutUnit, the maximum length of time to wait
156   * @param timeoutUnit with timeoutDuration, the maximum length of time to wait
157   * @throws TimeoutException if the time limit is reached
158   * @throws InterruptedException if the current thread was interrupted during execution
159   * @throws UncheckedExecutionException if {@code runnable} throws a {@code RuntimeException}
160   * @throws ExecutionError if {@code runnable} throws an {@code Error}
161   * @since 22.0
162   */
163  void runWithTimeout(Runnable runnable, long timeoutDuration, TimeUnit timeoutUnit)
164      throws TimeoutException, InterruptedException;
165
166  /**
167   * Invokes a specified Runnable, timing out after the specified time limit. If the target method
168   * run finishes before the limit is reached, this method returns or a wrapped exception is
169   * propagated. If, on the other hand, the time limit is reached, we attempt to abort the run, and
170   * throw a {@link TimeoutException} to the caller.
171   *
172   * <p>The difference with {@link #runWithTimeout(Runnable, long, TimeUnit)} is that this method
173   * will ignore interrupts on the current thread.
174   *
175   * @param runnable the Runnable to execute
176   * @param timeoutDuration with timeoutUnit, the maximum length of time to wait
177   * @param timeoutUnit with timeoutDuration, the maximum length of time to wait
178   * @throws TimeoutException if the time limit is reached
179   * @throws UncheckedExecutionException if {@code runnable} throws a {@code RuntimeException}
180   * @throws ExecutionError if {@code runnable} throws an {@code Error}
181   * @since 22.0
182   */
183  void runUninterruptiblyWithTimeout(Runnable runnable, long timeoutDuration, TimeUnit timeoutUnit)
184      throws TimeoutException;
185}