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
4 changes: 3 additions & 1 deletion rethinkdb/net.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,9 @@ def raise_error(self, message: str):
"""
if self.error is None:
self.error = ReqlRuntimeError(message, self.query.term_type, [])
dummy_response = f'{"t":{PbResponse.ResponseType.SUCCESS_SEQUENCE},"r":[]}'
dummy_response = (
f'{{"t":{PbResponse.ResponseType.SUCCESS_SEQUENCE},"r":[]}}'
).encode()
self.extend(dummy_response)

def _maybe_fetch_batch(self):
Expand Down
34 changes: 24 additions & 10 deletions rethinkdb/net_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ class AsyncioCursor(Cursor):
"""The asyncio implementation of the Cursor object."""

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.new_response = asyncio.Future()
super().__init__(*args, **kwargs)

def __aiter__(self):
return self
Expand All @@ -109,10 +109,14 @@ async def close(self): # pylint: disable=invalid-overridden-method
self.error = self._empty_error()
if self.conn.is_open():
self.outstanding_requests += 1
await self.conn.parent._stop(self) # pylint: disable=protected-access
await self.conn.parent.stop(self)

def extend(self, res_buf):
"""Extend the cursor and wake tasks waiting for the next batch."""
self._extend(res_buf)

def _extend(self, res_buf):
super()._extend(res_buf) # pylint: disable=no-member
super().extend(res_buf)
self.new_response.set_result(True)
self.new_response = asyncio.Future()

Expand Down Expand Up @@ -159,7 +163,7 @@ 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._continue(self), # pylint: disable=protected-access
self.conn.parent.resume(self),
loop=self.conn.io_loop,
)

Expand All @@ -181,6 +185,21 @@ def __init__(self, parent, io_loop=None):
if self._io_loop is None:
self._io_loop = asyncio.get_event_loop()

@property
def parent(self):
"""Return the parent connection."""
return self._parent

@property
def cursor_cache(self):
"""Return the cursor cache."""
return self._cursor_cache

@property
def io_loop(self):
"""Return the event loop used by this connection."""
return self._io_loop

def client_port(self):
"""Get the client port of the connection."""
if self.is_open():
Expand Down Expand Up @@ -274,7 +293,7 @@ async def close(self, noreply_wait=False, token=None, exception=None):

# Cursors may remove themselves when errored, so copy a list of them
for cursor in list(self._cursor_cache.values()):
cursor._error(err_message) # pylint: disable=protected-access
cursor.raise_error(err_message)

for _query, future in iter(self._user_queries.values()):
if not future.done():
Expand Down Expand Up @@ -377,11 +396,6 @@ async def __aenter__(self):
async def __aexit__(self, exception_type, exception_val, traceback):
await self.close(False)

async def _stop(self, cursor):
self.check_open()
q = Query(PbQuery.QueryType.STOP, cursor.query.token, None, None)
return await self._instance.run_query(q, True) # pylint: disable=protected-access

async def reconnect( # pylint: disable=invalid-overridden-method
self, noreply_wait=True, timeout=None
):
Expand Down
29 changes: 21 additions & 8 deletions rethinkdb/net_gevent.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,30 @@ class GeventCursor(Cursor):
"""Gevent-based cursor implementation for RethinkDB queries."""

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.new_response = Event()
super().__init__(*args, **kwargs)

def __iter__(self):
return self

def __next__(self):
return self._get_next(None)
try:
return self._get_next(None)
except ReqlCursorEmpty as exc:
raise StopIteration from exc

@staticmethod
def _empty_error():
"""Return the empty error exception class."""
return ReqlCursorEmpty()

def extend(self, res_buf):
"""Extend the cursor and wake greenlets waiting for the next batch."""
self._extend(res_buf)

def _extend(self, res_buf):
"""Extend the cursor with new response data."""
# pylint: disable=no-member
super()._extend(res_buf)
# pylint: enable=no-member
super().extend(res_buf)
self.new_response.set()
self.new_response.clear()

Expand Down Expand Up @@ -226,6 +231,16 @@ def __init__(self, parent, io_loop=None):
self._write_mutex = Semaphore()
self._socket = None

@property
def parent(self):
"""Return the parent connection."""
return self._parent

@property
def cursor_cache(self):
"""Return the cursor cache."""
return self._cursor_cache

def connect(self, timeout):
"""Connect to the RethinkDB server."""
with gevent.Timeout(
Expand All @@ -251,9 +266,7 @@ def close(self, noreply_wait=False, token=None, exception=None):

# Cursors may remove themselves when errored, so copy a list of them
for cursor in list(self._cursor_cache.values()):
# pylint: disable=protected-access
cursor._error(err_message)
# pylint: enable=protected-access
cursor.raise_error(err_message)

for _query, async_res in iter(self._user_queries.values()):
async_res.set_exception(ReqlDriverError(err_message))
Expand Down
38 changes: 34 additions & 4 deletions rethinkdb/net_tornado.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,17 @@ def __aiter__(self):

async def __anext__(self):
"""Get the next item from the cursor."""
if await self.fetch_next():
return await self.next()

raise StopAsyncIteration
try:
if not await self.fetch_next():
raise StopAsyncIteration
# next() returns the buffered row directly. It is only awaitable when
# a subclass waits for the next batch inside _get_next.
item = self.next()
if asyncio.iscoroutine(item) or asyncio.isfuture(item):
return await item
return item
except ReqlCursorEmpty as exc:
raise StopAsyncIteration from exc

def extend(self, res_buf):
Cursor.extend(self, res_buf)
Expand Down Expand Up @@ -120,6 +127,19 @@ def _get_next(self, timeout=None):
)
return self.items.popleft()

def _maybe_fetch_batch(self):
if (
self.error is None
and len(self.items) < self.threshold
and self.outstanding_requests == 0
):
self.outstanding_requests += 1
# resume() is a coroutine; the IOLoop must run it so CONTINUE is sent.
self.conn._io_loop.spawn_callback( # pylint: disable=protected-access
self.conn.parent.resume,
self,
)


class ConnectionInstance:
"""
Expand All @@ -137,6 +157,16 @@ def __init__(self, parent: "Connection", io_loop: IOLoop | None = None):
self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
self._socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)

@property
def parent(self) -> "Connection":
"""Return the parent connection."""
return self._parent

@property
def cursor_cache(self) -> dict[int, TornadoCursor]:
"""Return the cursor cache."""
return self._cursor_cache

def client_port(self) -> int | None:
"""
Get the client port of the connection.
Expand Down
31 changes: 20 additions & 11 deletions rethinkdb/net_trio.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,13 @@ async def close(self): # pylint: disable=invalid-overridden-method

aclose = close

def extend(self, res_buf):
"""Extend the cursor and wake tasks waiting for the next batch."""
self._extend(res_buf)

def _extend(self, res_buf):
"""Override so that we can make this async, and also to wake up blocked
tasks."""
self.outstanding_requests -= 1
self._maybe_fetch_batch()
res = Response(self.query.token, res_buf, self._json_decoder)
self._extend_internal(res)
"""Append a response batch and wake tasks waiting for it."""
super().extend(res_buf)
self._new_response.set()
self._new_response = trio.Event()

Expand Down Expand Up @@ -179,8 +179,8 @@ async def _get_next(self, timeout=None): # pylint: disable=invalid-overridden-m
return item

async def _parent_continue(self):
# pylint: disable=protected-access
return await self.conn._parent._continue(self)
"""Send a CONTINUE request for this cursor."""
return await self.conn.parent.resume(self)

def _maybe_fetch_batch(self):
if (
Expand All @@ -189,8 +189,7 @@ def _maybe_fetch_batch(self):
and self.outstanding_requests == 0
):
self.outstanding_requests += 1
# pylint: disable=protected-access
self._nursery.start_soon(self.conn._parent._continue, self)
self._nursery.start_soon(self._parent_continue)


class ConnectionInstance:
Expand All @@ -206,6 +205,16 @@ def __init__(self, parent, nursery=None):
self._reader_ended_event = None
self._nursery = nursery

@property
def parent(self):
"""Return the parent connection."""
return self._parent

@property
def cursor_cache(self):
"""Return the cursor cache."""
return self._cursor_cache

def client_port(self):
if self.is_open():
return self._sockname[1]
Expand Down Expand Up @@ -319,7 +328,7 @@ async def close(self, noreply_wait=False, token=None, exception=None):

# Cursors may remove themselves when errored, so copy a list of them
for cursor in list(self._cursor_cache.values()):
cursor._error(err_message) # pylint: disable=protected-access
cursor.raise_error(err_message)

for _, future in self._user_queries.values():
if not future.done():
Expand Down
22 changes: 20 additions & 2 deletions rethinkdb/net_twisted.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from twisted.internet.endpoints import clientFromString
from twisted.internet.error import TimeoutError as TwistedTimeoutError
from twisted.internet.protocol import ClientFactory, Protocol
from twisted.python import log

from rethinkdb.errors import (
ReqlAuthError,
Expand Down Expand Up @@ -269,7 +270,8 @@ def _is_empty(self) -> bool:
@inlineCallbacks
def _get_next(self, timeout: float | None = None) -> "Deferred[Any]":
if len(self.items) == 0 and self.error is not None:
return defer.fail(self.error)
result = yield defer.fail(self.error)
return result

def raise_timeout(errback: Any) -> None:
if isinstance(errback.value, CancelledError):
Expand All @@ -284,7 +286,18 @@ def raise_timeout(errback: Any) -> None:
reactor.callLater(timeout, item_defer.cancel)

self._maybe_fetch_batch()
return item_defer
item = yield item_defer
return item

def _maybe_fetch_batch(self) -> None:
if (
self.error is None
and len(self.items) < self.threshold
and self.outstanding_requests == 0
):
self.outstanding_requests += 1
deferred = self.conn.parent.resume(self)
deferred.addErrback(log.err)


class ConnectionInstance:
Expand Down Expand Up @@ -397,6 +410,11 @@ def connect(self, timeout: int) -> Generator:

return self._parent

@property
def parent(self) -> "Connection":
"""Return the parent connection."""
return self._parent

@property
def cursor_cache(self) -> dict[int, "TwistedCursor"]:
"""
Expand Down
Loading
Loading