Class: Synthra::Limits
- Inherits:
-
Object
- Object
- Synthra::Limits
- 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.
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.
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.
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.
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.
10_000
Instance Attribute Summary collapse
-
#max_array_size ⇒ Integer
Maximum allowed number of elements in generated arrays.
-
#max_latency_ms ⇒ Integer
Maximum allowed latency for @latency behaviors.
-
#max_recursion ⇒ Integer
Maximum allowed recursion depth for nested schemas.
-
#max_text_length ⇒ Integer
Maximum allowed length for generated text strings.
Instance Method Summary collapse
-
#initialize ⇒ Limits
constructor
Create a new Limits instance with default values.
-
#validate_array_size!(size) ⇒ void
Validate that an array size is within limits.
-
#validate_latency!(ms) ⇒ void
Validate that a latency value is within limits.
-
#validate_recursion!(depth) ⇒ void
Validate that a recursion depth is within limits.
-
#validate_text_length!(length) ⇒ void
Validate that a text length is within limits.
Constructor Details
#initialize ⇒ Limits
Create a new Limits instance with default values
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_size ⇒ Integer
Maximum allowed number of elements in generated arrays
142 143 144 |
# File 'lib/synthra/limits.rb', line 142 def max_array_size @max_array_size end |
#max_latency_ms ⇒ Integer
Maximum allowed latency for @latency behaviors
118 119 120 |
# File 'lib/synthra/limits.rb', line 118 def max_latency_ms @max_latency_ms end |
#max_recursion ⇒ Integer
Maximum allowed recursion depth for nested schemas
130 131 132 |
# File 'lib/synthra/limits.rb', line 130 def max_recursion @max_recursion end |
#max_text_length ⇒ Integer
Maximum allowed length for generated text strings
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.
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.
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.
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.
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 |