public static <T> CompletableFuture<T> failAfter(Duration duration) {
    final CompletableFuture<T> promise = new CompletableFuture<>();
    scheduler.schedule(() -> {
      final TimeoutException ex = new TimeoutException("Timeout after " + duration);
      return promise.completeExceptionally(ex);
    }, duration.toMillis(), MILLISECONDS);
    return promise;
  }
Returns a completable future which times out after a given duration.
@param duration for which a timeout future is needed
@param <T> type of the given future
@return A completable future which times out after a given duration
ambry-commons/src/main/java/com/github/ambry/commons/FutureUtils.java