Class RateLimiter
java.lang.Object
services.moleculer.service.MoleculerComponent
services.moleculer.service.Service
services.moleculer.web.middleware.HttpMiddleware
services.moleculer.web.middleware.RateLimiter
- All Implemented Interfaces:
services.moleculer.service.MoleculerLifecycle, HttpConstants
Rate Limiter limits concurrent constant requests to the HTTP calls in the
application. Sample:
route.use(new RateLimiter(100, true));
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanApply the performance cutoff for each service (= true), otherwise (= false) it will only limit the load with the Actions marked with the 'RateLimit' annotation.protected booleanAdd headers to all HTTP response (eg.protected longDefault rate limitprotected RatingStoreFactoryHits per IP-addresses storeprotected TimeUnitUnit of the time window/frameprotected longTime "window" (time frame) lengthstatic final Stringstatic final Stringstatic final StringFields inherited from class services.moleculer.service.MoleculerComponent
broker, logger, nameFields inherited from interface HttpConstants
ACCEPT, ACCEPT_ENCODING, AUTHORIZATION, CACHE_CONTROL, CHUNKED, CLOSE, CONNECT, CONNECTION, CONTENT_ENCODING, CONTENT_LENGTH, CONTENT_TYPE, CONTENT_TYPE_HTML, CONTENT_TYPE_JSON, COOKIE, DEFLATE, DELETE, ETAG, GET, GZIP, HEAD, IF_NONE_MATCH, KEEP_ALIVE, LOCATION, META_CONTENT_TYPE, META_HEADERS, META_LOCALE, META_LOCATION, META_SESSION, META_STATUS, META_TEMPLATE, NO_CACHE, OPTIONS, PATCH, POST, PROPERTY_COOKIES, PROPERTY_SESSION_ID, PROPERTY_USER, PUT, SET_COOKIE, TRACE, TRANSFER_ENCODING, WWW_AUTHENTICATE, X_FORWARDED_FOR -
Constructor Summary
ConstructorsConstructorDescriptionCreates a RateLimiter with default settings (50 requests / second for all URLs).RateLimiter(boolean applyForAll) Creates a RateLimiter.RateLimiter(int rateLimit, boolean applyForAll) RateLimiter(int rateLimit, int window, TimeUnit unit, boolean applyForAll) -
Method Summary
Modifier and TypeMethodDescriptionlonggetUnit()longinstall(RequestProcessor next, io.datatree.Tree config) booleanbooleanvoidsetApplyForAll(boolean applyForAll) voidsetHeaders(boolean headers) voidsetRateLimit(long rateLimit) voidsetStoreFactory(RatingStoreFactory storeFactory) voidvoidsetWindow(long window) voidstarted(services.moleculer.ServiceBroker broker) voidstopped()Methods inherited from class services.moleculer.service.MoleculerComponent
getBroker, getLogger, getName
-
Field Details
-
X_HEADER_LIMIT
- See Also:
-
X_HEADER_REMAINING
- See Also:
-
X_HEADER_RESET
- See Also:
-
applyForAll
protected boolean applyForAllApply the performance cutoff for each service (= true), otherwise (= false) it will only limit the load with the Actions marked with the 'RateLimit' annotation. -
limit
protected long limitDefault rate limit -
window
protected long windowTime "window" (time frame) length -
headers
protected boolean headersAdd headers to all HTTP response (eg. "X-Rate-Limit-Remaining", etc.) -
unit
Unit of the time window/frame -
storeFactory
Hits per IP-addresses store
-
-
Constructor Details
-
RateLimiter
public RateLimiter()Creates a RateLimiter with default settings (50 requests / second for all URLs). -
RateLimiter
public RateLimiter(boolean applyForAll) Creates a RateLimiter. It is adjustable to consider the 'RateLimit' annotation.- Parameters:
applyForAll- apply the performance cutoff for each service (= true), otherwise (= false) it will only limit the load with the Actions marked with the 'RateLimit' annotation.
-
RateLimiter
public RateLimiter(int rateLimit, boolean applyForAll) -
RateLimiter
-
-
Method Details
-
started
-
install
- Specified by:
installin classHttpMiddleware
-
stopped
public void stopped()- Specified by:
stoppedin interfaceservices.moleculer.service.MoleculerLifecycle- Overrides:
stoppedin classservices.moleculer.service.MoleculerComponent
-
isApplyForAll
public boolean isApplyForAll() -
setApplyForAll
public void setApplyForAll(boolean applyForAll) -
getRateLimit
public long getRateLimit() -
setRateLimit
public void setRateLimit(long rateLimit) -
getWindow
public long getWindow() -
setWindow
public void setWindow(long window) -
getUnit
-
setUnit
-
getStoreFactory
-
setStoreFactory
-
isHeaders
public boolean isHeaders() -
setHeaders
public void setHeaders(boolean headers)
-