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