Package me.ahoo.cosid.util
Class CallTimeout
java.lang.Object
me.ahoo.cosid.util.CallTimeout
Bounds how long a caller waits for a blocking backend call that has no timeout of its own.
When enabled, the call runs on a daemon worker and the caller gives up after getTimeout(), interrupting the
worker and throwing CosIdException. A call that timed out may still have been applied by the backend.
At most MAX_CONCURRENT_CALLS calls run at once; beyond that, callers fail fast instead of piling up threads
behind a hung backend.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intstatic final CallTimeoutNo timeout: calls run on the caller thread. -
Method Summary
Modifier and TypeMethodDescription<T> TRun a call within the timeout.<T> Tcall(String operation, Callable<T> call, BiFunction<String, TimeoutException, ? extends RuntimeException> timeoutExceptionFactory) Run a call within the timeout, throwing the exception created bytimeoutExceptionFactoryon timeout.booleanstatic CallTimeoutCreate a call timeout.toString()
-
Field Details
-
NONE
No timeout: calls run on the caller thread. -
MAX_CONCURRENT_CALLS
public static final int MAX_CONCURRENT_CALLS- See Also:
-
-
Method Details
-
of
Create a call timeout.- Parameters:
timeout- timeout of each call,nullor zero means no timeout- Returns:
- call timeout
-
getTimeout
-
isEnabled
public boolean isEnabled() -
call
Run a call within the timeout. Runtime exceptions and errors of the call are rethrown as they are, checked exceptions are wrapped inCosIdException.- Type Parameters:
T- result type- Parameters:
operation- name of the operation, used in error messagescall- the call- Returns:
- the call result
-
call
public <T> T call(String operation, Callable<T> call, BiFunction<String, TimeoutException, ? extends RuntimeException> timeoutExceptionFactory) Run a call within the timeout, throwing the exception created bytimeoutExceptionFactoryon timeout.- Type Parameters:
T- result type- Parameters:
operation- name of the operation, used in error messagescall- the calltimeoutExceptionFactory- creates the exception thrown on timeout from the operation and the cause- Returns:
- the call result
-
toString
-