mirror of
https://github.com/Shopify/liquid.git
synced 2026-10-02 16:45:12 -07:00
Update assign_score during capturing
To stop long captures before they grow the heap more then they should.
This commit is contained in:
@@ -170,7 +170,7 @@ module Liquid
|
|||||||
end
|
end
|
||||||
idx += 1
|
idx += 1
|
||||||
|
|
||||||
context.resource_limits.check_render_length(output.bytesize)
|
context.resource_limits.increment_write_score(output)
|
||||||
end
|
end
|
||||||
|
|
||||||
output
|
output
|
||||||
|
|||||||
@@ -22,8 +22,16 @@ module Liquid
|
|||||||
raise_limits_reached if @assign_score_limit && @assign_score > @assign_score_limit
|
raise_limits_reached if @assign_score_limit && @assign_score > @assign_score_limit
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_render_length(output_byte_size)
|
# update either render_length or assign_score based on whether or not the writes are captured
|
||||||
raise_limits_reached if @render_length_limit && output_byte_size > @render_length_limit
|
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
|
end
|
||||||
|
|
||||||
def raise_limits_reached
|
def raise_limits_reached
|
||||||
@@ -37,7 +45,18 @@ module Liquid
|
|||||||
|
|
||||||
def reset
|
def reset
|
||||||
@reached_limit = false
|
@reached_limit = false
|
||||||
|
@last_capture_length = nil
|
||||||
@render_score = @assign_score = 0
|
@render_score = @assign_score = 0
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def with_capture
|
||||||
|
old_capture_length = @last_capture_length
|
||||||
|
begin
|
||||||
|
@last_capture_length = 0
|
||||||
|
yield
|
||||||
|
ensure
|
||||||
|
@last_capture_length = old_capture_length
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -25,9 +25,10 @@ module Liquid
|
|||||||
end
|
end
|
||||||
|
|
||||||
def render_to_output_buffer(context, output)
|
def render_to_output_buffer(context, output)
|
||||||
|
context.resource_limits.with_capture do
|
||||||
capture_output = render(context)
|
capture_output = render(context)
|
||||||
context.scopes.last[@to] = capture_output
|
context.scopes.last[@to] = capture_output
|
||||||
context.resource_limits.increment_assign_score(capture_output.bytesize)
|
end
|
||||||
output
|
output
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user