Sha256: f5f6737b05638291a857fc23c40d78dda6b7ad646bbff616b01dab00c01da599

Contents?: true

Size: 915 Bytes

Versions: 5

Compression:

Stored size: 915 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 = self.instance_eval(&conn) 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
      LazyEvaluatedPool.new(config, &(block || connection))
    end
    private
    def connection; raise 'Please, override connection method or supply block' end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
grape-gen-0.0.5 template/config/initializers/em-patches/lazy_evaluated_pool.rb
grape-gen-0.0.4 template/config/initializers/em-patches/lazy_evaluated_pool.rb
grape-gen-0.0.3 template/config/initializers/em-patches/lazy_evaluated_pool.rb
grape-gen-0.0.2 template/config/initializers/em-patches/lazy_evaluated_pool.rb
grape-gen-0.0.1 template/config/initializers/em-patches/lazy_evaluated_pool.rb