Class: Liquid::ResourceLimits
- Inherits:
-
Object
- Object
- Liquid::ResourceLimits
- Defined in:
- lib/liquid/resource_limits.rb
Instance Attribute Summary collapse
-
#assign_score ⇒ Object
readonly
Returns the value of attribute assign_score.
-
#assign_score_limit ⇒ Object
Returns the value of attribute assign_score_limit.
-
#cumulative_assign_score ⇒ Object
readonly
Returns the value of attribute cumulative_assign_score.
-
#cumulative_assign_score_limit ⇒ Object
Returns the value of attribute cumulative_assign_score_limit.
-
#cumulative_render_score ⇒ Object
readonly
Returns the value of attribute cumulative_render_score.
-
#cumulative_render_score_limit ⇒ Object
Returns the value of attribute cumulative_render_score_limit.
-
#render_length_limit ⇒ Object
Returns the value of attribute render_length_limit.
-
#render_score ⇒ Object
readonly
Returns the value of attribute render_score.
-
#render_score_limit ⇒ Object
Returns the value of attribute render_score_limit.
Instance Method Summary collapse
- #increment_assign_score(amount) ⇒ Object
- #increment_render_score(amount) ⇒ Object
-
#increment_write_score(output) ⇒ Object
update either render_length or assign_score based on whether or not the writes are captured.
-
#initialize(limits) ⇒ ResourceLimits
constructor
A new instance of ResourceLimits.
- #raise_limits_reached ⇒ Object
- #reached? ⇒ Boolean
- #reset ⇒ Object
- #with_capture ⇒ Object
Constructor Details
#initialize(limits) ⇒ ResourceLimits
Returns a new instance of ResourceLimits.
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/liquid/resource_limits.rb', line 15 def initialize(limits) @render_length_limit = limits[:render_length_limit] @render_score_limit = limits[:render_score_limit] @assign_score_limit = limits[:assign_score_limit] @cumulative_render_score_limit = limits[:cumulative_render_score_limit] @cumulative_assign_score_limit = limits[:cumulative_assign_score_limit] @cumulative_render_score = 0 @cumulative_assign_score = 0 reset end |
Instance Attribute Details
#assign_score ⇒ Object (readonly)
Returns the value of attribute assign_score.
10 11 12 |
# File 'lib/liquid/resource_limits.rb', line 10 def assign_score @assign_score end |
#assign_score_limit ⇒ Object
Returns the value of attribute assign_score_limit.
5 6 7 |
# File 'lib/liquid/resource_limits.rb', line 5 def assign_score_limit @assign_score_limit end |
#cumulative_assign_score ⇒ Object (readonly)
Returns the value of attribute cumulative_assign_score.
10 11 12 |
# File 'lib/liquid/resource_limits.rb', line 10 def cumulative_assign_score @cumulative_assign_score end |
#cumulative_assign_score_limit ⇒ Object
Returns the value of attribute cumulative_assign_score_limit.
5 6 7 |
# File 'lib/liquid/resource_limits.rb', line 5 def cumulative_assign_score_limit @cumulative_assign_score_limit end |
#cumulative_render_score ⇒ Object (readonly)
Returns the value of attribute cumulative_render_score.
10 11 12 |
# File 'lib/liquid/resource_limits.rb', line 10 def cumulative_render_score @cumulative_render_score end |
#cumulative_render_score_limit ⇒ Object
Returns the value of attribute cumulative_render_score_limit.
5 6 7 |
# File 'lib/liquid/resource_limits.rb', line 5 def cumulative_render_score_limit @cumulative_render_score_limit end |
#render_length_limit ⇒ Object
Returns the value of attribute render_length_limit.
5 6 7 |
# File 'lib/liquid/resource_limits.rb', line 5 def render_length_limit @render_length_limit end |
#render_score ⇒ Object (readonly)
Returns the value of attribute render_score.
10 11 12 |
# File 'lib/liquid/resource_limits.rb', line 10 def render_score @render_score end |
#render_score_limit ⇒ Object
Returns the value of attribute render_score_limit.
5 6 7 |
# File 'lib/liquid/resource_limits.rb', line 5 def render_score_limit @render_score_limit end |
Instance Method Details
#increment_assign_score(amount) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/liquid/resource_limits.rb', line 33 def increment_assign_score(amount) @assign_score += amount @cumulative_assign_score += amount raise_limits_reached if @assign_score_limit && @assign_score > @assign_score_limit raise_limits_reached if @cumulative_assign_score_limit && @cumulative_assign_score > @cumulative_assign_score_limit end |
#increment_render_score(amount) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/liquid/resource_limits.rb', line 26 def increment_render_score(amount) @render_score += amount @cumulative_render_score += amount raise_limits_reached if @render_score_limit && @render_score > @render_score_limit raise_limits_reached if @cumulative_render_score_limit && @cumulative_render_score > @cumulative_render_score_limit end |
#increment_write_score(output) ⇒ Object
update either render_length or assign_score based on whether or not the writes are captured
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/liquid/resource_limits.rb', line 41 def increment_write_score(output) if (last_captured = @last_capture_length) captured = output.bytesize increment = captured - last_captured @last_capture_length = captured increment_assign_score(increment) elsif @render_length_limit && output.bytesize > @render_length_limit raise_limits_reached end end |
#raise_limits_reached ⇒ Object
52 53 54 55 |
# File 'lib/liquid/resource_limits.rb', line 52 def raise_limits_reached @reached_limit = true raise MemoryError, "Memory limits exceeded" end |
#reached? ⇒ Boolean
57 58 59 |
# File 'lib/liquid/resource_limits.rb', line 57 def reached? @reached_limit end |
#reset ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/liquid/resource_limits.rb', line 61 def reset @reached_limit = false @last_capture_length = nil @render_score = @assign_score = 0 raise_limits_reached if @cumulative_render_score_limit && @cumulative_render_score > @cumulative_render_score_limit raise_limits_reached if @cumulative_assign_score_limit && @cumulative_assign_score > @cumulative_assign_score_limit end |
#with_capture ⇒ Object
69 70 71 72 73 74 75 76 77 |
# File 'lib/liquid/resource_limits.rb', line 69 def with_capture old_capture_length = @last_capture_length begin @last_capture_length = 0 yield ensure @last_capture_length = old_capture_length end end |