自从从 Redis To Go 切换到 Heroku Redis 后,我们的 Ruby on Rails 应用程序中的 Redis 代码每天都会出现几次“OpenSSL::SSL::SSLError: SSL_read: sslv3 alert bad record mac”错误。
任何想法为什么?
自从从 Redis To Go 切换到 Heroku Redis 后,我们的 Ruby on Rails 应用程序中的 Redis 代码每天都会出现几次“OpenSSL::SSL::SSLError: SSL_read: sslv3 alert bad record mac”错误。
任何想法为什么?
我相信您遇到了一个多处理问题,其中一个分叉的进程关闭了父进程的 Redis 连接。resque
我刚刚发现了一个导致与此问题相同的错误的错误。
它不能直接解决问题,但这就是 Heroku 支持不得不说的:
这个问题一直是一个难以诊断的问题,因为它的频率和不一致。我们有很多关于它发生的报告,涵盖目的地、应用程序语言、Heroku Dyno 配置以及许多其他细节。这个问题已经写出来了,工程师正在诊断,但是细节再次使我们几乎不可能这样做。
此外,我们没有管理传出连接的基础设施。我们确实有原始形式的网络使用信息(传输的字节数、数据包数量等),但与让 Heroku 路由器处理请求的传入连接不同,出站连接都是我们的基础设施提供商提供的“标准”连接。没有处理出站连接的特定于 Heroku 的基础设施。唯一感兴趣的项目是 Dynos 使用的虚拟接口,以及 Dyno 主机的网络配置,但同样没有什么特别之处。它使用基础设施平台提供主机通信所需的网络配置。
到目前为止,我自己和工程师都没有为这些问题提出简明的答案,鉴于它们的不一致,我们目前的建议是通过连接错误处理、根据需要记录和重试来更好地处理这些问题。
如果您有关于此错误发生的一致可重现方式的详细信息,它将对我们有很大帮助。
通过重试这些错误,我在 Ruby on Rails 应用程序中解决了这个问题。到目前为止,它似乎已经摆脱了错误。我们过去每天大约有 5 个,而在过去的一天——自从引入了这种解决方法——就没有了。
在config/initializers/redis_heroku_error_handling.rb
:
# Hack to retry errors on Heroku Redis: https://stackoverflow.com/questions/50228454/why-am-i-getting-opensslsslsslerror-ssl-read-sslv3-alert-bad-record-mac
raise "Ensure this hack still works!" if Redis::VERSION != "3.3.5"
module RedisHerokuErrorHandlingHack
def call(command)
attempts_left = 3
begin
super
rescue OpenSSL::SSL::SSLError => e
raise unless e.message.include?("SSL_read: sslv3 alert bad record mac")
attempts_left -= 1
raise unless attempts_left > 0
retry
end
end
end
class Redis::Client
prepend RedisHerokuErrorHandlingHack
end
在spec/initializers/redis_heroku_error_handling_spec.rb
:
require "rails_helper"
describe RedisHerokuErrorHandlingHack do
it "retries 'bad record mac' errors" do
exception = OpenSSL::SSL::SSLError.new("SSL_read: sslv3 alert bad record mac")
fail_then_maybe_succeed(exception: exception, fail_times: 2)
expect($redis.get("hello")).to eq("success response")
end
it "fails if a few retries didn't help" do
exception = OpenSSL::SSL::SSLError.new("SSL_read: sslv3 alert bad record mac")
fail_then_maybe_succeed(exception: exception, fail_times: 3)
expect { $redis.get("hello") }.to raise_error(/bad record mac/)
end
it "does not retry other errors" do
fail_then_maybe_succeed(exception: "Boom", fail_times: 2)
expect { $redis.get("hello") }.to raise_error("Boom")
end
private
def fail_then_maybe_succeed(exception:, fail_times:)
attempt_count = 0
allow_any_instance_of(Redis::Client).to receive(:process) do |*args|
attempt_count += 1
if attempt_count <= fail_times
raise exception
else
"success response"
end
end
end
end