Errno :: ETIMEDOUT:连接超时 - 连接(2)

Joh*_*ohn 0 ruby-on-rails prawn amazon-s3 paperclip ruby-on-rails-3

在生产中,我偶尔会收到以下错误:

Errno::ETIMEDOUT: Connection timed out - connect(2)
Run Code Online (Sandbox Code Playgroud)

当我使用包含由paperclip/aws-sdk上传到s3的图像的prawn gem生成PDF时,似乎只会发生这种情况.这可能每周只发生几次,每天使用数百次(没有问题),但是当它失败时会导致500错误.

跟踪是:

/opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/net/http.rb:560:in
`initialize'
/opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/net/http.rb:560:in
`open'
/opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/net/http.rb:560:in
`connect'
/opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/timeout.rb:53:in
`timeout'
/opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/timeout.rb:101:in
`timeout'
/opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/net/http.rb:560:in
`connect'
/opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/net/http.rb:553:in
`do_start'
/opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/net/http.rb:542:in
`start'
/opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/open-uri.rb:242:in
`open_http'
/opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/open-uri.rb:616:in
`buffer_open'
/opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/open-uri.rb:164:in
`open_loop'
/opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/open-uri.rb:162:in
`catch'
/opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/open-uri.rb:162:in
`open_loop'
/opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/open-uri.rb:132:in
`open_uri'
/opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/open-uri.rb:518:in
`open'
/opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/open-uri.rb:30:in
`open'
....rb:57:in `render_image_to_pdf'
Run Code Online (Sandbox Code Playgroud)

第57行:

pdf.image open(image.expiring_url(30.minutes, :full)), :width => 300, :position => 20
Run Code Online (Sandbox Code Playgroud)

建立:

Rails 3.0.10
Ruby 1.8.7EE
Prawn  0.11.1
AWS-SDK 1.3.3
Paperclip 2.5.2
Run Code Online (Sandbox Code Playgroud)

我该怎么做才能防止此错误?

edk*_*750 8

一种选择是捕获异常并再试一次:

begin
  pdf.image open(image.expiring_url(30.minutes, :full)), :width => 300, :position => 20
rescue Errno::ETIMEDOUT
  # try one more time, or use retry with a counter to attempt a limited number of times
  pdf.image open(image.expiring_url(30.minutes, :full)), :width => 300, :position => 20
end
Run Code Online (Sandbox Code Playgroud)

您可能还需要进行一些诊断(记录或生成Airbrake)以通知您,以便您可以查看故障是否存在某种模式.