Package io.muserver
Class RateLimitBuilder
- java.lang.Object
-
- io.muserver.RateLimitBuilder
-
public class RateLimitBuilder extends java.lang.Object
A builder to createRateLimit
objects. The built limit is returned by aRateLimitSelector
. SeeMuServerBuilder.withRateLimiter(RateLimitSelector)
for more details on how to use this.
-
-
Constructor Summary
Constructors Constructor Description RateLimitBuilder()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description RateLimit
build()
Creates the rate limit from the builderstatic RateLimitBuilder
rateLimit()
Creates a new builderRateLimitBuilder
withBucket(java.lang.String name)
Sets the bucket that the limit applies to.RateLimitBuilder
withRate(long rate)
Sets the allowed rate.RateLimitBuilder
withRejectionAction(RateLimitRejectionAction action)
Specifies what to do when this limit is breached.RateLimitBuilder
withWindow(long period, java.util.concurrent.TimeUnit unit)
The period that the limit applies to.
-
-
-
Method Detail
-
withRate
public RateLimitBuilder withRate(long rate)
Sets the allowed rate. For example, this would be 10 if you allowed 10 per second.- Parameters:
rate
- The rate to use for this bucket- Returns:
- This builder
- Throws:
java.lang.IllegalArgumentException
- if the parameter is 0 or negative.
-
withWindow
public RateLimitBuilder withWindow(long period, java.util.concurrent.TimeUnit unit)
The period that the limit applies to. For example, this would be 1 second if you allowed 10 per 1 second. The default is 1 second.- Parameters:
period
- The time period of the limit.unit
- The unit of the period- Returns:
- This builder.
- Throws:
java.lang.IllegalArgumentException
- if period is less than 1, or unit is null.
-
withBucket
public RateLimitBuilder withBucket(java.lang.String name)
Sets the bucket that the limit applies to.- Parameters:
name
- The bucket, for example when limiting by IP address this would be an IP address.- Returns:
- This builder.
-
withRejectionAction
public RateLimitBuilder withRejectionAction(RateLimitRejectionAction action)
Specifies what to do when this limit is breached.- Parameters:
action
- The action to take- Returns:
- This builder
- Throws:
java.lang.IllegalArgumentException
- If the action is null
-
rateLimit
public static RateLimitBuilder rateLimit()
Creates a new builder- Returns:
- A new rate limit builder
-
build
public RateLimit build()
Creates the rate limit from the builder- Returns:
- a new Rate Limit
-
-