Skip to content

Commit

Permalink
Fix setting default config values for multi-threaded applciations
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-lord committed Oct 15, 2017
1 parent e7e4007 commit 38fa924
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/regexp-examples/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ def config=(**args)
end

def config
Thread.current[:regexp_examples_config] ||= {}
Thread.current[:regexp_examples_config] ||= {
max_repeater_variance: MAX_REPEATER_VARIANCE_DEFAULT,
max_group_results: MAX_GROUP_RESULTS_DEFAULT,
max_results_limit: MAX_RESULTS_LIMIT_DEFAULT
}
end
end
# The maximum variance for any given repeater, to prevent a huge/infinite number of
Expand All @@ -44,21 +48,18 @@ def config
# .{,3} is equivalent to .{0,2}
# .{3,8} is equivalent to .{3,5}
MAX_REPEATER_VARIANCE_DEFAULT = 2
self.max_repeater_variance = MAX_REPEATER_VARIANCE_DEFAULT

# Maximum number of characters returned from a char set, to reduce output spam
# For example, if self.max_group_results = 5 then:
# \d is equivalent to [01234]
# \w is equivalent to [abcde]
MAX_GROUP_RESULTS_DEFAULT = 5
self.max_group_results = MAX_GROUP_RESULTS_DEFAULT

# Maximum number of results to be generated, for Regexp#examples
# This is to prevent the system "freezing" when given instructions like:
# /[ab]{30}/.examples
# (Which would attempt to generate 2**30 == 1073741824 examples!!!)
MAX_RESULTS_LIMIT_DEFAULT = 10_000
self.max_results_limit = MAX_RESULTS_LIMIT_DEFAULT
end

# Definitions of various special characters, used in regular expressions.
Expand Down

0 comments on commit 38fa924

Please sign in to comment.