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
121 changes: 106 additions & 15 deletions rethinkdb/net.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
import ssl
import struct
import time
from typing import Any, Union
from types import TracebackType
from typing import Any, Protocol, Self, Union, cast
from urllib.parse import parse_qs, urlparse

from rethinkdb.ast import DB, RqlQuery, expr
Expand Down Expand Up @@ -56,9 +57,13 @@

__all__ = [
"DEFAULT_PORT",
"AsyncConnectionContextManager",
"AsyncContextManager",
"Connection",
"Cursor",
"DefaultConnection",
"SyncConnectionContextManager",
"SyncContextManager",
"make_connection",
]

Expand Down Expand Up @@ -190,10 +195,101 @@ def make_error(self, query: Query) -> ReqlError:
return error


class _Closes(Protocol):
"""Object that can be closed by a context manager."""

def close(self, *args: Any, **kwargs: Any) -> Any:
"""Close the object."""


class SyncContextManager:
"""Synchronous context manager that closes the object on exit.

Use ``with`` for drivers whose ``close`` method is synchronous.
"""

def __enter__(self) -> Self:
"""Enter the context and return this object."""
return self

def __exit__(
self,
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> None:
"""Close the object. Exceptions raised in the block propagate."""
cast(_Closes, self).close()


class SyncConnectionContextManager:
"""Synchronous context manager that closes a connection on exit.

Use ``with`` for drivers whose ``close`` method is synchronous.
Exit calls ``close(noreply_wait=False)``.
"""

def __enter__(self) -> Self:
"""Enter the context and return this connection."""
return self

def __exit__(
self,
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> None:
"""Close the connection. Exceptions raised in the block propagate."""
cast(_Closes, self).close(noreply_wait=False)


class AsyncContextManager:
"""Asynchronous context manager that closes the object on exit.

Use ``async with`` for drivers whose ``close`` method is awaitable.
"""

async def __aenter__(self) -> Self:
"""Enter the context and return this object."""
return self

async def __aexit__(
self,
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> None:
"""Close the object. Exceptions raised in the block propagate."""
await cast(_Closes, self).close()


class AsyncConnectionContextManager:
"""Asynchronous context manager that closes a connection on exit.

Use ``async with`` for drivers whose ``close`` method is awaitable.
Exit calls ``close(noreply_wait=False)``.
"""

async def __aenter__(self) -> Self:
"""Enter the context and return this connection."""
return self

async def __aexit__(
self,
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> None:
"""Close the connection. Exceptions raised in the block propagate."""
await cast(_Closes, self).close(noreply_wait=False)


class Cursor:
"""Shared behavior for every cursor implementation.

Callers iterate with ``iter`` or advance one result at a time with ``next``.
Concrete cursors add the context manager for their driver: ``with`` when
``close`` is synchronous, and ``async with`` when ``close`` is awaitable.

* ``query`` is the original query that produced this cursor.

Expand Down Expand Up @@ -242,12 +338,6 @@ def __init__(
self._maybe_fetch_batch()
self._extend_internal(first_response)

def __enter__(self):
return self

def __exit__(self, exc_type, exc_val, exc_tb):
self.close()

@staticmethod
def _empty_error() -> type[ReqlCursorEmpty]:
"""
Expand Down Expand Up @@ -384,9 +474,11 @@ class DefaultCursorEmpty(ReqlCursorEmpty, StopIteration):
"""


class DefaultCursor(Cursor):
class DefaultCursor(Cursor, SyncContextManager):
"""
Default cursor used to get data.

Use ``with`` to close the cursor when the block exits.
"""

def __iter__(self):
Expand Down Expand Up @@ -838,6 +930,9 @@ class Connection: # pylint: disable=too-many-instance-attributes
"""
Handle connection lifecycle, managing the connection instance, connect, reconnect,
connection close and more.

Concrete connections add the context manager for their driver: ``with`` when
``close`` is synchronous, and ``async with`` when ``close`` is awaitable.
"""

_r = None
Expand Down Expand Up @@ -947,12 +1042,6 @@ def repl(self) -> "Connection":
self._repl.set_connection(self)
return self

def __enter__(self):
return self

def __exit__(self, exc_type, exc_val, exc_tb):
self.close(noreply_wait=False)

def use(self, db: str):
"""
Set the encapsulated database to use.
Expand Down Expand Up @@ -1054,9 +1143,11 @@ def get_json_encoder(self, query):
return (query.json_encoder or self._json_encoder)()


class DefaultConnection(Connection):
class DefaultConnection(Connection, SyncConnectionContextManager):
"""
Default connection without async handlers.

Use ``with`` to close the connection when the block exits.
"""

def __init__(self, *args, **kwargs):
Expand Down
31 changes: 20 additions & 11 deletions rethinkdb/net_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,15 @@
ReqlDriverError,
ReqlTimeoutError,
)
from rethinkdb.net import (
AsyncConnectionContextManager,
AsyncContextManager,
Cursor,
Query,
Response,
maybe_profile,
)
from rethinkdb.net import Connection as ConnectionBase
from rethinkdb.net import Cursor, Query, Response, maybe_profile
from rethinkdb.ql2_pb2 import Query as PbQuery
from rethinkdb.ql2_pb2 import Response as PbResponse

Expand Down Expand Up @@ -88,8 +95,12 @@ def translate_timeout_errors():
# to grab the next result. In addition, the waiting coroutine will schedule a
# timeout at the given deadline (if provided), at which point the future will be
# errored.
class AsyncioCursor(Cursor):
"""The asyncio implementation of the Cursor object."""
class AsyncioCursor(Cursor, AsyncContextManager):
"""
The asyncio implementation of the Cursor object.

Use ``async with`` to close the cursor when the block exits.
"""

def __init__(self, *args, **kwargs):
self.new_response = asyncio.Future()
Expand Down Expand Up @@ -378,8 +389,12 @@ async def _reader(self):
await self.close(exception=ex)


class Connection(ConnectionBase):
"""The asyncio implementation of the RethinkDB connection."""
class Connection(ConnectionBase, AsyncConnectionContextManager):
"""
The asyncio implementation of the RethinkDB connection.

Use ``async with`` to close the connection when the block exits.
"""

def __init__(self, *args, **kwargs):
super().__init__(ConnectionInstance, *args, **kwargs)
Expand All @@ -390,12 +405,6 @@ def __init__(self, *args, **kwargs):
f"Could not convert port {self.port} to an integer."
) from exc

async def __aenter__(self):
return self

async def __aexit__(self, exception_type, exception_val, traceback):
await self.close(False)

async def reconnect( # pylint: disable=invalid-overridden-method
self, noreply_wait=True, timeout=None
):
Expand Down
25 changes: 20 additions & 5 deletions rethinkdb/net_gevent.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,27 @@
ReqlTimeoutError,
)
from rethinkdb.net import Connection as ConnectionBase
from rethinkdb.net import Cursor, Query, Response, maybe_profile
from rethinkdb.net import (
Cursor,
Query,
Response,
SyncConnectionContextManager,
SyncContextManager,
maybe_profile,
)
from rethinkdb.net import SocketWrapper as SocketWrapperBase
from rethinkdb.ql2_pb2 import Query as PbQuery
from rethinkdb.ql2_pb2 import Response as PbResponse

__all__ = ["Connection"]


class GeventCursor(Cursor):
"""Gevent-based cursor implementation for RethinkDB queries."""
class GeventCursor(Cursor, SyncContextManager):
"""
Gevent-based cursor implementation for RethinkDB queries.

Use ``with`` to close the cursor when the block exits.
"""

def __init__(self, *args, **kwargs):
self.new_response = Event()
Expand Down Expand Up @@ -340,8 +351,12 @@ def _reader(self):
self.close(exception=exc)


class Connection(ConnectionBase):
"""Gevent-based RethinkDB connection."""
class Connection(ConnectionBase, SyncConnectionContextManager):
"""
Gevent-based RethinkDB connection.

Use ``with`` to close the connection when the block exits.
"""

def __init__(self, *args, **kwargs):
super().__init__(ConnectionInstance, *args, **kwargs)
25 changes: 22 additions & 3 deletions rethinkdb/net_tornado.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,15 @@
ReqlDriverError,
ReqlTimeoutError,
)
from rethinkdb.net import (
AsyncConnectionContextManager,
AsyncContextManager,
Cursor,
Query,
Response,
maybe_profile,
)
from rethinkdb.net import Connection as ConnectionBase
from rethinkdb.net import Cursor, Query, Response, maybe_profile
from rethinkdb.ql2_pb2 import Query as PbQuery
from rethinkdb.ql2_pb2 import Response as PbResponse

Expand All @@ -58,9 +65,11 @@ async def with_absolute_timeout(deadline, generator, **kwargs):
# to grab the next result. In addition, the waiting coroutine will schedule a
# timeout at the given deadline (if provided), at which point the future will be
# errored.
class TornadoCursor(Cursor):
class TornadoCursor(Cursor, AsyncContextManager):
"""
The Tornado implementation of the Cursor object.

Use ``async with`` to close the cursor when the block exits.
"""

def __init__(self, *args, **kwargs):
Expand All @@ -85,6 +94,14 @@ async def __anext__(self):
except ReqlCursorEmpty as exc:
raise StopAsyncIteration from exc

async def close(self): # pylint: disable=invalid-overridden-method
"""Close the cursor and wait for the STOP query."""
if self.error is None:
self.error = self._empty_error()
if self.conn.is_open():
self.outstanding_requests += 1
await self.conn.parent.stop(self)

def extend(self, res_buf):
Cursor.extend(self, res_buf)
self.new_response.set_result(True)
Expand Down Expand Up @@ -382,9 +399,11 @@ async def _handle_response(self, token: int, buf: bytes) -> None:

# Wrap functions from the base connection class that may throw - these will
# put any exception inside a Future and return it.
class Connection(ConnectionBase):
class Connection(ConnectionBase, AsyncConnectionContextManager):
"""
A RethinkDB connection using the Tornado event loop.

Use ``async with`` to close the connection when the block exits.
"""

def __init__(self, *args, **kwargs):
Expand Down
23 changes: 20 additions & 3 deletions rethinkdb/net_trio.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,15 @@
ReqlDriverError,
ReqlTimeoutError,
)
from rethinkdb.net import (
AsyncConnectionContextManager,
Cursor,
Query,
Response,
make_connection,
maybe_profile,
)
from rethinkdb.net import Connection as ConnectionBase
from rethinkdb.net import Cursor, Query, Response, make_connection, maybe_profile

__all__ = ["Connection"]
# pylint: disable=missing-class-docstring,missing-function-docstring
Expand Down Expand Up @@ -108,7 +115,11 @@ def _reql_timeout(seconds):


class TrioCursor(Cursor, trio.abc.AsyncResource):
"""A cursor that allows async iteration within the Trio framework."""
"""
A cursor that allows async iteration within the Trio framework.

Use ``async with`` to close the cursor when the block exits.
"""

def __init__(self, *args, **kwargs):
"""Constructor"""
Expand Down Expand Up @@ -410,7 +421,13 @@ async def _reader_task(self):
self._reader_ended_event.set()


class Connection(ConnectionBase):
class Connection(ConnectionBase, AsyncConnectionContextManager):
"""
A RethinkDB connection using the Trio event loop.

Use ``async with`` to close the connection when the block exits.
"""

def __init__(self, *args, **kwargs):
self._nursery = kwargs.pop("nursery", None)
super().__init__(ConnectionInstance, *args, **kwargs)
Expand Down
Loading
Loading