GithubHelp home page GithubHelp logo

Comments (2)

leehambley avatar leehambley commented on June 16, 2024

I benchmarked the opening of SSH channels, here were the results, the connections for each were opened 1_000 times.

Rehearsal -------------------------------------------------
with ssh key    0.500000   0.090000   0.590000 (  7.526066)
with password   0.470000   0.100000   0.570000 (  7.342561)
---------------------------------------- total: 1.160000sec

                    user     system      total        real
with ssh key    0.510000   0.090000   0.600000 (  7.381403)
with password   0.500000   0.100000   0.600000 (  7.513771)

That tells me that it's fast enough, call it 0.007s (7ms) to open a connection, under good conditions, and it should be fast enough for a first implementation.

So saying that, let's say an implementation of on() might look like this:

def on(hosts, options = {}, &block)
  pool = ConnectionPool.new(hosts)
  pool.execute(&block, options)
  pool.terminate
end

from sshkit.

leehambley avatar leehambley commented on June 16, 2024

And further to that, a naïve pseudo implementation of ConnectionPool:

require 'net/ssh'

class ConnectionPool

  Error = Class.new(RuntimeError)

  attr_reader :members, :hosts

  def initialize(hosts)
    @hosts   = hosts
    @members = {}
  end

  def perform(&block)
    begin
      # Something here needs to support
      # parallel/sequential/limit/etc
      members.each do |host, session|
        yield host
      end
    rescue # Suitable Exception list here
      raise ConnectionPool::Error
    ensure
      terminate
    end
  end

  def terminate
    members.map(&:close)
  end

  private

    def open_connections
      begin
        hosts.each do
          members[host] = Net::SSH::Session.new(host)
        end
      rescue Net::SSH::Authentication::AgentError,
             Net::SSH::Authentication::KeyManagerError,
             Net::SSH::AuthenticationFailed,
             Net::SSH::ConnectionTimeout,
             Net::SSH::Exception 
             # Etcetera
        terminate
        raise ConnectionPool::Error            
      end
    end

end

With the beginnings of this in mind, I see the following problems:

  1. I'm starting to dislike the name "ConnectionPool", as we're having stubbed/test backends, we should be careful to come up with a useful name that fits all the paradigms we care about. (HostPool?)
  2. Further to the first point, It's not just a pool in my naïve implementation, it's also responsible for doing some work, this is a limitation of my spike in the Github issue, and something that we could solve by renaming/redesigning slightly (and I think we should)
  3. I don't see a nice way to handle the parallel vs. sequential with(±out) limits here, again, splitting this functionality into two classes might be wise.
  4. Maybe we could have something that checks-out connections from the pool, this could block until a connection is ready (pool would handle :limit) but the something would handle parallel vs. sequential. This something would then be responsible for threading, and/or forking. This way the parallel/sequential option is separated from the handling of the connections.

from sshkit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.