* connectDBComplete * * Block and complete a connection. * * Returns 1 on success, 0 on failure. */
| 2183 | * Returns 1 on success, 0 on failure. |
| 2184 | */ |
| 2185 | static int |
| 2186 | connectDBComplete(PGconn *conn) |
| 2187 | { |
| 2188 | PostgresPollingStatusType flag = PGRES_POLLING_WRITING; |
| 2189 | time_t finish_time = ((time_t) -1); |
| 2190 | int timeout = 0; |
| 2191 | int last_whichhost = -2; /* certainly different from whichhost */ |
| 2192 | struct addrinfo *last_addr_cur = NULL; |
| 2193 | |
| 2194 | if (conn == NULL || conn->status == CONNECTION_BAD) |
| 2195 | return 0; |
| 2196 | |
| 2197 | /* |
| 2198 | * Set up a time limit, if connect_timeout isn't zero. |
| 2199 | */ |
| 2200 | if (conn->connect_timeout != NULL) |
| 2201 | { |
| 2202 | if (!parse_int_param(conn->connect_timeout, &timeout, conn, |
| 2203 | "connect_timeout")) |
| 2204 | { |
| 2205 | /* mark the connection as bad to report the parsing failure */ |
| 2206 | conn->status = CONNECTION_BAD; |
| 2207 | return 0; |
| 2208 | } |
| 2209 | |
| 2210 | if (timeout > 0) |
| 2211 | { |
| 2212 | /* |
| 2213 | * Rounding could cause connection to fail unexpectedly quickly; |
| 2214 | * to prevent possibly waiting hardly-at-all, insist on at least |
| 2215 | * two seconds. |
| 2216 | */ |
| 2217 | if (timeout < 2) |
| 2218 | timeout = 2; |
| 2219 | } |
| 2220 | else /* negative means 0 */ |
| 2221 | timeout = 0; |
| 2222 | } |
| 2223 | |
| 2224 | for (;;) |
| 2225 | { |
| 2226 | int ret = 0; |
| 2227 | |
| 2228 | /* |
| 2229 | * (Re)start the connect_timeout timer if it's active and we are |
| 2230 | * considering a different host than we were last time through. If |
| 2231 | * we've already succeeded, though, needn't recalculate. |
| 2232 | */ |
| 2233 | if (flag != PGRES_POLLING_OK && |
| 2234 | timeout > 0 && |
| 2235 | (conn->whichhost != last_whichhost || |
| 2236 | conn->addr_cur != last_addr_cur)) |
| 2237 | { |
| 2238 | finish_time = time(NULL) + timeout; |
| 2239 | last_whichhost = conn->whichhost; |
| 2240 | last_addr_cur = conn->addr_cur; |
| 2241 | } |
| 2242 |
no test coverage detected