-
Notifications
You must be signed in to change notification settings - Fork 158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixing connection with multiple hosts and unavailable replicas #579
Open
r-dmv
wants to merge
3
commits into
aio-libs:master
Choose a base branch
from
r-dmv:multiple-hosts
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,10 +3,12 @@ | |
import errno | ||
import platform | ||
import select | ||
import socket | ||
import sys | ||
import traceback | ||
import warnings | ||
import weakref | ||
import ctypes | ||
|
||
import psycopg2 | ||
from psycopg2 import extras | ||
|
@@ -28,6 +30,17 @@ | |
# to OSError.errno EBADF | ||
WSAENOTSOCK = 10038 | ||
|
||
# Connection status from psycopg2 (psycopg2/psycopg/connection.h) | ||
CONN_STATUS_CONNECTING = 20 | ||
|
||
# In socket.socket we should know type and family to shutdown socket by fd | ||
# This function is used for shutdown libpq connection | ||
# where family and type is unknown | ||
libc = ctypes.CDLL(None) | ||
socket_shutdown = libc.shutdown | ||
socket_shutdown.restypes = ctypes.c_int | ||
socket_shutdown.argtypes = ctypes.c_int, ctypes.c_int | ||
|
||
|
||
async def _enable_hstore(conn): | ||
cur = await conn.cursor() | ||
|
@@ -111,6 +124,11 @@ def __init__(self, dsn, loop, timeout, waiter, echo, **kwargs): | |
self._loop = loop | ||
self._conn = psycopg2.connect(dsn, async_=True, **kwargs) | ||
self._dsn = self._conn.dsn | ||
self._dns_params = self._conn.get_dsn_parameters() | ||
self._conn_timeout = self._dns_params.get('connect_timeout') | ||
if self._conn_timeout: | ||
self._conn_timeout = float(self._conn_timeout) | ||
|
||
assert self._conn.isexecuting(), "Is conn an async at all???" | ||
self._fileno = self._conn.fileno() | ||
self._timeout = timeout | ||
|
@@ -124,10 +142,26 @@ def __init__(self, dsn, loop, timeout, waiter, echo, **kwargs): | |
self._notifies = asyncio.Queue(loop=loop) | ||
self._weakref = weakref.ref(self) | ||
self._loop.add_reader(self._fileno, self._ready, self._weakref) | ||
self._conn_timeout_handler = None | ||
if self._conn_timeout: | ||
self._conn_timeout_handler = self._loop.call_later( | ||
self._conn_timeout, self._shutdown, self._weakref) | ||
|
||
if loop.get_debug(): | ||
self._source_traceback = traceback.extract_stack(sys._getframe(1)) | ||
|
||
@staticmethod | ||
def _shutdown(weak_self): | ||
# Make sure that we won't get stuck in a blocking read or write | ||
# inside poll, then give libpq a chance to try another host. | ||
# If there is an error, we'll get it from poll. | ||
self = weak_self() | ||
|
||
if self._conn.status == CONN_STATUS_CONNECTING: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This occasionally fails due to Minimal fix would be to skip the rest when that is the case, but generally there shouldn't be active timers for a deleted object. |
||
socket_shutdown(ctypes.c_int(self._fileno), | ||
ctypes.c_int(socket.SHUT_RDWR)) | ||
self._ready(self._weakref) | ||
|
||
@staticmethod | ||
def _ready(weak_self): | ||
self = weak_self() | ||
|
@@ -172,6 +206,28 @@ def _ready(weak_self): | |
if waiter is not None and not waiter.done(): | ||
waiter.set_exception( | ||
psycopg2.OperationalError("Connection closed")) | ||
|
||
if self._conn.status == CONN_STATUS_CONNECTING \ | ||
and state != POLL_ERROR: | ||
# libpq could close and open new connection to the next host | ||
old_fileno = self._fileno | ||
self._fileno = self._conn.fileno() | ||
|
||
if self._conn_timeout_handler: | ||
self._conn_timeout_handler.cancel() | ||
self._conn_timeout_handler = self._loop.call_later( | ||
self._conn_timeout, self._shutdown, self._weakref) | ||
|
||
with contextlib.suppress(OSError): | ||
# if we are using select selector | ||
self._loop.remove_reader(old_fileno) | ||
if self._writing: | ||
self._loop.remove_writer(old_fileno) | ||
|
||
self._loop.add_reader(self._fileno, self._ready, weak_self) | ||
if self._writing: | ||
self._loop.add_writer(self._fileno, self._ready, weak_self) | ||
|
||
if state == POLL_OK: | ||
if self._writing: | ||
self._loop.remove_writer(self._fileno) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the problem with just using the socket object?