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 com.google.errorprone.annotations.DoNotMock;
021import java.util.concurrent.Callable;
022import java.util.concurrent.ExecutionException;
023import java.util.concurrent.TimeUnit;
024import java.util.concurrent.TimeoutException;
025
026/**
027 * Imposes a time limit on method calls.
028 *
029 * @author Kevin Bourrillion
030 * @author Jens Nyman
031 * @since 1.0
032 */
033@Beta
034@DoNotMock("Use FakeTimeLimiter")
035@GwtIncompatible
036@SuppressWarnings("GoodTime") // should have java.time.Duration overloads
037public interface TimeLimiter {
038
039  /**
040   * Returns an instance of {@code interfaceType} that delegates all method calls to the {@code
041   * target} object, enforcing the specified time limit on each call. This time-limited delegation
042   * is also performed for calls to {@link Object#equals}, {@link Object#hashCode}, and {@link
043   * Object#toString}.
044   *
045   * <p>If the target method call finishes before the limit is reached, the return value or
046   * exception is propagated to the caller exactly as-is. If, on the other hand, the time limit is
047   * reached, the proxy will attempt to abort the call to the target, and will throw an {@link
048   * UncheckedTimeoutException} to the caller.
049   *
050   * <p>It is important to note that the primary purpose of the proxy object is to return control to
051   * the caller when the timeout elapses; aborting the target method call is of secondary concern.
052   * The particular nature and strength of the guarantees made by the proxy is
053   * implementation-dependent. However, it is important that each of the methods on the target
054   * object behaves appropriately when its thread is interrupted.
055   *
056   * <p>For example, to return the value of {@code target.someMethod()}, but substitute {@code
057   * DEFAULT_VALUE} if this method call takes over 50 ms, you can use this code:
058   *
059   * <pre>
060   *   TimeLimiter limiter = . . .;
061   *   TargetType proxy = limiter.newProxy(
062   *       target, TargetType.class, 50, TimeUnit.MILLISECONDS);
063   *   try {
064   *     return proxy.someMethod();
065   *   } catch (UncheckedTimeoutException e) {
066   *     return DEFAULT_VALUE;
067   *   }
068   * </pre>
069   *
070   * @param target the object to proxy
071   * @param interfaceType the interface you wish the returned proxy to implement
072   * @param timeoutDuration with timeoutUnit, the maximum length of time that callers are willing to
073   *     wait on each method call to the proxy
074   * @param timeoutUnit with timeoutDuration, the maximum length of time that callers are willing to
075   *     wait on each method call to the proxy
076   * @return a time-limiting proxy
077   * @throws IllegalArgumentException if {@code interfaceType} is a regular class, enum, or
078   *     annotation type, rather than an interface
079   */
080  <T> T newProxy(T target, Class<T> interfaceType, long timeoutDuration, TimeUnit timeoutUnit);
081
082  /**
083   * Invokes a specified Callable, timing out after the specified time limit. If the target method
084   * call finishes before the limit is reached, the return value or a wrapped exception is
085   * propagated. If, on the other hand, the time limit is reached, we attempt to abort the call to
086   * the target, and throw a {@link TimeoutException} to the caller.
087   *
088   * @param callable the Callable to execute
089   * @param timeoutDuration with timeoutUnit, the maximum length of time to wait
090   * @param timeoutUnit with timeoutDuration, the maximum length of time to wait
091   * @return the result returned by the Callable
092   * @throws TimeoutException if the time limit is reached
093   * @throws InterruptedException if the current thread was interrupted during execution
094   * @throws ExecutionException if {@code callable} throws a checked exception
095   * @throws UncheckedExecutionException if {@code callable} throws a {@code RuntimeException}
096   * @throws ExecutionError if {@code callable} throws an {@code Error}
097   * @since 22.0
098   */
099  @CanIgnoreReturnValue
100  <T> T callWithTimeout(Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit)
101      throws TimeoutException, InterruptedException, ExecutionException;
102
103  /**
104   * Invokes a specified Callable, timing out after the specified time limit. If the target method
105   * call finishes before the limit is reached, the return value or a wrapped exception is
106   * propagated. If, on the other hand, the time limit is reached, we attempt to abort the call to
107   * the target, and throw a {@link TimeoutException} to the caller.
108   *
109   * <p>The difference with {@link #callWithTimeout(Callable, long, TimeUnit)} is that this method
110   * will ignore interrupts on the current thread.
111   *
112   * @param callable the Callable to execute
113   * @param timeoutDuration with timeoutUnit, the maximum length of time to wait
114   * @param timeoutUnit with timeoutDuration, the maximum length of time to wait
115   * @return the result returned by the Callable
116   * @throws TimeoutException if the time limit is reached
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  @CanIgnoreReturnValue
123  <T> T callUninterruptiblyWithTimeout(
124      Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit)
125      throws TimeoutException, ExecutionException;
126
127  /**
128   * Invokes a specified Runnable, timing out after the specified time limit. If the target method
129   * run finishes before the limit is reached, this method returns or a wrapped exception is
130   * propagated. If, on the other hand, the time limit is reached, we attempt to abort the run, and
131   * throw a {@link TimeoutException} to the caller.
132   *
133   * @param runnable the Runnable to execute
134   * @param timeoutDuration with timeoutUnit, the maximum length of time to wait
135   * @param timeoutUnit with timeoutDuration, the maximum length of time to wait
136   * @throws TimeoutException if the time limit is reached
137   * @throws InterruptedException if the current thread was interrupted during execution
138   * @throws UncheckedExecutionException if {@code runnable} throws a {@code RuntimeException}
139   * @throws ExecutionError if {@code runnable} throws an {@code Error}
140   * @since 22.0
141   */
142  void runWithTimeout(Runnable runnable, long timeoutDuration, TimeUnit timeoutUnit)
143      throws TimeoutException, InterruptedException;
144
145  /**
146   * Invokes a specified Runnable, timing out after the specified time limit. If the target method
147   * run finishes before the limit is reached, this method returns or a wrapped exception is
148   * propagated. If, on the other hand, the time limit is reached, we attempt to abort the run, and
149   * throw a {@link TimeoutException} to the caller.
150   *
151   * <p>The difference with {@link #runWithTimeout(Runnable, long, TimeUnit)} is that this method
152   * will ignore interrupts on the current thread.
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 UncheckedExecutionException if {@code runnable} throws a {@code RuntimeException}
159   * @throws ExecutionError if {@code runnable} throws an {@code Error}
160   * @since 22.0
161   */
162  void runUninterruptiblyWithTimeout(Runnable runnable, long timeoutDuration, TimeUnit timeoutUnit)
163      throws TimeoutException;
164}