Class: Synthra::Limits

Inherits:
Object
  • Object
show all
Defined in:
lib/synthra/limits.rb

Overview

Resource limits to prevent runaway generation

The Limits class provides configurable safety limits that prevent accidental resource exhaustion during data generation. Each limit has a default value and can be customized per application needs.

Limits are validated during generation, and appropriate errors are raised when limits are exceeded.

Examples:

Access limits

limits = Synthra.configuration.limits
limits.max_latency_ms   # => 5000
limits.max_recursion    # => 10
limits.max_array_size   # => 1000

Configure limits

Synthra.configure do |config|
  config.limits.max_latency_ms = 10000   # Allow up to 10 second delays
  config.limits.max_recursion = 3        # Limit nesting to 3 levels
  config.limits.max_array_size = 50      # Limit arrays to 50 elements
end

Handle limit errors

begin
  schema.generate
rescue Synthra::LatencyLimitError => e
  puts "Latency too high: #{e.requested}ms > #{e.max_allowed}ms"
rescue Synthra::RecursionLimitError => e
  puts "Too deeply nested: #{e.depth} > #{e.max_allowed}"
rescue Synthra::ArraySizeLimitError => e
  puts "Array too large: #{e.size} > #{e.max_allowed}"
end

Constant Summary collapse

DEFAULT_MAX_LATENCY_MS =

Default maximum latency in milliseconds (5 seconds)

This prevents behaviors from creating excessively long delays that could cause timeouts in tests.

Returns:

  • (Integer)

    default maximum latency in milliseconds

5000
DEFAULT_MAX_RECURSION =

Default maximum recursion depth (10 levels)

This prevents infinite recursion when schemas reference each other. Most real-world schemas don't need more than 5-10 levels of nesting.

Returns:

  • (Integer)

    default maximum recursion depth

10
DEFAULT_MAX_ARRAY_SIZE =

Default maximum array size (1000 elements)

This prevents generating extremely large arrays that could consume excessive memory or take too long to generate.

Returns:

  • (Integer)

    default maximum array elements

1000
DEFAULT_MAX_TEXT_LENGTH =

Default maximum text length (10,000 characters)

This prevents generating extremely long text strings that could consume excessive memory or cause performance issues.

Returns:

  • (Integer)

    default maximum text length in characters

10_000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLimits

Create a new Limits instance with default values

Examples:

limits = Synthra::Limits.new
limits.max_latency_ms  # => 5000
limits.max_recursion   # => 10
limits.max_array_size  # => 1000


168
169
170
171
172
173
# File 'lib/synthra/limits.rb', line 168

def initialize
  @max_latency_ms = DEFAULT_MAX_LATENCY_MS
  @max_recursion = DEFAULT_MAX_RECURSION
  @max_array_size = DEFAULT_MAX_ARRAY_SIZE
  @max_text_length = DEFAULT_MAX_TEXT_LENGTH
end

Instance Attribute Details

#max_array_sizeInteger

Maximum allowed number of elements in generated arrays

Returns:

  • (Integer)

    maximum array size

See Also:



142
143
144
# File 'lib/synthra/limits.rb', line 142

def max_array_size
  @max_array_size
end

#max_latency_msInteger

Maximum allowed latency for @latency behaviors

Returns:

  • (Integer)

    maximum latency in milliseconds

See Also:



118
119
120
# File 'lib/synthra/limits.rb', line 118

def max_latency_ms
  @max_latency_ms
end

#max_recursionInteger

Maximum allowed recursion depth for nested schemas

Returns:

  • (Integer)

    maximum nesting depth

See Also:



130
131
132
# File 'lib/synthra/limits.rb', line 130

def max_recursion
  @max_recursion
end

#max_text_lengthInteger

Maximum allowed length for generated text strings

Returns:

  • (Integer)

    maximum text length in characters

See Also:



154
155
156
# File 'lib/synthra/limits.rb', line 154

def max_text_length
  @max_text_length
end

Instance Method Details

#validate_array_size!(size) ⇒ void

This method returns an undefined value.

Validate that an array size is within limits

Checks if the requested array size is within the configured maximum. This prevents generating arrays with excessive elements that could exhaust memory.

Examples:

Valid size

limits.validate_array_size!(100)  # => nil (100 elements is OK)

Invalid size

limits.max_array_size = 1000
limits.validate_array_size!(5000)
# => ArraySizeLimitError: Array size 5000 exceeds maximum allowed 1000

Parameters:

  • size (Integer)

    the array size to validate

Raises:



246
247
248
249
250
# File 'lib/synthra/limits.rb', line 246

def validate_array_size!(size)
  return if size <= max_array_size

  raise ArraySizeLimitError.new(size: size, max_allowed: max_array_size)
end

#validate_latency!(ms) ⇒ void

This method returns an undefined value.

Validate that a latency value is within limits

Checks if the requested latency (in milliseconds) is within the configured maximum. If not, raises a LatencyLimitError.

Examples:

Valid latency

limits.validate_latency!(1000)  # => nil (1 second is OK)

Invalid latency

limits.max_latency_ms = 5000
limits.validate_latency!(10000)
# => LatencyLimitError: Latency 10000ms exceeds maximum allowed 5000ms

Parameters:

  • ms (Integer)

    the latency value in milliseconds to validate

Raises:



194
195
196
197
198
# File 'lib/synthra/limits.rb', line 194

def validate_latency!(ms)
  return if ms <= max_latency_ms

  raise LatencyLimitError.new(requested: ms, max_allowed: max_latency_ms)
end

#validate_recursion!(depth) ⇒ void

This method returns an undefined value.

Validate that a recursion depth is within limits

Checks if the current recursion depth is within the configured maximum. This is called during nested schema generation to prevent infinite recursion.

Examples:

Valid depth

limits.validate_recursion!(5)  # => nil (5 levels is OK)

Invalid depth

limits.max_recursion = 10
limits.validate_recursion!(15)
# => RecursionLimitError: Recursion depth 15 exceeds maximum allowed 10

Parameters:

  • depth (Integer)

    the current recursion depth to validate

Raises:



220
221
222
223
224
# File 'lib/synthra/limits.rb', line 220

def validate_recursion!(depth)
  return if depth < max_recursion

  raise RecursionLimitError.new(depth: depth, max_allowed: max_recursion)
end

#validate_text_length!(length) ⇒ void

This method returns an undefined value.

Validate that a text length is within limits

Checks if the requested text length is within the configured maximum. This prevents generating text strings with excessive characters that could exhaust memory.

Examples:

Valid length

limits.validate_text_length!(1000)  # => nil (1000 chars is OK)

Invalid length

limits.max_text_length = 10000
limits.validate_text_length!(20000)
# => TextLengthLimitError: Text length 20000 exceeds maximum allowed 10000

Parameters:

  • length (Integer)

    the text length in characters to validate

Raises:



272
273
274
275
276
277
# File 'lib/synthra/limits.rb', line 272

def validate_text_length!(length)
  return if length <= max_text_length
  # :nocov:
  raise TextLengthLimitError.new(length: length, max_allowed: max_text_length)
  # :nocov:
end