Class RateLimitExecutor

java.lang.Object
ai.nervemind.app.executor.RateLimitExecutor
All Implemented Interfaces:
NodeExecutor

@Component public class RateLimitExecutor extends Object implements NodeExecutor
Executor for the "rate_limit" node type - throttles operations using rate limiting algorithms.

Prevents overwhelming external services by limiting the rate of operations. Supports two rate limiting strategies: Token Bucket (smooth rate limiting) and Sliding Window (strict per-window limits). Rate limit buckets are shared globally across workflow executions.

Node Parameters

Rate limit node configuration parameters
Parameter Type Default Description
operations List [] Operations to execute with rate limiting
bucketId String "default" Rate limit bucket identifier
strategy String "token_bucket" "token_bucket" or "sliding_window"
waitForTokens Boolean true Wait for tokens if unavailable
maxWaitMs Long 60000 Max wait time for tokens

Token Bucket Parameters

Token bucket specific parameters
Parameter Type Default Description
tokensPerSecond Double 10.0 Rate of token refill
maxTokens Integer 100 Maximum bucket capacity
tokensPerRequest Integer 1 Tokens consumed per request

Sliding Window Parameters

Sliding window specific parameters
Parameter Type Default Description
windowSizeMs Long 1000 Window duration in milliseconds
maxRequestsPerWindow Integer 10 Max requests per window

Output Data

Output keys added by this executor
Key Type Description
success Boolean True if rate limit acquired and operation succeeded
throttled Boolean True if request was rejected due to rate limit
waitedMs Long Time spent waiting for rate limit
tokensRemaining Double Tokens left (token bucket only)
requestsInWindow Integer Requests in window (sliding window only)

Shared Buckets

Buckets are stored in static ConcurrentHashMaps and shared across all workflow executions. Use unique bucketIds to isolate rate limits.

See Also:
  • Constructor Details

    • RateLimitExecutor

      public RateLimitExecutor(@Lazy NodeExecutorRegistry nodeExecutorRegistry)
      Creates a new RateLimitExecutor with the given node executor registry.
      Parameters:
      nodeExecutorRegistry - the registry for accessing other node executors
  • Method Details

    • getNodeType

      public String getNodeType()
      Description copied from interface: NodeExecutor
      Unique identifier for the node type this executor handles. This must match the 'type' field in the JSON definition of the node.
      Specified by:
      getNodeType in interface NodeExecutor
      Returns:
      The unique type string (e.g., "httpRequest", "llmChat").
    • execute

      public Map<String,Object> execute(ai.nervemind.common.domain.Node node, Map<String,Object> input, ExecutionService.ExecutionContext context)
      Description copied from interface: NodeExecutor
      Executes the business logic for this node type.
      Specified by:
      execute in interface NodeExecutor
      Parameters:
      node - The node definition containing parameters and configuration. Use Node.parameters() to access user settings.
      input - Combined output from all upstream nodes that connected to this node. For simple flows, this contains the direct predecessor's output. For merge nodes, it contains combined data.
      context - Verification context providing access to workflow-scoped services, logger, and execution metadata.
      Returns:
      A Map containing the results of this node's execution. Keys in this map become available variables for downstream nodes.
      Note: Returning null is treated as an empty map.
    • clearAllBuckets

      public static void clearAllBuckets()
      Clear all rate limit buckets. Useful for testing.
    • clearBucket

      public static void clearBucket(String bucketId)
      Clear a specific bucket.
      Parameters:
      bucketId - the ID of the bucket to clear