Method: ConnectionPool::TimedStack#pop

Defined in:
lib/connection_pool/timed_stack.rb

#pop(timeout: 0.5, exception: ConnectionPool::TimeoutError) ⇒ Object

Retrieves a connection from the stack. If a connection is available it is immediately returned. If no connection is available within the given timeout a ConnectionPool::TimeoutError is raised.

Other options may be used by subclasses that extend TimedStack.

Parameters:

  • options (Hash)

    a customizable set of options



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/connection_pool/timed_stack.rb', line 62

def pop(timeout: 0.5, exception: ConnectionPool::TimeoutError, **)
  deadline = current_time + timeout
  @mutex.synchronize do
    loop do
      raise ConnectionPool::PoolShuttingDownError if @shutdown_block
      if (conn = try_fetch_connection(**))
        return conn
      end

      connection = try_create(**)
      return connection if connection

      to_wait = deadline - current_time
      if to_wait <= 0
        if exception
          raise exception, "Waited #{timeout} sec, #{length}/#{@max} available"
        else
          return nil
        end
      end
      @resource.wait(@mutex, to_wait)
    end
  end
end