4242 * <ul>
4343 * <li>{@link #tryAcquirePermit()}</li>
4444 * <li>{@link #tryAcquirePermits(int)}</li>
45+ * <li>{@link #reservePermit()}</li>
46+ * <li>{@link #reservePermits(int)}</li>
47+ * <li>{@link #tryReservePermit(Duration)}</li>
48+ * <li>{@link #tryReservePermits(int, Duration)}</li>
4549 * </ul>
4650 * </p>
4751 * <p>
5054 * permits cannot be acquired, and the {@code tryAcquire} methods return a boolean.
5155 * </p>
5256 * <p>
57+ * The {@code reserve} methods attempt to reserve permits and return an expected wait time before the permit can be
58+ * used. This helps integrate with scenarios where you need to wait externally
59+ * </p>
60+ * <p>
5361 * This class is threadsafe.
5462 * </p>
5563 *
@@ -132,6 +140,7 @@ static <R> RateLimiterBuilder<R> builder(RateLimiterConfig<R> config) {
132140 *
133141 * @throws InterruptedException if the current thread is interrupted while waiting to acquire a permit
134142 * @see #tryAcquirePermit()
143+ * @see #reservePermit()
135144 */
136145 default void acquirePermit () throws InterruptedException {
137146 acquirePermits (1 );
@@ -144,6 +153,7 @@ default void acquirePermit() throws InterruptedException {
144153 * @throws IllegalArgumentException if {@code permits} is < 1
145154 * @throws InterruptedException if the current thread is interrupted while waiting to acquire the {@code permits}
146155 * @see #tryAcquirePermits(int)
156+ * @see #reservePermits(int)
147157 */
148158 void acquirePermits (int permits ) throws InterruptedException ;
149159
@@ -196,10 +206,68 @@ default boolean isBursty() {
196206 return getConfig ().getPeriod () != null ;
197207 }
198208
209+ /**
210+ * Reserves a permit to perform an execution against the rate limiter, and returns the time that the caller is
211+ * expected to wait before acting on the permit. Returns {@code 0} if the permit is immediately available and no
212+ * waiting is needed.
213+ *
214+ * @see #acquirePermit()
215+ * @see #tryAcquirePermit()
216+ */
217+ default Duration reservePermit () {
218+ return reservePermits (1 );
219+ }
220+
221+ /**
222+ * Reserves the {@code permits} to perform executions against the rate limiter, and returns the time that the caller
223+ * is expected to wait before acting on the permits. Returns {@code 0} if the permits are immediately available and no
224+ * waiting is needed.
225+ *
226+ * @throws IllegalArgumentException if {@code permits} is < 1
227+ * @see #acquirePermits(int)
228+ * @see #tryAcquirePermits(int)
229+ */
230+ Duration reservePermits (int permits );
231+
232+ /**
233+ * Tries to reserve a permit to perform an execution against the rate limiter, and returns the time that the caller is
234+ * expected to wait before acting on the permit, as long as it's less than the {@code maxWaitTime}.
235+ * <ul>
236+ * <li>Returns the expected wait time for the permit if it was successfully reserved.</li>
237+ * <li>Returns {@code 0} if the permit was successfully reserved and no waiting is needed.</li>
238+ * <li>Returns {@code -1 nanoseconds} if the permit was not reserved because the wait time would be greater than the {@code maxWaitTime}.</li>
239+ * </ul>
240+ *
241+ * @throws NullPointerException if {@code maxWaitTime} is null
242+ * @see #acquirePermit(Duration)
243+ * @see #tryAcquirePermit(Duration)
244+ */
245+ default Duration tryReservePermit (Duration maxWaitTime ) {
246+ return tryReservePermits (1 , maxWaitTime );
247+ }
248+
249+ /**
250+ * Tries to reserve the {@code permits} to perform executions against the rate limiter, and returns the time that the
251+ * caller is expected to wait before acting on the permits, as long as it's less than the {@code maxWaitTime}.
252+ * <ul>
253+ * <li>Returns the expected wait time for the permits if they were successfully reserved.</li>
254+ * <li>Returns {@code 0} if the permits were successfully reserved and no waiting is needed.</li>
255+ * <li>Returns {@code -1 nanoseconds} if the permits were not reserved because the wait time would be greater than the {@code maxWaitTime}.</li>
256+ * </ul>
257+ *
258+ * @throws IllegalArgumentException if {@code permits} is < 1
259+ * @throws NullPointerException if {@code maxWaitTime} is null
260+ * @see #acquirePermit(Duration)
261+ * @see #tryAcquirePermit(Duration)
262+ */
263+ Duration tryReservePermits (int permits , Duration maxWaitTime );
264+
199265 /**
200266 * Tries to acquire a permit to perform an execution against the rate limiter, returning immediately without waiting.
201267 *
202268 * @return whether the requested {@code permits} are successfully acquired or not
269+ * @see #acquirePermit()
270+ * @see #reservePermits(int)
203271 */
204272 default boolean tryAcquirePermit () {
205273 return tryAcquirePermits (1 );
@@ -211,6 +279,7 @@ default boolean tryAcquirePermit() {
211279 *
212280 * @return whether the requested {@code permits} are successfully acquired or not
213281 * @throws IllegalArgumentException if {@code permits} is < 1
282+ * @see #acquirePermits(int)
214283 */
215284 boolean tryAcquirePermits (int permits );
216285
@@ -221,6 +290,7 @@ default boolean tryAcquirePermit() {
221290 * @return whether a permit is successfully acquired
222291 * @throws NullPointerException if {@code maxWaitTime} is null
223292 * @throws InterruptedException if the current thread is interrupted while waiting to acquire a permit
293+ * @see #acquirePermit(Duration)
224294 */
225295 default boolean tryAcquirePermit (Duration maxWaitTime ) throws InterruptedException {
226296 return tryAcquirePermits (1 , maxWaitTime );
@@ -234,6 +304,7 @@ default boolean tryAcquirePermit(Duration maxWaitTime) throws InterruptedExcepti
234304 * @throws IllegalArgumentException if {@code permits} is < 1
235305 * @throws NullPointerException if {@code maxWaitTime} is null
236306 * @throws InterruptedException if the current thread is interrupted while waiting to acquire the {@code permits}
307+ * @see #acquirePermits(int, Duration)
237308 */
238309 boolean tryAcquirePermits (int permits , Duration maxWaitTime ) throws InterruptedException ;
239310}
0 commit comments