a
    !f	                     @   s\   d Z ddlZddlZddlZddlmZ ddlmZ dZG dd deZ	G dd	 d	e
ZdS )
z,Google Cloud Bigtable HappyBase pool module.    N)
Connection)_get_instance   c                   @   s   e Zd ZdZdS )NoConnectionsAvailablezException raised when no connections are available.

    This happens if a timeout was specified when obtaining a connection,
    and no connection became available within the specified timeout.
    N)__name__
__module____qualname____doc__ r
   r
   _/var/www/html/python-backend/venv/lib/python3.9/site-packages/gcloud/bigtable/happybase/pool.pyr      s   r   c                   @   s2   e Zd ZdZdd Zd	ddZejd
ddZdS )ConnectionPoola  Thread-safe connection pool.

    .. note::

        All keyword arguments are passed unmodified to the
        :class:`Connection <.happybase.connection.Connection>` constructor
        **except** for ``autoconnect``. This is because the ``open`` /
        ``closed`` status of a connection is managed by the pool. In addition,
        if ``instance`` is not passed, the default / inferred instance is
        determined by the pool and then passed to each
        :class:`Connection <.happybase.connection.Connection>` that is created.

    :type size: int
    :param size: The maximum number of concurrently open connections.

    :type kwargs: dict
    :param kwargs: Keyword arguments passed to
                   :class:`Connection <.happybase.Connection>`
                   constructor.

    :raises: :class:`TypeError <exceptions.TypeError>` if ``size``
             is non an integer.
             :class:`ValueError <exceptions.ValueError>` if ``size``
             is not positive.
    c                 K   s   t |tjstd|tk r$tdt | _tj	j
j|d| _t | _|}d|d< d|vrrt|dd|d< tj	|D ]}tf i |}| j| q~d S )	Nz Pool size arg must be an integerzPool size must be positive)maxsizeFZautoconnectinstancetimeout)r   )
isinstancesixinteger_types	TypeError_MIN_POOL_SIZE
ValueError	threadingLock_lockmovesqueue	LifoQueue_queuelocal_thread_connectionsr   getranger   put)selfsizekwargsZconnection_kwargs_
connectionr
   r
   r   __init__A   s     


zConnectionPool.__init__Nc                 C   s8   z| j jd|dW S  tjjjy2   tdY n0 dS )a  Acquire a connection from the pool.

        :type timeout: int
        :param timeout: (Optional) Time (in seconds) to wait for a connection
                        to open.

        :rtype: :class:`Connection <.happybase.Connection>`
        :returns: An active connection from the queue stored on the pool.
        :raises: :class:`NoConnectionsAvailable` if ``Queue.get`` fails
                 before the ``timeout`` (only if a timeout is specified).
        T)blockr   z:No connection available from pool within specified timeoutN)r   r   r   r   r   Emptyr   )r"   r   r
   r
   r   _acquire_connectionV   s    z"ConnectionPool._acquire_connectionc                 c   s~   t | jdd}d}|du rVd}| |}| j || j_W d   n1 sL0    Y  |  |V  |rz| j`| j| dS )a  Obtain a connection from the pool.

        Must be used as a context manager, for example::

            with pool.connection() as connection:
                pass  # do something with the connection

        If ``timeout`` is omitted, this method waits forever for a connection
        to become available from the local queue.

        Yields an active :class:`Connection <.happybase.connection.Connection>`
        from the pool.

        :type timeout: int
        :param timeout: (Optional) Time (in seconds) to wait for a connection
                        to open.

        :raises: :class:`NoConnectionsAvailable` if no connection can be
                 retrieved from the pool before the ``timeout`` (only if
                 a timeout is specified).
        currentNFT)getattrr   r*   r   r+   openr   r!   )r"   r   r&   Zretrieved_new_cnxnr
   r
   r   r&   h   s    

&zConnectionPool.connection)N)N)	r   r   r   r	   r'   r*   
contextlibcontextmanagerr&   r
   r
   r
   r   r   '   s
   
r   )r	   r.   r   r   Z$gcloud.bigtable.happybase.connectionr   r   r   RuntimeErrorr   objectr   r
   r
   r
   r   <module>   s   