tags:

views:

55

answers:

2

I'm working on a Ruby web Application that uses the Net::FTP library. One part of it allows users to interact with an FTP site via AJAX. When the user does something, and AJAX call is made, and then Ruby reconnects to the FTP server, performs an action, and outputs information.

Every time the AJAX call is made, Ruby has to reconnect to the FTP server, and that's slow. Is there a way I could cache this FTP connection? I've tried caching in the session hash, but "We're sorry, but something went wrong" is displayed, and a TCP dump is outputted in my logs whenever I attempt to store it in the session hash. I haven't tried memcache yet.

Any suggestions?

+1  A: 

AJAX can't directly talk to FTP. It's designed for HTTP. That doesn't stop you from writing something that does cache the FTP server though. You probably should profile it to find out what's really slow. My guess is that the FTP access is just slow. Caching it may be a mixed blessing though. How do you know when the content of the ftp site changes?

Jay
I understand that AJAX does not talk to FTP; I'm using Rails controller and Ruby as a proxy to Javascript. It's just the TCP connection I want to cache. Hopefully the server is just slow like you said.
Chad Johnson
+1  A: 

What you are trying to do is possible, but far from trivial, and Rails doesn't offer any built-in support for it. In fact you will need to descend to the OS level to get this done, and if you have more than one physical server then it will get even more complicated.

First, you can't store a connection in the session. In fact you don't want to store any Ruby object in the session for many reasons, including but not limited to:

  • Some types of objects have trouble being marshalled/unmarshalled
  • Deploying could break stuff if the model changes and people have outdates stuff serialized in their session
  • If you are using the cookie session store then you only have 4k

So in general, you only ever want to put primitives like strings, numbers and booleans into the session.

Now as far as an FTP connection is concerned, this falls into the category of things that can't be serialized/unserialized reliably. The reason is because it's not just a Ruby object, but also has a socket open which is going to be closed as soon as the original object is garbage collected.

So, in order to keep a FTP connection persistent, it can't be stored in a controller instance variable because the controller instance is per-request. You could try to instantiate it it somewhere outside the controller instance, but that has the potential for memory leaks if you are not very careful to clean up the connections, and besides, if you have more than one app server instance then you would also need to find a way to guarantee that the user talks to the same app server instance on each request, or it wouldn't be able to find the hook. So all in all, keeping the session open in the Ruby process is a non-starter.

What you need to do is open the connection in a separate process that any of the ruby processes can talk to. There's really no established and standard way to do that, you'll have to roll your own. You could look into DRb to provide some of the primitives you will need.

dasil003
Great information. Clearly this is nontrivial and will take time, so I will have to put this on the back-burner for now. I'll come back to this later and look through DRb and rolling a custom solution a bit later. Thanks!
Chad Johnson