Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 33 additions & 30 deletions rethinkdb/net_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,6 @@
__all__ = ["Connection"]


async def _read_until(streamreader, delimiter):
"""Naive implementation of reading until a delimiter"""
buffer = bytearray()

while True:
chunk = await streamreader.read(1)
if not chunk:
break # EOF
buffer.append(chunk[0])
if chunk == delimiter:
break

return bytes(buffer)


def reusable_waiter(loop, timeout):
"""Wait for something, with a timeout from when the waiter was created.

Expand Down Expand Up @@ -173,9 +158,8 @@ def _maybe_fetch_batch(self):
):
self.outstanding_requests += 1
# Keep a reference so the task is not garbage-collected before it runs.
self._fetch_batch_task = asyncio.ensure_future(
self.conn.parent.resume(self),
loop=self.conn.io_loop,
self._fetch_batch_task = self.conn.io_loop.create_task(
self.conn.parent.resume(self)
)


Expand All @@ -192,9 +176,7 @@ def __init__(self, parent, io_loop=None):
self._user_queries = {}
self._cursor_cache = {}
self._ready = asyncio.Future()
self._io_loop = io_loop
if self._io_loop is None:
self._io_loop = asyncio.get_event_loop()
self._io_loop = io_loop if io_loop is not None else asyncio.get_running_loop()

@property
def parent(self):
Expand Down Expand Up @@ -264,9 +246,10 @@ async def connect(self, timeout):
# an optimization, then need to read each separately
if request != "":
self._streamwriter.write(request)
await self._streamwriter.drain()

response = await asyncio.wait_for(
_read_until(self._streamreader, b"\0"),
self._streamreader.readuntil(b"\0"),
timeout,
)
response = response[:-1]
Expand All @@ -287,7 +270,7 @@ async def connect(self, timeout):

# Start a parallel function to perform reads
# store a reference to it so it doesn't get destroyed
self._reader_task = asyncio.ensure_future(self._reader(), loop=self._io_loop)
self._reader_task = self._io_loop.create_task(self._reader())
return self._parent

def is_open(self):
Expand Down Expand Up @@ -327,16 +310,36 @@ async def close(self, noreply_wait=False, token=None, exception=None):

return

def _discard_pending_query(self, token):
"""Drop a waiter whose write failed before the server could answer."""
pending = self._user_queries.get(token)
if pending is None:
return

_, future = pending
if not future.done():
del self._user_queries[token]

async def run_query(self, query, noreply):
"""Run a query on the connection."""
self._streamwriter.write(
query.serialize(self._parent.get_json_encoder(query)) # pylint: disable=protected-access
)
if noreply:
return None
# Register before writing. drain() yields, and the reader can observe
# the response before this coroutine resumes.
response_future = None
if not noreply:
response_future = self._io_loop.create_future()
self._user_queries[query.token] = (query, response_future)

response_future = asyncio.Future()
self._user_queries[query.token] = (query, response_future)
try:
self._streamwriter.write(
query.serialize(self._parent.get_json_encoder(query)) # pylint: disable=protected-access
)
await self._streamwriter.drain()
except Exception:
self._discard_pending_query(query.token)
raise

if response_future is None:
return None
return await response_future

# The _reader coroutine runs in parallel, reading responses
Expand Down
25 changes: 20 additions & 5 deletions rethinkdb/net_gevent.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,20 +294,35 @@ def close(self, noreply_wait=False, token=None, exception=None):
except OSError:
pass

def _discard_pending_query(self, token):
"""Drop a waiter whose write failed before the server could answer."""
pending = self._user_queries.get(token)
if pending is None:
return

_, async_res = pending
if not async_res.ready():
del self._user_queries[token]

def run_query(self, query, noreply):
"""Run a query on the connection."""
# Register before sending. sendall can yield, and the reader can
# observe the response before this greenlet resumes.
self._write_mutex.acquire()

async_res = None
try:
if not noreply:
async_res = AsyncResult()
self._user_queries[query.token] = (query, async_res)
self._socket.sendall(query.serialize(self._parent.get_json_encoder(query)))
except Exception:
self._discard_pending_query(query.token)
raise
finally:
self._write_mutex.release()

if noreply:
if async_res is None:
return None

async_res = AsyncResult()
self._user_queries[query.token] = (query, async_res)
return async_res.get()

def _reader(self):
Expand Down
36 changes: 28 additions & 8 deletions rethinkdb/net_tornado.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ async def _perform_handshake(self, deadline):
# This may happen in the `V1_0` protocol where we send two requests as
# an optimization, then need to read each separately
if request != "":
self._stream.write(request)
await self._stream.write(request)

response = await with_absolute_timeout(
deadline,
Expand Down Expand Up @@ -318,21 +318,41 @@ async def close(
pass
return

def _discard_pending_query(self, token: int) -> None:
"""Drop a waiter whose write failed before the server could answer."""
pending = self._user_queries.get(token)
if pending is None:
return

_, future = pending
if not future.done():
del self._user_queries[token]

async def run_query(self, query: Query, noreply: bool) -> Any | None:
"""
Run a query on the connection.
"""
if self._stream is None:
raise ReqlDriverError("Connection is closed.")

await self._stream.write(query.serialize(self._parent.get_json_encoder(query)))
if noreply:
return None
# Register before writing. The write future yields, and the reader can
# observe the response before this coroutine resumes.
response_future: Future[Any] | None = None
if not noreply:
response_future = Future()
self._user_queries[query.token] = (query, response_future)

response_future: Future = Future()
self._user_queries[query.token] = (query, response_future)
res = await response_future
return res
try:
await self._stream.write(
query.serialize(self._parent.get_json_encoder(query))
)
except Exception:
self._discard_pending_query(query.token)
raise

if response_future is None:
return None
return await response_future

# The _reader coroutine runs in its own context at the top level of the
# Tornado.IOLoop it was created with. It runs in parallel, reading responses
Expand Down
29 changes: 24 additions & 5 deletions rethinkdb/net_trio.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,13 +382,32 @@ async def close(self, noreply_wait=False, token=None, exception=None):

return

def _discard_pending_query(self, token):
"""Drop a waiter whose write failed before the server could answer."""
pending = self._user_queries.get(token)
if pending is None:
return

_, future = pending
if not future.done():
del self._user_queries[token]

async def run_query(self, query, noreply):
await self._send(query.serialize(self._parent.get_json_encoder(query)))
if noreply:
return None
# Register before sending. send_all yields, and the reader can observe
# the response before this coroutine resumes.
response_future = None
if not noreply:
response_future = TrioFuture()
self._user_queries[query.token] = (query, response_future)

response_future = TrioFuture()
self._user_queries[query.token] = (query, response_future)
try:
await self._send(query.serialize(self._parent.get_json_encoder(query)))
except Exception:
self._discard_pending_query(query.token)
raise

if response_future is None:
return None
return await response_future.wait()

# The reader task runs in parallel, reading responses
Expand Down
Loading
Loading