This topic contains 3 replies, has 3 voices, and was last updated by  mark.brighton 4 years, 2 months ago.

Best practices for using the net.sftp module

  • I have a question about best practices for using the net.sftp module, but it probably applies to many/most of the other net modules. Is it better to call net.init{} at the beginning of the main.lua file (so that it is only executed on channel start), or inside of main()?

    There’s no explicit quit function in this module (or an open for that matter), so it’s hard to tell how Iguana is managing the connection(s). My first instinct was to put init{} at the top, erring on the side of not creating another connection every time main() polls, but I’ve discovered that the connection object can become invalid. Are there any negative effects of calling init{} every time main() runs?

    Posted 23 January 2013 – 02:47 PM
    I have a question about best practices for using the net.sftp module, but it probably applies to many/most of the other net modules. Is it better to call net.init{} at the beginning of the main.lua file (so that it is only executed on channel start), or inside of main()?

    There’s no explicit quit function in this module (or an open for that matter), so it’s hard to tell how Iguana is managing the connection(s). My first instinct was to put init{} at the top, erring on the side of not creating another connection every time main() polls, but I’ve discovered that the connection object can become invalid. Are there any negative effects of calling init{} every time main() runs?

    (Originally posted in the Linked In Forum)

    I think it caches the connections under the hood so it would be best practice to put it into the main function – we’ll have to double check.

    The “init” call does cache the connection so subsequent calls will just pull up the cached version of the connection data. The connection is not established until it needs to be. For example, FTP = net.ftp.init{server=’x.x.x.x’, etc…} does not invoke an FTP connection. The connection data is cached and when a subsequent ftp call is made it will then make the connection. Therefore, you could run the init once (outside of main) or for each message (inside of main), the former being slightly more performant but either one should work and the user should not care about where to put the init call.

    So how long does the connection persist? Will the net.sftp connection be closed and recerated as needed?

    I’m asking this because I’m seeing connection errors in the Iguana log. Without knowing how this works, I can tell what (if anything) I can do to handle them better.

You must be logged in to reply to this topic.