Skip to content

Commit e7a1b26

Browse files
committed
PYTHON-848 Remove MongoClient.alive.
The ``alive`` method is removed since it no longer provides meaningful information; even if the client is disconnected, it may discover a server in time to fulfill the next operation.
1 parent 7164ba2 commit e7a1b26

File tree

5 files changed

+8
-41
lines changed

5 files changed

+8
-41
lines changed

doc/api/pymongo/mongo_client.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
.. automethod:: disconnect
1010
.. automethod:: close
11-
.. automethod:: alive
1211

1312
.. describe:: c[db_name] || c.db_name
1413

doc/api/pymongo/mongo_replica_set_client.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
.. automethod:: disconnect
1010
.. automethod:: close
11-
.. automethod:: alive
1211

1312
.. describe:: c[db_name] || c.db_name
1413

doc/changelog.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ returns immediately and launches the connection process on
4444
background threads. The ``connect`` option is added to control whether
4545
these threads are started immediately, or when the client is first used.
4646

47+
Therefore the ``alive`` method is removed since it no longer provides meaningful
48+
information; even if the client is disconnected, it may discover a server in
49+
time to fulfill the next operation.
50+
4751
In PyMongo 2.x, :class:`~pymongo.MongoClient` accepted a list of standalone
4852
MongoDB servers and used the first it could connect to::
4953

pymongo/mongo_client.py

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,10 @@ def __init__(
220220
returns immediately and launches the connection process on
221221
background threads.
222222
223+
Therefore the ``alive`` method is removed since it no longer
224+
provides meaningful information; even if the client is disconnected,
225+
it may discover a server in time to fulfill the next operation.
226+
223227
In PyMongo 2.x, :class:`~pymongo.MongoClient` accepted a list of
224228
standalone MongoDB servers and used the first it could connect to::
225229
@@ -657,39 +661,6 @@ def close(self):
657661
"""
658662
self.disconnect()
659663

660-
def alive(self):
661-
"""Return ``False`` if there has been an error communicating with the
662-
server, else ``True``.
663-
664-
This method attempts to check the status of the server (the standalone,
665-
replica set primary, or the mongos currently in use) with minimal I/O.
666-
Retrieves a socket from the pool and checks whether calling `select`_
667-
on it raises an error. If there are currently no idle sockets,
668-
:meth:`alive` attempts to actually connect to the server.
669-
670-
A more certain way to determine server availability is::
671-
672-
client.admin.command('ping')
673-
674-
.. _select: http://docs.python.org/2/library/select.html#select.select
675-
"""
676-
# In the common case, a socket is available and was used recently, so
677-
# calling select() on it is a reasonable attempt to see if the OS has
678-
# reported an error.
679-
try:
680-
# TODO: Mongos pinning.
681-
server = self._topology.select_server(
682-
writable_server_selector,
683-
server_wait_time=0)
684-
685-
# Avoid race when other threads log in or out.
686-
all_credentials = self.__all_credentials.copy()
687-
with server.get_socket(all_credentials) as sock_info:
688-
return not socket_closed(sock_info.sock)
689-
690-
except (socket.error, ConnectionFailure):
691-
return False
692-
693664
def set_cursor_manager(self, manager_class):
694665
"""Set this client's cursor manager.
695666

test/test_client.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -686,12 +686,6 @@ def test_operation_failure(self):
686686
new_sock_info = next(iter(pool.sockets))
687687
self.assertEqual(old_sock_info, new_sock_info)
688688

689-
def test_alive(self):
690-
self.assertTrue(self.client.alive())
691-
692-
client = MongoClient('doesnt exist', connect=False)
693-
self.assertFalse(client.alive())
694-
695689
def test_kill_cursors(self):
696690
if (client_context.is_mongos
697691
and not client_context.version.at_least(2, 4, 7)):

0 commit comments

Comments
 (0)