MCPcopy
hub / github.com/psycopg/psycopg / _add_connection

Method _add_connection

psycopg_pool/psycopg_pool/pool.py:656–706  ·  view source on GitHub ↗

Try to connect and add the connection to the pool. If failed, reschedule a new attempt in the future for a few times, then give up, decrease the pool connections number and call `self.reconnect_failed()`.

(
        self, attempt: AttemptWithBackoff | None, growing: bool = False
    )

Source from the content-addressed store, hash-verified

654 return self.kwargs
655
656 def _add_connection(
657 self, attempt: AttemptWithBackoff | None, growing: bool = False
658 ) -> None:
659 """Try to connect and add the connection to the pool.
660
661 If failed, reschedule a new attempt in the future for a few times, then
662 give up, decrease the pool connections number and call
663 `self.reconnect_failed()`.
664
665 """
666 now = monotonic()
667 if not attempt:
668 attempt = AttemptWithBackoff(timeout=self.reconnect_timeout)
669
670 try:
671 conn = self._connect()
672 except CLIENT_EXCEPTIONS as ex:
673 logger.warning("error connecting in %r: %s", self.name, ex)
674 if attempt.time_to_give_up(now):
675 logger.warning(
676 "reconnection attempt in pool %r failed after %s sec",
677 self.name,
678 self.reconnect_timeout,
679 )
680 with self._lock:
681 self._nconns -= 1
682 # If we have given up with a growing attempt, allow a new one.
683 if growing and self._growing:
684 self._growing = False
685 self.reconnect_failed()
686 else:
687 attempt.update_delay(now)
688 self.schedule_task(
689 AddConnection(self, attempt, growing=growing), attempt.delay
690 )
691 return
692
693 logger.info("adding new connection to the pool")
694 self._add_to_pool(conn)
695 if growing:
696 with self._lock:
697 # Keep on growing if the pool is not full yet, or if there are
698 # clients waiting and the pool can extend.
699 if self._nconns < self._min_size or (
700 self._nconns < self._max_size and self._waiting
701 ):
702 self._nconns += 1
703 logger.info("growing pool %r to %s", self.name, self._nconns)
704 self.run_task(AddConnection(self, growing=True))
705 else:
706 self._growing = False
707
708 def _return_connection(self, conn: CT, from_getconn: bool) -> None:
709 """

Callers 1

_runMethod · 0.45

Calls 10

_connectMethod · 0.95
time_to_give_upMethod · 0.95
reconnect_failedMethod · 0.95
update_delayMethod · 0.95
schedule_taskMethod · 0.95
_add_to_poolMethod · 0.95
run_taskMethod · 0.95
AttemptWithBackoffClass · 0.85
AddConnectionClass · 0.70
infoMethod · 0.45

Tested by

no test coverage detected