Class DmxRateLimiter

java.lang.Object
dmx.fun.resilience4j.DmxRateLimiter

@NullMarked public final class DmxRateLimiter extends Object
dmx-fun adapter for Resilience4J RateLimiter.

Executes a supplier through the rate limiter and returns a dmx-fun type instead of throwing. Configure the rate limiter using the native Resilience4J API:

DmxRateLimiter rl = DmxRateLimiter.of("payments", RateLimiterConfig.ofDefaults());

Try<Receipt>                         r1 = rl.executeTry(() -> gateway.charge(amount));
Result<Receipt, Throwable>           r2 = rl.executeResult(() -> gateway.charge(amount));
Result<Receipt, RequestNotPermitted> r3 = rl.executeResultTyped(() -> gateway.charge(amount));
  • Method Details

    • of

      public static DmxRateLimiter of(io.github.resilience4j.ratelimiter.RateLimiter rateLimiter)
      Wraps an existing RateLimiter instance.
      Parameters:
      rateLimiter - the Resilience4J rate limiter to wrap
      Returns:
      a new DmxRateLimiter backed by the given rate limiter
    • of

      public static DmxRateLimiter of(String name, io.github.resilience4j.ratelimiter.RateLimiterConfig config)
      Creates a new RateLimiter from the given name and config, then wraps it.
      Parameters:
      name - the rate limiter name
      config - the rate limiter configuration
      Returns:
      a new DmxRateLimiter backed by the created rate limiter
    • executeTry

      public <V> Try<V> executeTry(CheckedSupplier<V> supplier)
      Executes the supplier through the rate limiter.
      Type Parameters:
      V - the value type
      Parameters:
      supplier - the operation to execute
      Returns:
      Success(value) on success, Failure(RequestNotPermitted) when the rate limit is exceeded, or Failure(cause) when the call itself fails
    • executeResult

      public <V> Result<V, Throwable> executeResult(CheckedSupplier<V> supplier)
      Executes the supplier through the rate limiter.
      Type Parameters:
      V - the value type
      Parameters:
      supplier - the operation to execute
      Returns:
      Ok(value) on success, Err(cause) on any failure
    • executeResultTyped

      public <V> Result<V, io.github.resilience4j.ratelimiter.RequestNotPermitted> executeResultTyped(CheckedSupplier<V> supplier)
      Executes the supplier through the rate limiter, surfacing rate-limit rejections as a typed error.
      Type Parameters:
      V - the value type
      Parameters:
      supplier - the operation to execute
      Returns:
      Ok(value) on success, Err(RequestNotPermitted) when the rate limit is exceeded; other exceptions from the call propagate as unchecked