|Syntactic Confectionery Delight|
Re^5: REST API with SFTP Poolingby salva (Canon)
|on Nov 16, 2020 at 16:05 UTC||Need Help??|
The way to pass extra options to the underlying ssh process from Net::OpenSSH is using the constructor master_opts argument.
The way to share the SSH connection is to pass around the path to the multiplexing socket (you can retrieve it calling method get_ctl_path).
You can also specify where you want to create the multiplexing socket explicitly (instead of letting the module pick a place for you) with the constructor argument ctl_path. Net::OpenSSH, by default, enforces some security policies for that path that you should obey.
So, for every connection you want to keep open (in your case, four connections every one using a different account), you need a process that establishes the connection and runs the dummy command from time to time. For instance:
Then from the process where you want to do the SFTP operation:
Most servers limit the number of channels open simultaneously in a single SSH session. For instance, it is 10 by default for OpenSSH. If you expect very many request coming in parallel, that could be a source of problems.
Finally note that allowing direct connections between a web server and other services has some security implications. If the front end becomes compromised, intruders would be able to access those backend services and the data there freely. Because of that, sometimes using an intermediate layer as the queue mechanism proposed in my OP could still be the most sensible thing to do.
Update: Both Net::OpenSSH and Net::SFTP::Foreign allow you to set timeouts for connecting and/or running commands. You should adjust those, to ensure that if any connection becomes stalled, it is discarded and established again, so that services recover promptly.