Class: Liquid::ResourceLimits

Inherits:
Object
  • Object
show all
Defined in:
lib/liquid/resource_limits.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(limits) ⇒ ResourceLimits

Returns a new instance of ResourceLimits.



8
9
10
11
12
13
# File 'lib/liquid/resource_limits.rb', line 8

def initialize(limits)
  @render_length_limit = limits[:render_length_limit]
  @render_score_limit  = limits[:render_score_limit]
  @assign_score_limit  = limits[:assign_score_limit]
  reset
end

Instance Attribute Details

#assign_scoreObject (readonly)

Returns the value of attribute assign_score.



6
7
8
# File 'lib/liquid/resource_limits.rb', line 6

def assign_score
  @assign_score
end

#assign_score_limitObject

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

#render_length_limitObject

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_scoreObject (readonly)

Returns the value of attribute render_score.



6
7
8
# File 'lib/liquid/resource_limits.rb', line 6

def render_score
  @render_score
end

#render_score_limitObject

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



20
21
22
23
# File 'lib/liquid/resource_limits.rb', line 20

def increment_assign_score(amount)
  @assign_score += amount
  raise_limits_reached if @assign_score_limit && @assign_score > @assign_score_limit
end

#increment_render_score(amount) ⇒ Object



15
16
17
18
# File 'lib/liquid/resource_limits.rb', line 15

def increment_render_score(amount)
  @render_score += amount
  raise_limits_reached if @render_score_limit && @render_score > @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



26
27
28
29
30
31
32
33
34
35
# File 'lib/liquid/resource_limits.rb', line 26

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_reachedObject

Raises:



37
38
39
40
# File 'lib/liquid/resource_limits.rb', line 37

def raise_limits_reached
  @reached_limit = true
  raise MemoryError, "Memory limits exceeded"
end

#reached?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/liquid/resource_limits.rb', line 42

def reached?
  @reached_limit
end

#resetObject



46
47
48
49
50
# File 'lib/liquid/resource_limits.rb', line 46

def reset
  @reached_limit = false
  @last_capture_length = nil
  @render_score = @assign_score = 0
end

#with_captureObject



52
53
54
55
56
57
58
59
60
# File 'lib/liquid/resource_limits.rb', line 52

def with_capture
  old_capture_length = @last_capture_length
  begin
    @last_capture_length = 0
    yield
  ensure
    @last_capture_length = old_capture_length
  end
end