问题
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