A way to access common FTP connection resource poo

2019-08-16 10:46发布

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.

2条回答
Luminary・发光体
2楼-- · 2019-08-16 11:20

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.

查看更多
smile是对你的礼貌
3楼-- · 2019-08-16 11:31

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.

查看更多
登录 后发表回答