Skip to content

unable to roll over quickly with multiple hosts in green mode #1253

Open
@zhangyangyu

Description

@zhangyangyu

Using psycopg2, I find it's unable that if you provide mutliple hosts to connect to and fail quickly over unconnectable hosts.

For example, if I specify four hosts, host1,2,3,4 in dsn, and host1, host2 are not connectable, currently I find in green mode, I must go through the whole tcp timeout with host1 and host2, and then connect to host3 successfully. I'll explain the reason below, correct me if it's wrong.

For a typical green python application using postgres, we use psycopg2's set_wait_callback, for example eventlet's:

def eventlet_wait_callback(conn, timeout=-1):
    """A wait callback useful to allow eventlet to work with Psycopg."""
    while 1:
        state = conn.poll()
        if state == extensions.POLL_OK:
            break
        elif state == extensions.POLL_READ:
            eventlet.hubs.trampoline(conn.fileno(), read=True)
        elif state == extensions.POLL_WRITE:
            eventlet.hubs.trampoline(conn.fileno(), write=True)
        else:
            raise psycopg2.OperationalError(
                "Bad result from poll: %r" % state)

conn.poll will use libpq's PQConnectPoll to return a socket, but in nonblocking mode, the socket returned doesn't already connect successfully, so we have to poll the socket to see when it's ready. This is how it's designed to work. poll will block until the connect failed, for example typically 127s. It's too long to accept! I could hack the trampoline to give it a timeout, but the timeout could only affect application level, psycopg2 and libpq won't know the timeout. There is no way to tell libpq that ohh don't stick to the host since I think it already fails and try next host please. Inside libpq if the connection is still not made it will move it's state machine and go to next stage if you call the PQConnectPoll again.

The only way I can hack over this is tweak the tcp_sync_retries tcp option to make the connect stage fails more quickly(connect_timeout and tcp_user_timeout not work). But of course it's rather not ideal. I think psycopg2 could provide a function to tweak pgconn's internal status to accomplish this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions