| 153 | autocommit: bool = False |
| 154 | |
| 155 | def connect(self) -> psycopg.Connection: |
| 156 | conn = psycopg.connect( |
| 157 | host=self.host, |
| 158 | port=self.port, |
| 159 | user=self.user, |
| 160 | password=self.password, |
| 161 | dbname=self.database, |
| 162 | sslmode="require" if self.ssl else None, |
| 163 | ) |
| 164 | # Set SO_LINGER(1, 0) so close() sends RST instead of FIN, bypassing |
| 165 | # TIME_WAIT. Prevents exhausting the ~28k ephemeral port range under |
| 166 | # high connection churn (e.g. benchmarks doing rapid connect/disconnect). |
| 167 | self._set_linger(conn) |
| 168 | if self.autocommit: |
| 169 | conn.autocommit = True |
| 170 | if self.cluster: |
| 171 | with conn.cursor() as cur: |
| 172 | cur.execute(f"SET cluster = {self.cluster}".encode()) |
| 173 | return conn |
| 174 | |
| 175 | @staticmethod |
| 176 | def _set_linger(conn: psycopg.Connection) -> None: |