A way to access common FTP connection resource pool in Ruby across AJAX calls?

末鹿安然 提交于 2019-12-13 07:36:07

问题


I have a web application which establishes many FTP or SFTP connections with outside servers. Its interface uses AJAX, and via AJAX I get file listings on remote FTP servers and return those to the client browser.

Each time I run an AJAX call, I have to reconnect to the remote server and reauthenticate. This takes a ton of extra time.

Is there a way I can somehow store FTP connection resource objects in some common memory pool and re-access to the connection resource objects with future AJAX calls? I tried Memcached, but it looks like it's not possible to store connection resources there. Maybe I could store them in a thread and somehow access them there? Any other ideas?

I could always have a daemon manage connections and act as a proxy, but that feels overkill.


回答1:


You can open a connection for each worker/app process you have. For example, with passenger:

if defined?(PhusionPassenger)
  PhusionPassenger.on_event(:starting_worker_process) do |forked|
    if forked
      # connect to ftp server
    end
  end
end

With Rails this would go into environment.rb.

That said, I'm not sure if this is a great idea though since I don't use ftp much.




回答2:


I ended up making this work using global variables (eg. $my_global). I have a ConnectionPooler singleton class which manages connections stored in a hash. Easy as pie.



来源:https://stackoverflow.com/questions/4471680/a-way-to-access-common-ftp-connection-resource-pool-in-ruby-across-ajax-calls

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