Interface RateLimiterFactory

All Superinterfaces:
AutoCloseable, Serializable
All Known Implementing Classes:
EnvoyRateLimiterFactory

public interface RateLimiterFactory extends Serializable, 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

    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.
    Creates a lightweight ratelimiter handle bound to a specific context.

    Methods inherited from interface java.lang.AutoCloseable

    close
  • Method Details

    • 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 IOException, 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:
      IOException - if there is an error communicating with the ratelimiter service.
      InterruptedException - if the thread is interrupted while waiting.