]> git.notmuchmail.org Git - sup/commitdiff
shared ssh connections if they're to the same username and host
authorwmorgan <wmorgan@5c8cc53c-5e98-4d25-b20a-d8db53a31250>
Tue, 2 Jan 2007 20:32:34 +0000 (20:32 +0000)
committerwmorgan <wmorgan@5c8cc53c-5e98-4d25-b20a-d8db53a31250>
Tue, 2 Jan 2007 20:32:34 +0000 (20:32 +0000)
git-svn-id: svn://rubyforge.org/var/svn/sup/trunk@139 5c8cc53c-5e98-4d25-b20a-d8db53a31250

lib/sup/mbox/ssh-file.rb

index 50a217265f0a7ba9d99d0768fe058af4f3010f86..8ceaaaf1718d57ff319fd5552c952229ef31698d 100644 (file)
@@ -82,6 +82,9 @@ class SSHFile
   REASONABLE_TRANSFER_SIZE = 1024 * 32
   SIZE_CHECK_INTERVAL = 60 * 1 # seconds
 
+  @@shells = {}
+  @@shells_mutex = Mutex.new
+
   def initialize host, fn, ssh_opts={}
     @buf = Buffer.new
     @host = host
@@ -95,6 +98,7 @@ class SSHFile
 
   def broken?; !@broken_msg.nil?; end
 
+  ## TODO: share this code with imap
   def say s
     @say_id = BufferManager.say s, @say_id if BufferManager.instantiated?
     Redwood::log s
@@ -106,20 +110,35 @@ class SSHFile
   private :say, :shutup
 
   def connect
-    return if @session
     raise SSHFileError, @broken_msg if broken?
-
-    say "Opening SSH connection to #{@host}..."
-
-    begin
-      #raise SSHFileError, "simulated SSH file error"
-      @session = Net::SSH.start @host, @ssh_opts
-      say "Starting SSH shell..."
-      @shell = @session.shell.sync
-      say "Checking for #@fn..."
-      raise Errno::ENOENT, @fn unless @shell.test("-e #@fn").status == 0
-    ensure
-      shutup
+    return if @shell
+
+    key = [@host, @ssh_opts[:username]]
+    @shell = 
+      @@shells_mutex.synchronize do
+      if @@shells.member? key
+        returning(@@shells[key]) do |shell|
+          say "Checking for #@fn..."
+          begin
+            raise Errno::ENOENT, @fn unless shell.test("-e #@fn").status == 0
+          ensure
+            shutup
+          end
+        end
+      else
+        say "Opening SSH connection to #{@host}..."
+        begin
+          #raise SSHFileError, "simulated SSH file error"
+          session = Net::SSH.start @host, @ssh_opts
+          say "Starting SSH shell..."
+          shell = session.shell.sync
+          say "Checking for #@fn..."
+          raise Errno::ENOENT, @fn unless shell.test("-e #@fn").status == 0
+          @@shells[key] = shell
+        ensure
+          shutup
+        end
+      end
     end
   end