我的网站需要读取速度慢的网站,如何提高性能

发布于 2024-09-09 01:54:07 字数 301 浏览 5 评论 0原文

我正在编写一个带有 Rails 的网站,它可以让访问者输入一些域并检查它们是否已注册。

当用户单击“提交”按钮时,我的网站将尝试将一些数据发布到另一个网站,并读回结果。但是那个网站对我来说很慢,每个请求需要2或3秒。所以我很担心表演。

例如,如果我的网络服务器最多允许 100 个进程,那么只有 30 或 40 个用户可以同时访问我的网站。这是不可接受的,有什么办法可以提高性能吗?

PS: 起初,我想使用ajax读取该网站,但由于“跨域”问题,它不起作用。所以我必须使用这个“ajax代理”解决方案。

I'm writing a web site with rails, which can let visitors inputing some domains and check if they had been regiestered.

When user clicked "Submit" button, my web site will try to post some data to another web site, and read the result back. But that website is slow for me, each request need 2 or 3 seconds. So I'm worried about the performance.

For example, if my web server allows 100 processes at most, that there are only 30 or 40 users can visit my website at the same time. This is not acceptable, is there any way to improve the performance?

PS:
At first, I want to use ajax reading that web site, but because of the "cross-domain" problem, it doesn't work. So I have to use this "ajax proxy" solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

提笔落墨 2024-09-16 01:54:08

这需要更多工作,但您可以使用 DelayedJob 之类的东西来处理对其他站点的请求背景。

DelayedJob 创建单独的工作进程,这些进程查看 Jobs 表以查找要做的事情。当用户单击“提交”时,就会创建这样一个作业,并开始在其中一个工作线程中运行。这减轻了 Rails 工作人员的负担,并使您的网站保持活力。

但是,您必须在作业运行时在浏览器中创建某种轮询机制。也许使用刷新或一些简单的 AJAX。这样,访问者可以看到诸如“请稍等...”之类的消息,过了一会儿,就会看到实际结果。

It's a bit more work, but you can use something like DelayedJob to process the requests to the other site in the background.

DelayedJob creates separate worker processes that look at a jobs table for stuff to do. When the user clicks submit, such a job is created, and starts running in one of those workers. This off-loads your Rails workers, and keeps your website snappy.

However, you will have to create some sort of polling mechanism in the browser while the job is running. Perhaps using a refresh or some simple AJAX. That way, the visitor could see a message such as “One moment, please...”, and after a while, the actual results.

零度℉ 2024-09-16 01:54:08

您可以使用 HTTP HEAD 请求,而不是向网站发布一些数据,它(我相信)应该只返回该 URL 的标头信息。

我通过谷歌搜索找到了这段代码:

require "net/http"
req = Net::HTTP.new('google.com', 80)
p req.request_head('/')

这可能比 POST 请求更快,而且您不必等待接收该资源的全部内容。您应该能够根据响应代码确定该站点是否正在使用。

Rather than posting some data to the websites, you could use an HTTP HEAD request, which (I believe) should return only the header information for that URL.

I found this code by googling around a bit:

require "net/http"
req = Net::HTTP.new('google.com', 80)
p req.request_head('/')

This will probably be faster than a POST request, and you won't have to wait to receive the entire contents of that resource. You should be able to determine whether the site is in use based on the response code.

梦里梦着梦中梦 2024-09-16 01:54:08

尝试使用typheus而不是AJAX来获取正文。您可以使用tyhoeus POST 该站点的域名进行检查,并可以解析获取的响应。与其他解决方案相比,它的速度非常快。我从 github 存储库的 wiki 页面中摘录的片段 http://github.com/pauldix/typhoeus 表明您可以并行运行请求(考虑到 ajax 请求需要 1 到 2 秒,这可能是您想要的!!):

hydra = Typhoeus::Hydra.new

first_request = Typhoeus::Request.new("http://localhost:3000/posts/1.json")
first_request.on_complete do |response|
  post = JSON.parse(response.body)
  third_request = Typhoeus::Request.new(post.links.first) # get the first url in the post
  third_request.on_complete do |response|
    # do something with that
  end
  hydra.queue third_request
  return post
end
second_request = Typhoeus::Request.new("http://localhost:3000/users/1.json")
second_request.on_complete do |response|
  JSON.parse(response.body)
end
hydra.queue first_request
hydra.queue second_request
hydra.run # this is a blocking call that returns once all requests are complete

first_request.handled_response # the value returned from the on_complete block
second_request.handled_response # the value returned from the on_complete block (parsed JSON)

还有 Typhoeus + dependent_job = AWESOME!

Try using typhoeus rather than AJAX to get the body. You can POST the domain names for that site to check using typhoeus and can parse the response fetched. Its extremely fast compared to other solutions. A snippet that i ripped from the wiki page from the github repo http://github.com/pauldix/typhoeus shows that you can run requests in parallel (Which is probably what you want considering that it takes 1 to 2 seconds for an ajax request!!) :

hydra = Typhoeus::Hydra.new

first_request = Typhoeus::Request.new("http://localhost:3000/posts/1.json")
first_request.on_complete do |response|
  post = JSON.parse(response.body)
  third_request = Typhoeus::Request.new(post.links.first) # get the first url in the post
  third_request.on_complete do |response|
    # do something with that
  end
  hydra.queue third_request
  return post
end
second_request = Typhoeus::Request.new("http://localhost:3000/users/1.json")
second_request.on_complete do |response|
  JSON.parse(response.body)
end
hydra.queue first_request
hydra.queue second_request
hydra.run # this is a blocking call that returns once all requests are complete

first_request.handled_response # the value returned from the on_complete block
second_request.handled_response # the value returned from the on_complete block (parsed JSON)

Also Typhoeus + delayed_job = AWESOME!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文