/** * @param maxConcurrency maximum number of futures in progress, * @param maxQueueSize maximum number of jobs in queue. This is a soft bound and may be * temporarily exceeded if add() is called concurrently. * @return a new concurrency limiter */ public static <T> ConcurrencyReducer<T> create(int maxConcurrency, int maxQueueSize) { return new ConcurrencyReducer<>(maxConcurrency, maxQueueSize); }
private void invoke(final CompletableFuture<T> response, final Callable<? extends CompletionStage<T>> callable) { final CompletionStage<T> future; try { future = callable.call(); if (future == null) { limit.release(); response.completeExceptionally(new NullPointerException()); return; } } catch (Throwable e) { limit.release(); response.completeExceptionally(e); return; } future.whenComplete( (result, t) -> { if (result != null) { limit.release(); response.complete(result); pump(); } else { limit.release(); response.completeExceptionally(t); pump(); } }); }
private void invoke(final CompletableFuture<T> response, final Callable<? extends CompletionStage<T>> callable) { final CompletionStage<T> future; try { future = callable.call(); if (future == null) { limit.release(); response.completeExceptionally(new NullPointerException()); return; } } catch (Throwable e) { limit.release(); response.completeExceptionally(e); return; } future.whenComplete( (result, t) -> { if (result != null) { limit.release(); response.complete(result); pump(); } else { limit.release(); response.completeExceptionally(t); pump(); } }); }
/** * the callable function will run as soon as the currently active set of futures is less than the * maxConcurrency limit. * * @param callable - a function that creates a future. * @return a proxy future that completes with the future created by the input function. This * future will be immediately failed with {@link CapacityReachedException} if the soft queue * size limit is exceeded. */ public CompletableFuture<T> add(final Callable<? extends CompletionStage<T>> callable) { requireNonNull(callable); final CompletableFuture<T> response = new CompletableFuture<>(); final Job<T> job = new Job<>(callable, response); if (!queue.offer(job)) { final String message = "Queue size has reached capacity: " + maxQueueSize; return CompletableFutures.exceptionallyCompletedFuture(new CapacityReachedException(message)); } pump(); return response; }
/** * @param maxConcurrency maximum number of futures in progress, * @param maxQueueSize maximum number of jobs in queue. This is a soft bound and may be * temporarily exceeded if add() is called concurrently. * @return a new concurrency limiter */ public static <T> ConcurrencyReducer<T> create(int maxConcurrency, int maxQueueSize) { return new ConcurrencyReducer<>(maxConcurrency, maxQueueSize); }
/** * the callable function will run as soon as the currently active set of futures is less than the * maxConcurrency limit. * * @param callable - a function that creates a future. * @return a proxy future that completes with the future created by the input function. This * future will be immediately failed with {@link CapacityReachedException} if the soft queue * size limit is exceeded. */ public CompletableFuture<T> add(final Callable<? extends CompletionStage<T>> callable) { requireNonNull(callable); final CompletableFuture<T> response = new CompletableFuture<>(); final Job<T> job = new Job<>(callable, response); if (!queue.offer(job)) { final String message = "Queue size has reached capacity: " + maxQueueSize; return CompletableFutures.exceptionallyCompletedFuture(new CapacityReachedException(message)); } pump(); return response; }