-
Notifications
You must be signed in to change notification settings - Fork 93
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
🐛 Fix async pending error #271
Open
fuergaosi233
wants to merge
5
commits into
Thriftpy:master
Choose a base branch
from
fuergaosi233:fix/fix-async-error
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 1 commit
Commits
Show all changes
5 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 |
---|---|---|
|
@@ -93,8 +93,7 @@ def __init__(self, host=None, port=None, unix_socket=None, | |
ciphers=ciphers) | ||
|
||
if cafile or capath: | ||
self.ssl_context.load_verify_locations(cafile=cafile, | ||
capath=capath) | ||
self.ssl_context.load_verify_locations(cafile=cafile, capath=capath) | ||
|
||
if certfile: | ||
self.ssl_context.load_cert_chain(certfile, keyfile=keyfile) | ||
|
@@ -106,58 +105,23 @@ def __init__(self, host=None, port=None, unix_socket=None, | |
self.ssl_context = None | ||
self.server_hostname = None | ||
|
||
def _init_sock(self): | ||
if self.unix_socket: | ||
_sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) | ||
else: | ||
_sock = socket.socket(self.socket_family, socket.SOCK_STREAM) | ||
_sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) | ||
|
||
# socket options | ||
linger = struct.pack('ii', 0, 0) | ||
_sock.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, linger) | ||
_sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) | ||
|
||
self.raw_sock = _sock | ||
|
||
def set_handle(self, sock): | ||
self.raw_sock = sock | ||
|
||
def set_timeout(self, ms): | ||
"""Backward compat api, will bind the timeout to both connect_timeout | ||
and socket_timeout. | ||
""" | ||
self.socket_timeout = ms / 1000 if (ms and ms > 0) else None | ||
self.connect_timeout = self.socket_timeout | ||
|
||
if self.raw_sock is not None: | ||
self.raw_sock.settimeout(self.socket_timeout) | ||
|
||
def is_open(self): | ||
return bool(self.raw_sock) | ||
|
||
async def open(self): | ||
self._init_sock() | ||
|
||
addr = self.unix_socket or (self.host, self.port) | ||
|
||
try: | ||
if self.connect_timeout: | ||
self.raw_sock.settimeout(self.connect_timeout) | ||
|
||
self.raw_sock.connect(addr) | ||
|
||
if self.socket_timeout: | ||
self.raw_sock.settimeout(self.socket_timeout) | ||
|
||
kwargs = {'sock': self.raw_sock, 'ssl': self.ssl_context} | ||
if self.server_hostname: | ||
kwargs['server_hostname'] = self.server_hostname | ||
|
||
self.reader, self.writer = await asyncio.wait_for( | ||
self.sock_factory(**kwargs), | ||
self.socket_timeout | ||
) | ||
if self.unix_socket: | ||
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. use self.socket_factory instead, see https://github.com/Thriftpy/thriftpy2/pull/271/files#diff-19812d639b869b73a1158c80d996c6fa56787f93905759034caac0381ed7f8fdR82 |
||
self.reader, self.writer = await asyncio.wait_for( | ||
asyncio.open_unix_connection(addr), self.connect_timeout | ||
) | ||
else: | ||
self.reader, self.writer = await asyncio.wait_for( | ||
asyncio.open_connection(self.host, self.port, ssl=self.ssl_context), | ||
self.connect_timeout, | ||
) | ||
sock = self.writer.get_extra_info("socket") | ||
# socket options | ||
linger = struct.pack("ii", 0, 0) | ||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, linger) | ||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) | ||
|
||
except (socket.error, OSError): | ||
raise TTransportException( | ||
|
@@ -166,9 +130,14 @@ async def open(self): | |
|
||
async def read(self, sz): | ||
try: | ||
buff = await asyncio.wait_for( | ||
self.reader.read(sz), | ||
self.connect_timeout | ||
buff = await asyncio.wait_for(self.reader.read(sz), self.connect_timeout) | ||
except asyncio.TimeoutError: | ||
raise TTransportException( | ||
type=TTransportException.TIMED_OUT, message="TSocket read timed out" | ||
) | ||
except asyncio.IncompleteReadError as e: | ||
raise TTransportException( | ||
type=TTransportException.END_OF_FILE, message="TSocket read 0 bytes" | ||
) | ||
except socket.error as e: | ||
if e.errno == errno.ECONNRESET and MAC_OR_BSD: | ||
|
@@ -199,7 +168,6 @@ def close(self): | |
|
||
try: | ||
self.writer.close() | ||
self.raw_sock.close() | ||
self.raw_sock = None | ||
except (socket.error, OSError): | ||
pass | ||
|
@@ -251,7 +219,7 @@ def __init__(self, host=None, port=None, unix_socket=None, | |
self.ssl_context = ssl_context | ||
elif certfile: | ||
if not os.access(certfile, os.R_OK): | ||
raise IOError('No such certfile found: %s' % certfile) | ||
raise IOError("No such certfile found: %s" % certfile) | ||
|
||
self.ssl_context = create_thriftpy_context(server_side=True, | ||
ciphers=ciphers) | ||
|
Oops, something went wrong.
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.
I think we still can implement this method, just set the instance fields, which can be used in open and read methods.