Interface RateLimiterFactory

  • All Superinterfaces:
    java.lang.AutoCloseable, java.io.Serializable
    All Known Implementing Classes:
    EnvoyRateLimiterFactory

    public interface RateLimiterFactory
    extends java.io.Serializable, java.lang.AutoCloseable
    A factory that manages connections to rate limit service and creates lightweight handles.

    Implementations must be Serializable as they are passed to workers. The factory typically manages the heavy connection (e.g. gRPC stub) and is thread-safe.

    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      boolean allow​(RateLimiterContext context, int permits)
      Blocks until the specified number of permits are acquired and returns true if the request was allowed or false if the request was rejected.
      RateLimiter getLimiter​(RateLimiterContext context)
      Creates a lightweight ratelimiter handle bound to a specific context.
      • Methods inherited from interface java.lang.AutoCloseable

        close
    • Method Detail

      • getLimiter

        RateLimiter getLimiter​(RateLimiterContext context)
        Creates a lightweight ratelimiter handle bound to a specific context.

        Use this when passing ratelimiter to IO components, which doesn't need to know about the configuration or the underlying ratelimiter service details. This is also useful in DoFns when you want to use the ratelimiter in a static way based on the compile time context.

        Parameters:
        context - The context for the ratelimit.
        Returns:
        A RateLimiter handle.
      • allow

        boolean allow​(RateLimiterContext context,
                      int permits)
               throws java.io.IOException,
                      java.lang.InterruptedException
        Blocks until the specified number of permits are acquired and returns true if the request was allowed or false if the request was rejected.

        Use this for when the ratelimit namespace or descriptors are not known at compile time. allows you to use the ratelimiter in a dynamic way based on the runtime data.

        Parameters:
        context - The context for the ratelimit.
        permits - Number of permits to acquire.
        Returns:
        true if the request is allowed, false if rejected.
        Throws:
        java.io.IOException - if there is an error communicating with the ratelimiter service.
        java.lang.InterruptedException - if the thread is interrupted while waiting.