Sha256: 4483498781da60ddb2f2035482521613581d8a0bce7d70fd438864c96e8ca605

Contents?: true

Size: 890 Bytes

Versions: 1

Compression:

Stored size: 890 Bytes

Contents

require 'em-synchrony/connection_pool'

class LazyEvaluatedPool < EventMachine::Synchrony::ConnectionPool
  def initialize(opts, &block)
    @reserved  = {}   # map of in-progress connections
    @available = []   # pool of free connections
    @pending   = []   # pending reservations (FIFO)
    @config = opts
    opts[:size].times do
      @available.push(block) if block_given?
    end
  end

  private
  def acquire(fiber)
    if conn = @available.pop
      conn = conn.call(@config) if conn.respond_to?(:call)
      @reserved[fiber.object_id] = conn
      conn
    else
      Fiber.yield @pending.push fiber
      acquire(fiber)
    end
  end

  class << self
    def pool_with_config(config, &block)
      config[:size] ||= 10
      new(config, &(block || connection))
    end
    private
    def connection; raise 'Please, override connection method or supply block' end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
grape-gen-0.0.6 template/config/initializers/em-patches/lazy_evaluated_pool.rb