简体   繁体   English

加快ruby ssh握手

[英]Speed up ruby ssh handshake

I'm in ruby on rails and using the net-ssh gem to ssh into another machine, run some scripts, get the output of the scripts back into rails, then serve that output on my site. 我身处红宝石的轨道上,并使用net-ssh gem将ssh推入另一台计算机中,运行一些脚本,将脚本的输出返回到Rails中,然后在我的网站上提供该输出。 This will need to happen a lot, in fact it is the core of my site. 这将需要发生很多,实际上这是我网站的核心。

The script itself takes very little time, but the time to response ranges between instant to 15 seconds. 脚本本身花费的时间很少,但是响应时间介于即时到15秒之间。 The SSH handshake is taking variable time. SSH握手耗时不定。

Here's my ruby code to do the ssh: 这是我做ssh的ruby代码:

res = []
Net::SSH.start( host, user, :password => pass ) do |ssh|
  # execute container
  ssh.exec("run script") do |ch, stream, data|
      res.push(data)
  end
end
res = res.join("")
render text: res

Any idea for how to make this super fast? 有什么想法可以使这个超级快吗?

I'm wondering if I should be keeping an ssh connection open at all times and add new threads every time I need to run the script (which will be a lot). 我想知道是否应该一直保持ssh连接打开,并在每次需要运行脚本时添加新线程(这会很多)。 I've seen people talk about ControlMaster, but not sure how to make that work. 我见过人们谈论ControlMaster,但不确定如何使它正常工作。

If this is the core of your site, you may not want to rely on SSH and exec. 如果这是您网站的核心,则您可能不想依靠SSH和exec。

The alternative is to use Ruby/Rails instead of SSH on the external host and push the jobs directly to Ruby via a more standard web API or a messaging system like zeromq. 另一种选择是在外部主机上使用Ruby / Rails代替SSH,然后通过更标准的Web API或消息系统(如zeromq)将作业直接推送到Ruby。 Creating a consistent Service-oriented architecture . 创建一致的面向服务的体系结构

Otherwise, you should maintain the SSH connection from Ruby as a long running task in a background daemon then push jobs to it via IPC (like zeromq or http or simply your database) then wait for the response. 否则,您应该在后台守护程序中将来自Ruby的SSH连接作为长期运行的任务进行维护,然后通过IPC(例如zeromq或http或仅是您的数据库)将作业推送到它,然后等待响应。 Then you only have the overhead of starting up your scripts on the remote host for each request. 这样,您仅需为每个请求在远程主机上启动脚本的开销。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM