Ruby 的“open_uri"是否在读取或失败后可靠地关闭套接字? [英] Does Ruby's 'open_uri' reliably close sockets after read or on fail?

查看:45
本文介绍了Ruby 的“open_uri"是否在读取或失败后可靠地关闭套接字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 open_uri 拉下一个 ftp 路径作为数据源,但突然发现我几乎连续530 对不起,允许的最大客户端数(95) 已经连接."

I have been using open_uri to pull down an ftp path as a data source for some time, but suddenly found that I'm getting nearly continual "530 Sorry, the maximum number of allowed clients (95) are already connected."

我不确定我的代码是否有问题,或者是其他人在访问服务器,不幸的是,我无法真正确定谁有错.

I am not sure if my code is faulty or if it is someone else who's accessing the server and unfortunately there's no way for me to really seemingly know for sure who's at fault.

基本上我正在阅读 FTP URI:

Essentially I am reading FTP URI's with:

  def self.read_uri(uri)
    begin
      uri = open(uri).read
      uri == "Error" ? nil : uri
    rescue OpenURI::HTTPError
      nil
    end
  end

我猜我需要在这里添加一些额外的错误处理代码...我想确保我采取一切预防措施关闭所有连接,这样我的连接就不是问题所在,但是我认为 open_uri + read 会采取这种预防措施,而不是使用 Net::FTP 方法.

I'm guessing that I need to add some additional error handling code in here... I want to be sure that I take every precaution to close down all connections so that my connections are not the problem in question, however I thought that open_uri + read would take this precaution vs using the Net::FTP methods.

最重要的是,我必须 100% 确定这些连接正在关闭,而且我没有以某种方式放置一堆打开的连接.

The bottom line is I've got to be 100% sure that these connections are being closed and I don't somehow have a bunch open connections laying around.

有人可以建议正确使用 read_uri 来拉入 ftp 并保证它关闭连接吗?或者我应该将逻辑转移到 Net::FTP,如果 open_uri 不够健壮,它可以更好地控制情况?

Can someone please advise as to correctly using read_uri to pull in ftp with a guarantee that it's closing the connection? Or should I shift the logic over to Net::FTP which could yield more control over the situation if open_uri is not robust enough?

如果我确实需要使用 Net::FTP 方法,是否有一种我应该熟悉的读取方法与将其拉到 tmp 位置然后读取它(因为我更愿意保留它)如果可能,在缓冲区与 fs 中)?

If I do need to use the Net::FTP methods instead, is there a read method that I should be familiar with vs pulling it down to a tmp location and then reading it (as I'd much prefer to keep it in a buffer vs the fs if possible)?

推荐答案

我怀疑您没有关闭句柄.OpenURI 的文档从以下评论开始:

I suspect you are not closing the handles. OpenURI's docs start with this comment:

It is possible to open http/https/ftp URL as usual like opening a file:

open("http://www.ruby-lang.org/") {|f|
  f.each_line {|line| p line}
}

我查看了源代码,如果您传递一个块,open_uri 方法确实会关闭流,因此,调整上面的示例以适合您的代码:

I looked at the source and the open_uri method does close the stream if you pass a block, so, tweaking the above example to fit your code:

uri = ''
open("http://www.ruby-lang.org/") {|f|
  uri = f.read
}

应该让你接近你想要的.

Should get you close to what you want.

这是处理异常的一种方法:

Here's one way to handle exceptions:

# The list of URLs to pass in to check if one times out or is refused.
urls = %w[
  http://www.ruby-lang.org/
  http://www2.ruby-lang.org/
]

# the method
def self.read_uri(urls)

  content = ''

  open(urls.shift) { |f| content = f.read }
  content == "Error" ? nil : content

  rescue OpenURI::HTTPError
    retry if (urls.any?)
    nil
end

这篇关于Ruby 的“open_uri"是否在读取或失败后可靠地关闭套接字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆