class Mist::Pool

Public Class Methods

get(servers) click to toggle source
# File lib/mist/pool.rb, line 18
def self.get(servers)
  @@pool ||= nil
  if @@pool.nil?
    @@pool = Pool.new(Mutex.new)
    begin
      servers.each do |server|
        @@pool.add(server)
      end
    rescue StandardError => ex
      Mist.logger.error "couldn't load config file: #{ex}"
    end
  end
  @@pool
end
new(mutex) click to toggle source
# File lib/mist/pool.rb, line 33
def initialize(mutex)
  @mutex = mutex

  @available = []
  @busy = []
end

Public Instance Methods

acquire() click to toggle source
# File lib/mist/pool.rb, line 53
def acquire
  server = nil
  # Get the first available client; loop until one becomes available
  loop do
    @mutex.synchronize do
      server = @available.shuffle.pop unless @available.empty?
      @busy.push server unless server.nil?
    end
    break if server
    sleep 1
  end

  return server
end
add(server) click to toggle source
# File lib/mist/pool.rb, line 40
def add(server)
  @mutex.synchronize do
    @available << server
  end
end
release(server) click to toggle source
# File lib/mist/pool.rb, line 68
def release(server)
  # Put the server back in the available list; if the server was removed
  # while we were using it, don't put it back.
  @mutex.synchronize do
    @available.push server if @busy.include? server
    @busy.delete server if @busy.include? server
  end
end
remove(server) click to toggle source
# File lib/mist/pool.rb, line 46
def remove(server)
  @mutex.synchronize do
    @available.delete server if @available.include? server
    @busy.delete server if @busy.include? server
  end
end