Errno::ECONNRESET: Connection reset by peer in Rails using rest-client

一个人想着一个人 提交于 2019-11-28 08:48:20

问题


We have a Ruby on Rails application and this has a "search" functionality (search for some company). From browser user key-in some name and hit search and this search make an rest api call to outside system and get us some search results.

We are using "rest-client" (for Ruby on Rails).

I noticed this seems to work for few hours and suddenly my search seems to be broken all of a sudden and I can see in my log I get:

Errno::ECONNRESET: Connection reset by peer

We tried to investigate this issue by looking in to logs and we dont see any logs.

If we need to make this search work again we need to restart the passenger and then it works immediately. This is happening only in production environment. I tested in staging it seems to work well.

Questions:

  1. What could be causing this "reset issue"
  2. Why on my prod passenger reset it starts to work again.
  3. We use reset-client should be write a code to manually close connection when this exception happens.
  4. Any issue in firewall could causing this?
  5. Is there any code I can place in the exception to restart this connection so the next call is success.

Code:

def call
   resp_data = RestClient.get(@request_url, @header)
   rescue => error
     puts 'Exception: ' error.message
end

回答1:


Try to the following

resp_data = RestClient::Request.new(
    method: :get,
    url: @request_url, #=> https://api.example.com/auth2/endpoint
    :headers => {
        :Authorization => @header, #=> "Bearer access_token",
    }
)

rescue => error
    puts 'Exception: ' error.message



回答2:


It's very wired. I met the same problem.

my script is shown as below: ( works great in my local machine, works greate in remote server, until someday the disk was full and this scripts dead, saying: Errno::ECONNRESET: Connection reset by peer)

ENV['RAILS_ENV'] = ARGV.first || ENV['RAILS_ENV'] || 'production'
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require 'rails'
require 'rubygems'

require 'rest-client'

url = 'https://api-cn.faceplusplus.com/cardpp/v1/ocridcard'
id_card = File.expand_path(File.dirname(__FILE__) + "/id_card.jpg")

puts "== id_card: #{id_card}"
response = RestClient.post url,
  {
    api_key: 'Td_XijYBZUMp',
    api_secret: 'ihehEuWYwOWM',
    image_file: File.open(id_card, 'rb')
  }

puts "==response: "
puts response.inspect

my environment: ruby 2.5.0 , ubuntu server 16, with 8 core CPU, 50G ram

this problem happens when I found my hard disk was used 100%. no space left.

However, once I freed enough disk space, this problem exists.

after I restart my server, this problem exists.

when I run this script under rails, this problem exists.

However, when I run this script stand alone, it works fine.

so , finally, I turned to "curl" command. works great!

the working CURL script looks like:

$ curl -X POST "https://api-cn.faceplusplus.com/cardpp/v1/ocridcard" \ 
-F "api_key=Td_XijYBCOYh-Rf_kCMj" \
-F "api_secret=iheWYoQcbCPM9n2VS" \
-F "image_file=@scripts/id_card.jpg


来源:https://stackoverflow.com/questions/49567486/errnoeconnreset-connection-reset-by-peer-in-rails-using-rest-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!