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 finishes before the limit is reached, the return value or a wrapped exception is
082   * propagated. If, on the other hand, the time limit is reached, we attempt to abort the call to
083   * the target, and throw a {@link TimeoutException} 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   * @return the result returned by the Callable
089   * @throws TimeoutException if the time limit is reached
090   * @throws InterruptedException if the current thread was interrupted during execution
091   * @throws ExecutionException if {@code callable} throws a checked exception
092   * @throws UncheckedExecutionException if {@code callable} throws a {@code RuntimeException}
093   * @throws ExecutionError if {@code callable} throws an {@code Error}
094   * @since 22.0
095   */
096  @CanIgnoreReturnValue
097  <T> T callWithTimeout(Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit)
098      throws TimeoutException, InterruptedException, ExecutionException;
099
100  /**
101   * Invokes a specified Callable, timing out after the specified time limit. If the target method
102   * call finishes before the limit is reached, the return value or a wrapped exception is
103   * propagated. If, on the other hand, the time limit is reached, we attempt to abort the call to
104   * the target, and throw a {@link TimeoutException} to the caller.
105   *
106   * <p>The difference with {@link #callWithTimeout(Callable, long, TimeUnit)} is that this method
107   * will ignore interrupts on the current thread.
108   *
109   * @param callable the Callable to execute
110   * @param timeoutDuration with timeoutUnit, the maximum length of time to wait
111   * @param timeoutUnit with timeoutDuration, the maximum length of time to wait
112   * @return the result returned by the Callable
113   * @throws TimeoutException if the time limit is reached
114   * @throws ExecutionException if {@code callable} throws a checked exception
115   * @throws UncheckedExecutionException if {@code callable} throws a {@code RuntimeException}
116   * @throws ExecutionError if {@code callable} throws an {@code Error}
117   * @since 22.0
118   */
119  @CanIgnoreReturnValue
120  <T> T callUninterruptiblyWithTimeout(
121      Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit)
122      throws TimeoutException, ExecutionException;
123
124  /**
125   * Invokes a specified Runnable, timing out after the specified time limit. If the target method
126   * run finishes before the limit is reached, this method returns or a wrapped exception is
127   * propagated. If, on the other hand, the time limit is reached, we attempt to abort the run, and
128   * throw a {@link TimeoutException} to the caller.
129   *
130   * @param runnable the Runnable to execute
131   * @param timeoutDuration with timeoutUnit, the maximum length of time to wait
132   * @param timeoutUnit with timeoutDuration, the maximum length of time to wait
133   * @throws TimeoutException if the time limit is reached
134   * @throws InterruptedException if the current thread was interrupted during execution
135   * @throws UncheckedExecutionException if {@code runnable} throws a {@code RuntimeException}
136   * @throws ExecutionError if {@code runnable} throws an {@code Error}
137   * @since 22.0
138   */
139  void runWithTimeout(Runnable runnable, long timeoutDuration, TimeUnit timeoutUnit)
140      throws TimeoutException, InterruptedException;
141
142  /**
143   * Invokes a specified Runnable, timing out after the specified time limit. If the target method
144   * run finishes before the limit is reached, this method returns or a wrapped exception is
145   * propagated. If, on the other hand, the time limit is reached, we attempt to abort the run, and
146   * throw a {@link TimeoutException} to the caller.
147   *
148   * <p>The difference with {@link #runWithTimeout(Runnable, long, TimeUnit)} is that this method
149   * will ignore interrupts on the current thread.
150   *
151   * @param runnable the Runnable to execute
152   * @param timeoutDuration with timeoutUnit, the maximum length of time to wait
153   * @param timeoutUnit with timeoutDuration, the maximum length of time to wait
154   * @throws TimeoutException if the time limit is reached
155   * @throws UncheckedExecutionException if {@code runnable} throws a {@code RuntimeException}
156   * @throws ExecutionError if {@code runnable} throws an {@code Error}
157   * @since 22.0
158   */
159  void runUninterruptiblyWithTimeout(Runnable runnable, long timeoutDuration, TimeUnit timeoutUnit)
160      throws TimeoutException;
161}