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

public class RateLimiter extends HttpMiddleware implements HttpConstants
Rate Limiter limits concurrent constant requests to the HTTP calls in the application. Sample:
route.use(new RateLimiter(100, true));
  • Field Details

    • X_HEADER_LIMIT

      public static final String X_HEADER_LIMIT
      See Also:
    • X_HEADER_REMAINING

      public static final String X_HEADER_REMAINING
      See Also:
    • X_HEADER_RESET

      public static final String X_HEADER_RESET
      See Also:
    • applyForAll

      protected boolean 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.
    • limit

      protected long limit
      Default rate limit
    • window

      protected long window
      Time "window" (time frame) length
    • headers

      protected boolean headers
      Add headers to all HTTP response (eg. "X-Rate-Limit-Remaining", etc.)
    • unit

      protected TimeUnit unit
      Unit of the time window/frame
    • storeFactory

      protected RatingStoreFactory 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

      public RateLimiter(int rateLimit, int window, TimeUnit unit, boolean applyForAll)
  • Method Details

    • started

      public void started(services.moleculer.ServiceBroker broker) throws Exception
      Specified by:
      started in interface services.moleculer.service.MoleculerLifecycle
      Overrides:
      started in class services.moleculer.service.MoleculerComponent
      Throws:
      Exception
    • install

      public RequestProcessor install(RequestProcessor next, io.datatree.Tree config)
      Specified by:
      install in class HttpMiddleware
    • stopped

      public void stopped()
      Specified by:
      stopped in interface services.moleculer.service.MoleculerLifecycle
      Overrides:
      stopped in class services.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

      public TimeUnit getUnit()
    • setUnit

      public void setUnit(TimeUnit unit)
    • getStoreFactory

      public RatingStoreFactory getStoreFactory()
    • setStoreFactory

      public void setStoreFactory(RatingStoreFactory storeFactory)
    • isHeaders

      public boolean isHeaders()
    • setHeaders

      public void setHeaders(boolean headers)