From c58ace1fb9356ccab5b2c9f7fcbb1bf665da9960 Mon Sep 17 00:00:00 2001 From: Hari Babu Date: Mon, 25 Mar 2019 18:11:18 +1100 Subject: [PATCH 7/8] New function to rejecting the checked write connection When the connection is checked for write or not and based on the result, if we decide to reject it, call the newly added function to reject it. --- src/interfaces/libpq/fe-connect.c | 123 ++++++++++++------------------ 1 file changed, 47 insertions(+), 76 deletions(-) diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c index 59efaa5133..529985ea0c 100644 --- a/src/interfaces/libpq/fe-connect.c +++ b/src/interfaces/libpq/fe-connect.c @@ -2121,6 +2121,51 @@ restoreErrorMessage(PGconn *conn, PQExpBuffer savedMessage) termPQExpBuffer(savedMessage); } +static void +reject_checked_write_connection(PGconn *conn) +{ + /* Not a requested type; fail this connection. */ + const char *displayed_host; + const char *displayed_port; + + /* Append error report to conn->errorMessage. */ + if (conn->connhost[conn->whichhost].type == CHT_HOST_ADDRESS) + displayed_host = conn->connhost[conn->whichhost].hostaddr; + else + displayed_host = conn->connhost[conn->whichhost].host; + displayed_port = conn->connhost[conn->whichhost].port; + if (displayed_port == NULL || displayed_port[0] == '\0') + displayed_port = DEF_PGPORT_STR; + + if (conn->requested_session_type == SESSION_TYPE_READ_WRITE) + appendPQExpBuffer(&conn->errorMessage, + libpq_gettext("could not make a writable " + "connection to server " + "\"%s:%s\"\n"), + displayed_host, displayed_port); + else + appendPQExpBuffer(&conn->errorMessage, + libpq_gettext("could not make a readonly " + "connection to server " + "\"%s:%s\"\n"), + displayed_host, displayed_port); + + /* Close connection politely. */ + conn->status = CONNECTION_OK; + sendTerminateConn(conn); + + /* Record read-write host index */ + if (conn->requested_session_type == SESSION_TYPE_PREFER_READ && + conn->read_write_or_primary_host_index == -1) + conn->read_write_or_primary_host_index = conn->whichhost; + + /* + * Try next host if any, but we don't want to consider additional + * addresses for this host. + */ + conn->try_next_host = true; +} + /* ---------------- * PQconnectPoll * @@ -3547,10 +3592,6 @@ keep_going: /* We will come back to here until there is (conn->requested_session_type == SESSION_TYPE_PREFER_READ || conn->requested_session_type == SESSION_TYPE_READ_ONLY))) { - /* Not a requested type; fail this connection. */ - const char *displayed_host; - const char *displayed_port; - /* * The following scenario is possible only for the * prefer-read mode for the next pass of the list of @@ -3560,42 +3601,7 @@ keep_going: /* We will come back to here until there is if (conn->read_write_or_primary_host_index == -2) goto consume_checked_target_connection; - /* Append error report to conn->errorMessage. */ - if (conn->connhost[conn->whichhost].type == CHT_HOST_ADDRESS) - displayed_host = conn->connhost[conn->whichhost].hostaddr; - else - displayed_host = conn->connhost[conn->whichhost].host; - displayed_port = conn->connhost[conn->whichhost].port; - if (displayed_port == NULL || displayed_port[0] == '\0') - displayed_port = DEF_PGPORT_STR; - - if (conn->requested_session_type == SESSION_TYPE_READ_WRITE) - appendPQExpBuffer(&conn->errorMessage, - libpq_gettext("could not make a writable " - "connection to server " - "\"%s:%s\"\n"), - displayed_host, displayed_port); - else - appendPQExpBuffer(&conn->errorMessage, - libpq_gettext("could not make a readonly " - "connection to server " - "\"%s:%s\"\n"), - displayed_host, displayed_port); - - /* Close connection politely. */ - conn->status = CONNECTION_OK; - sendTerminateConn(conn); - - /* Record read-write host index */ - if (conn->requested_session_type == SESSION_TYPE_PREFER_READ && - conn->read_write_or_primary_host_index == -1) - conn->read_write_or_primary_host_index = conn->whichhost; - - /* - * Try next host if any, but we don't want to consider - * additional addresses for this host. - */ - conn->try_next_host = true; + reject_checked_write_connection(conn); goto keep_going; } @@ -3776,42 +3782,7 @@ keep_going: /* We will come back to here until there is PQclear(res); restoreErrorMessage(conn, &savedMessage); - /* Append error report to conn->errorMessage. */ - if (conn->connhost[conn->whichhost].type == CHT_HOST_ADDRESS) - displayed_host = conn->connhost[conn->whichhost].hostaddr; - else - displayed_host = conn->connhost[conn->whichhost].host; - displayed_port = conn->connhost[conn->whichhost].port; - if (displayed_port == NULL || displayed_port[0] == '\0') - displayed_port = DEF_PGPORT_STR; - - if (conn->requested_session_type == SESSION_TYPE_READ_WRITE) - appendPQExpBuffer(&conn->errorMessage, - libpq_gettext("could not make a writable " - "connection to server " - "\"%s:%s\"\n"), - displayed_host, displayed_port); - else - appendPQExpBuffer(&conn->errorMessage, - libpq_gettext("could not make a readonly " - "connection to server " - "\"%s:%s\"\n"), - displayed_host, displayed_port); - - /* Close connection politely. */ - conn->status = CONNECTION_OK; - sendTerminateConn(conn); - - /* Record read-write host index */ - if (conn->requested_session_type == SESSION_TYPE_PREFER_READ && - conn->read_write_or_primary_host_index == -1) - conn->read_write_or_primary_host_index = conn->whichhost; - - /* - * Try next host if any, but we don't want to consider - * additional addresses for this host. - */ - conn->try_next_host = true; + reject_checked_write_connection(conn); goto keep_going; } -- 2.21.0