Class CallTimeout

java.lang.Object
me.ahoo.cosid.util.CallTimeout

public final class CallTimeout extends Object
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 Details

    • NONE

      public static final CallTimeout NONE
      No timeout: calls run on the caller thread.
    • MAX_CONCURRENT_CALLS

      public static final int MAX_CONCURRENT_CALLS
      See Also:
  • Method Details

    • of

      public static CallTimeout of(Duration timeout)
      Create a call timeout.
      Parameters:
      timeout - timeout of each call, null or zero means no timeout
      Returns:
      call timeout
    • getTimeout

      public Duration getTimeout()
    • isEnabled

      public boolean isEnabled()
    • call

      public <T> T call(String operation, Callable<T> call)
      Run a call within the timeout. Runtime exceptions and errors of the call are rethrown as they are, checked exceptions are wrapped in CosIdException.
      Type Parameters:
      T - result type
      Parameters:
      operation - name of the operation, used in error messages
      call - 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 by timeoutExceptionFactory on timeout.
      Type Parameters:
      T - result type
      Parameters:
      operation - name of the operation, used in error messages
      call - the call
      timeoutExceptionFactory - creates the exception thrown on timeout from the operation and the cause
      Returns:
      the call result
    • toString

      public String toString()
      Overrides:
      toString in class Object