Package discord4j.rest.request
Class BucketGlobalRateLimiter
- java.lang.Object
-
- discord4j.rest.request.BucketGlobalRateLimiter
-
- All Implemented Interfaces:
GlobalRateLimiter
public class BucketGlobalRateLimiter extends Object implements GlobalRateLimiter
An implementation ofGlobalRateLimiterthat uses aRateLimitOperatorcoordinate requests, injecting an additional delay if a source trips the limiter early throughrateLimitFor(Duration).
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static BucketGlobalRateLimitercreate()Creates a new global rate limiter of 50 requests per second usingSchedulers.parallel()to inject delays.static BucketGlobalRateLimitercreate(int capacity, Duration refillPeriod, Scheduler delayScheduler)Creates a new global rate limiter with the given parameters.Mono<Duration>getRemaining()Returns theDurationremaining until the current global rate limit is completed.Mono<Void>rateLimitFor(Duration duration)Sets a new rate limit that will be applied to every operation performed usingGlobalRateLimiter.withLimiter(Publisher).<T> Flux<T>withLimiter(Publisher<T> stage)Provides a scope to perform reactive operations under this global rate limiter.
-
-
-
Method Detail
-
create
public static BucketGlobalRateLimiter create()
Creates a new global rate limiter of 50 requests per second usingSchedulers.parallel()to inject delays.- Returns:
- a
BucketGlobalRateLimiterwith default parameters
-
create
public static BucketGlobalRateLimiter create(int capacity, Duration refillPeriod, Scheduler delayScheduler)
Creates a new global rate limiter with the given parameters. Be aware that modifying these parameters can lead your bot hitting 429 TOO MANY REQUESTS errors.- Parameters:
capacity- the number of requests that can be performed in the givenrefillPeriodrefillPeriod- theDurationbefore refilling request permitsdelayScheduler- theSchedulerused to inject delays- Returns:
- a
BucketGlobalRateLimiterwith the given parameters.
-
rateLimitFor
public Mono<Void> rateLimitFor(Duration duration)
Description copied from interface:GlobalRateLimiterSets a new rate limit that will be applied to every operation performed usingGlobalRateLimiter.withLimiter(Publisher).- Specified by:
rateLimitForin interfaceGlobalRateLimiter- Parameters:
duration- theDurationevery new operation should wait before being used
-
getRemaining
public Mono<Duration> getRemaining()
Description copied from interface:GlobalRateLimiterReturns theDurationremaining until the current global rate limit is completed. Can be negative or zero if there is no currently active global rate limit.- Specified by:
getRemainingin interfaceGlobalRateLimiter- Returns:
- a positive
Durationindicating the remaining time a global rate limit is being applied. Zero or negative if no global rate limit is currently active.
-
withLimiter
public <T> Flux<T> withLimiter(Publisher<T> stage)
Description copied from interface:GlobalRateLimiterProvides a scope to perform reactive operations under this global rate limiter. Limiter resources are acquired on subscription and released when the given stage is cancelled, has completed or has been terminated with an error.- Specified by:
withLimiterin interfaceGlobalRateLimiter- Type Parameters:
T- the type of the stage supplier- Parameters:
stage- aPublisherthat will manage this global rate limiter resources- Returns:
- a
Fluxwhere each subscription represents acquiring a rate limiter resource
-
-