ruby net/http 打开连接很慢 [英] ruby net/http opening connection very slow

查看:43
本文介绍了ruby net/http 打开连接很慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在开发中使用 net/http 时,它非常慢.我已经安装了 net-http-spy gem 来获取有关每个请求的一些信息,我发现打开连接"部分花费的时间最多(超过 10 秒).此外,它不会保持连接处于活动状态,因此需要在每次请求时重新打开它.

When I'm working with net/http in development it is extremely slow. I've installed net-http-spy gem to get some info about each request and I've found out that "opening connection" part takes the most time (more than 10 seconds). Further more, it doesn't keep connections alive, so it needs to reopen it on every request.

opening connection to maps.google.com...
# ~10 seconds
opened

有什么办法可以通过设置一些默认值来提高 net/http 库的性能?我不想要一个请求特定的修复,而是一个可以在全球范围内解决问题的东西.我在当前项目中使用 geokit gem,除了知道它使用的是 net/http 之外,我无法更改它内部处理请求的方式,并且它运行良好生产(Heroku),但开发速度极慢.

Is there any way I could somehow improve the performance of net/http library by settings some of its defaults? I don't want a request specific fix, but something that would fix the issue globally. I'm using geokit gem in curent project and I cannot change the way it handles request internally, other than knowing it is using net/http, and it is working great on production (Heroku) but extremely slow in development.

我在 Mac OS X Lion 上使用 ruby​​ 1.9.2-p290.Ruby 使用 rbenv 进行安装和管理.

I'm using ruby 1.9.2-p290 on Mac OS X Lion. Ruby is installed and managed with rbenv.

这是示例代码:

require 'net/http'
require 'uri'

time = Time.now.to_i
uri = URI.parse("http://maps.google.com/maps/geo?q=Chicago&output=xml&oe=utf-8")

Net::HTTP.get(uri)
puts (Time.now.to_i - time)

Net::HTTP.get(uri)
puts (Time.now.to_i - time)

我在那里放什么 URL 并不重要.每个请求总是需要 10 秒.

It doesn't really matter what URL I put in there. It always take exactly 10 seconds for each request..

推荐答案

这是 DNS 服务器的问题.切换到谷歌公共 DNS (8.8.8.8, 8.8.4.4).谢谢@Ineu.

It was an issue with DNS server. Switched to google public DNS (8.8.8.8, 8.8.4.4). Thanks @Ineu.

这篇关于ruby net/http 打开连接很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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