|
| 1 | +/* |
| 2 | + * Copyright 2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License |
| 15 | + */ |
| 16 | +package dev.failsafe; |
| 17 | + |
| 18 | +import dev.failsafe.internal.BulkheadImpl; |
| 19 | + |
| 20 | +import java.time.Duration; |
| 21 | + |
| 22 | +/** |
| 23 | + * A bulkhead allows you to restrict concurrent executions as a way of preventing system overload. |
| 24 | + * <p> |
| 25 | + * This class is threadsafe. |
| 26 | + * </p> |
| 27 | + * |
| 28 | + * @param <R> result type |
| 29 | + * @author Jonathan Halterman |
| 30 | + * @see BulkheadConfig |
| 31 | + * @see BulkheadBuilder |
| 32 | + * @see BulkheadFullException |
| 33 | + */ |
| 34 | +public interface Bulkhead<R> extends Policy<R> { |
| 35 | + /** |
| 36 | + * Returns a Bulkhead for the {@code maxConcurrency} that has {@link BulkheadBuilder#withMaxWaitTime(Duration) zero |
| 37 | + * wait} and is {@link BulkheadBuilder#withFairness() not fair} by default. |
| 38 | + * |
| 39 | + * @param maxConcurrency controls the max concurrent executions that are permitted within the bulkhead |
| 40 | + */ |
| 41 | + static <R> BulkheadBuilder<R> builder(int maxConcurrency) { |
| 42 | + return new BulkheadBuilder<>(maxConcurrency); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Creates a new BulkheadBuilder that will be based on the {@code config}. The built bulkhead is {@link |
| 47 | + * BulkheadBuilder#withFairness() not fair} by default. |
| 48 | + */ |
| 49 | + static <R> BulkheadBuilder<R> builder(BulkheadConfig<R> config) { |
| 50 | + return new BulkheadBuilder<>(config); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Returns a Bulkhead for the {@code maxConcurrency} that has {@link BulkheadBuilder#withMaxWaitTime(Duration) zero |
| 55 | + * wait} and is {@link BulkheadBuilder#withFairness() not fair} by default. Alias for {@code |
| 56 | + * Bulkhead.builder(maxConcurrency).build()}. To configure additional options on a Bulkhead, use {@link #builder(int)} |
| 57 | + * instead. |
| 58 | + * |
| 59 | + * @param maxConcurrency controls the max concurrent executions that are permitted within the bulkhead |
| 60 | + * @see #builder(int) |
| 61 | + */ |
| 62 | + static <R> Bulkhead<R> of(int maxConcurrency) { |
| 63 | + return new BulkheadImpl<>(new BulkheadConfig<>(maxConcurrency)); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Returns the {@link BulkheadConfig} that the Bulkhead was built with. |
| 68 | + */ |
| 69 | + @Override |
| 70 | + BulkheadConfig<R> getConfig(); |
| 71 | + |
| 72 | + /** |
| 73 | + * Attempts to acquire a permit to perform an execution against within the bulkhead, waiting until one is available or |
| 74 | + * the thread is interrupted. After execution is complete, the permit should be {@link #releasePermit() released} back |
| 75 | + * to the bulkhead. |
| 76 | + * |
| 77 | + * @throws InterruptedException if the current thread is interrupted while waiting to acquire a permit |
| 78 | + * @see #tryAcquirePermit() |
| 79 | + */ |
| 80 | + void acquirePermit() throws InterruptedException; |
| 81 | + |
| 82 | + /** |
| 83 | + * Attempts to acquire a permit to perform an execution within the bulkhead, waiting up to the {@code maxWaitTime} |
| 84 | + * until one is available, else throwing {@link BulkheadFullException} if a permit will not be available in time. |
| 85 | + * After execution is complete, the permit should be {@link #releasePermit() released} back to the bulkhead. |
| 86 | + * |
| 87 | + * @throws NullPointerException if {@code maxWaitTime} is null |
| 88 | + * @throws BulkheadFullException if the bulkhead cannot acquire a permit within the {@code maxWaitTime} |
| 89 | + * @throws InterruptedException if the current thread is interrupted while waiting to acquire a permit |
| 90 | + * @see #tryAcquirePermit(Duration) |
| 91 | + */ |
| 92 | + default void acquirePermit(Duration maxWaitTime) throws InterruptedException { |
| 93 | + if (!tryAcquirePermit(maxWaitTime)) |
| 94 | + throw new BulkheadFullException(this); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Tries to acquire a permit to perform an execution within the bulkhead, returning immediately without waiting. After |
| 99 | + * execution is complete, the permit should be {@link #releasePermit() released} back to the bulkhead. |
| 100 | + * |
| 101 | + * @return whether the requested {@code permits} are successfully acquired or not |
| 102 | + */ |
| 103 | + boolean tryAcquirePermit(); |
| 104 | + |
| 105 | + /** |
| 106 | + * Tries to acquire a permit to perform an execution within the bulkhead, waiting up to the {@code maxWaitTime} until |
| 107 | + * they are available. After execution is complete, the permit should be {@link #releasePermit() released} back to the |
| 108 | + * bulkhead. |
| 109 | + * |
| 110 | + * @return whether a permit is successfully acquired |
| 111 | + * @throws NullPointerException if {@code maxWaitTime} is null |
| 112 | + * @throws InterruptedException if the current thread is interrupted while waiting to acquire a permit |
| 113 | + */ |
| 114 | + boolean tryAcquirePermit(Duration maxWaitTime) throws InterruptedException; |
| 115 | + |
| 116 | + /** |
| 117 | + * Releases a permit to execute. |
| 118 | + */ |
| 119 | + void releasePermit(); |
| 120 | +} |
0 commit comments