def METHOD_NAME(builder, size, timeout=None):
    """Create a pool that imposes a limit on the number of stored
    instances.
    Args:
        builder: a function to build an instance.
        size: the size of the pool.
        timeout(Optional[float]): the seconds to wait before raising
            a ``queue.Empty`` exception if no instances are available
            within that time.
    Raises:
        If ``timeout`` is defined but the request is taking longer
        than the specified time, the context manager will raise
        a ``queue.Empty`` exception.
    Returns:
        A context manager that can be used with the ``with``
        statement.
    """
    lock = threading.Lock()
    local_pool = queue.Queue()
    current_size = 0
    @contextlib.contextmanager
    def pooled():
        nonlocal current_size
        instance = None
        # If we still have free slots, then we have room to create new
        # instances.
        if current_size < size:
            with lock:
                # We need to check again if we have slots available, since
                # the situation might be different after acquiring the lock
                if current_size < size:
                    current_size += 1
                    instance = builder()
        # Watchout: current_size can be equal to size if the previous part of
        # the function has been executed, that's why we need to check if the
        # instance is None.
        if instance is None:
            instance = local_pool.get(timeout=timeout)
        yield instance
        local_pool.put(instance)
    return pooled