public class DefaultRateLimiter extends Object implements RateLimiter
Modifier and Type | Field and Description |
---|---|
protected static RateLimitRequestToken |
BYPASS_TOKEN |
protected static int |
DEFAULT_REQUEST_LIMIT_GLOBAL |
protected static int |
DEFAULT_REQUEST_LIMIT_PER_USER |
protected static int |
DEFAULT_REQUEST_LIMIT_UI |
protected static int |
DISABLED_RATE |
protected AtomicInteger |
globalCount |
protected static com.codahale.metrics.MetricRegistry |
REGISTRY |
protected static RateLimitRequestToken |
REJECT_REQUEST_TOKEN |
protected com.codahale.metrics.Meter |
rejectUiMeter |
protected com.codahale.metrics.Meter |
rejectUserMeter |
protected static String |
REQUEST_LIMIT_GLOBAL_KEY |
protected static String |
REQUEST_LIMIT_PER_USER_KEY |
protected static String |
REQUEST_LIMIT_UI_KEY |
protected com.codahale.metrics.Meter |
requestBypassMeter |
protected com.codahale.metrics.Counter |
requestGlobalCounter |
protected int |
requestLimitGlobal |
protected int |
requestLimitPerUser |
protected int |
requestLimitUi |
protected com.codahale.metrics.Meter |
requestUiMeter |
protected com.codahale.metrics.Meter |
requestUserMeter |
protected static SystemConfig |
SYSTEM_CONFIG |
protected Map<String,AtomicInteger> |
userCounts |
protected com.codahale.metrics.Counter |
usersCounter |
Constructor and Description |
---|
DefaultRateLimiter()
Loads defaults and creates DefaultRateLimiter.
|
Modifier and Type | Method and Description |
---|---|
protected RateLimitRequestToken |
createNewRateLimitRequestToken(AtomicInteger count,
String userName,
boolean isUIQuery,
int requestLimit,
com.codahale.metrics.Meter requestMeter,
com.codahale.metrics.Meter rejectMeter)
Creates a new RateLimitRequestToken.
|
protected RateLimitCleanupOnRequestComplete |
generateCleanupClosure(AtomicInteger count,
String userName)
Creates a callback to be passed to a token to execute when a request has completed.
|
protected AtomicInteger |
getCount(String userName)
Get the current count for this username.
|
RateLimitRequestToken |
getToken(javax.ws.rs.container.ContainerRequestContext request)
Request a token from the RateLimiter, which represents an in-flight request.
|
protected boolean |
incrementAndCheckCount(AtomicInteger initialCount,
int requestLimit)
Increment the initial count and check if the count has gone over the request limit.
|
protected void |
rejectRequest(com.codahale.metrics.Meter rejectMeter,
boolean isRejectGlobal,
boolean isUIQuery,
String userName)
Do the house keeping needed to reject the request.
|
protected static final SystemConfig SYSTEM_CONFIG
protected static final RateLimitRequestToken REJECT_REQUEST_TOKEN
protected static final RateLimitRequestToken BYPASS_TOKEN
@NotNull protected static final String REQUEST_LIMIT_GLOBAL_KEY
@NotNull protected static final String REQUEST_LIMIT_PER_USER_KEY
@NotNull protected static final String REQUEST_LIMIT_UI_KEY
protected static final int DEFAULT_REQUEST_LIMIT_GLOBAL
protected static final int DEFAULT_REQUEST_LIMIT_PER_USER
protected static final int DEFAULT_REQUEST_LIMIT_UI
protected static final com.codahale.metrics.MetricRegistry REGISTRY
protected static final int DISABLED_RATE
protected final int requestLimitGlobal
protected final int requestLimitPerUser
protected final int requestLimitUi
protected final AtomicInteger globalCount
protected final Map<String,AtomicInteger> userCounts
protected final com.codahale.metrics.Counter requestGlobalCounter
protected final com.codahale.metrics.Counter usersCounter
protected final com.codahale.metrics.Meter requestBypassMeter
protected final com.codahale.metrics.Meter requestUiMeter
protected final com.codahale.metrics.Meter requestUserMeter
protected final com.codahale.metrics.Meter rejectUiMeter
protected final com.codahale.metrics.Meter rejectUserMeter
public DefaultRateLimiter() throws SystemConfigException
SystemConfigException
- If any parameters fail to loadprotected AtomicInteger getCount(String userName)
userName
- Username to get the count forprotected boolean incrementAndCheckCount(AtomicInteger initialCount, int requestLimit)
initialCount
- Initial count that we're incrementing and checking against the limitrequestLimit
- Limit to check the incremented initial count againstprotected void rejectRequest(com.codahale.metrics.Meter rejectMeter, boolean isRejectGlobal, boolean isUIQuery, String userName)
rejectMeter
- Meter to count the rejection inisRejectGlobal
- Whether or not the rejection is on the global rate limitisUIQuery
- Whether or not the request is a UI QueryuserName
- Username of the user who made the requestpublic RateLimitRequestToken getToken(javax.ws.rs.container.ContainerRequestContext request)
RateLimiter
getToken
in interface RateLimiter
request
- The object representing the requestprotected RateLimitRequestToken createNewRateLimitRequestToken(AtomicInteger count, String userName, boolean isUIQuery, int requestLimit, com.codahale.metrics.Meter requestMeter, com.codahale.metrics.Meter rejectMeter)
count
- The atomic reference that holds the amount of in-flight requests the user ownsuserName
- The user who launched the requestisUIQuery
- Whether or not this query was generated from the UIrequestLimit
- The limit of requests the user is allowed to launchrequestMeter
- Meter tracking the amount of requests that have been launchedrejectMeter
- Meter tracking the amount of requests that have been rejectedprotected RateLimitCleanupOnRequestComplete generateCleanupClosure(AtomicInteger count, String userName)
count
- The AtomicInteger that stores the amount of in-flight requests an individual user ownsuserName
- The name of the user that made the requestCopyright © 2016–2018 Yahoo! Inc.. All rights reserved.