Skip to content

Commit

Permalink
Retry once on Zlib::BufError
Browse files Browse the repository at this point in the history
  • Loading branch information
ianks committed Jan 16, 2024
1 parent f6c7e3b commit 79f06d0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
15 changes: 14 additions & 1 deletion lib/response_bank.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,20 @@ def read_from_backing_cache_store(_env, cache_key, backing_cache_store: cache_st
def compress(content, encoding = "br")
case encoding
when 'gzip'
Zlib.gzip(content, level: Zlib::BEST_COMPRESSION)
attempts = 0

begin
Zlib.gzip(content, level: Zlib::BEST_COMPRESSION)
rescue Zlib::BufError
# We get sporadic Zlib::BufError, so we retry once (https://github.com/ruby/zlib/issues/49)
attempts += 1

if attempts <= 1
retry
else
raise
end
end
when 'br'
Brotli.deflate(content, mode: :text, quality: 7)
else
Expand Down
16 changes: 16 additions & 0 deletions test/response_bank_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,20 @@ def test_cache_key_for_datetime
def test_cache_key_for_date
assert_equal("2020-01-01", ResponseBank.cache_key_for(Date.new(2020, 1, 1)))
end

def test_compress_retries_once_on_zlib_buferror
content = 'mycontent'
compressed_content = Zlib.gzip(content, level: Zlib::BEST_COMPRESSION)
Zlib.stubs(:gzip).raises(Zlib::BufError).then.returns(compressed_content)

assert_equal(compressed_content, ResponseBank.compress(content, "gzip"))
end

def test_compress_retries_once_on_zlib_buferror_and_raises_if_it_happens_again
Zlib.stubs(:gzip).raises(Zlib::BufError)

assert_raises(Zlib::BufError) do
ResponseBank.compress("mycontent", "gzip")
end
end
end
4 changes: 2 additions & 2 deletions test/response_cache_handler_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def test_server_cache_hit_but_empty_body
_status, _headers, _body, _timestamp = empty_page
ResponseBank.expects(:decompress).never

status, headers, body = handler.run!
status, headers, _body = handler.run!

assert_equal(_status, status)
assert_equal(_headers['Content-Type'], headers["Content-Type"])
Expand All @@ -143,7 +143,7 @@ def test_server_cache_hit_support_gzip

ResponseBank.expects(:decompress).returns(_body).once

status, headers, body = handler.run!
status, headers, _body = handler.run!

assert_equal(_status, status)
assert_equal(_headers['Content-Type'], headers["Content-Type"])
Expand Down

0 comments on commit 79f06d0

Please sign in to comment.