Skip to content

Commit cf877e9

Browse files
committed
PYTHON-2503 Always use time.monotonic
For monotonic time needs.
1 parent e01d9a3 commit cf877e9

13 files changed

+50
-131
lines changed

pymongo/client_session.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
"""
9999

100100
import collections
101+
import time
101102
import uuid
102103

103104
from collections.abc import Mapping as _Mapping
@@ -107,7 +108,6 @@
107108
from bson.son import SON
108109
from bson.timestamp import Timestamp
109110

110-
from pymongo import monotonic
111111
from pymongo.errors import (ConfigurationError,
112112
ConnectionFailure,
113113
InvalidOperation,
@@ -336,7 +336,7 @@ def _max_time_expired_error(exc):
336336

337337
def _within_time_limit(start_time):
338338
"""Are we within the with_transaction retry limit?"""
339-
return monotonic.time() - start_time < _WITH_TRANSACTION_RETRY_TIME_LIMIT
339+
return time.monotonic() - start_time < _WITH_TRANSACTION_RETRY_TIME_LIMIT
340340

341341

342342
class ClientSession(object):
@@ -518,7 +518,7 @@ def callback(session, custom_arg, custom_kwarg=None):
518518
519519
.. versionadded:: 3.9
520520
"""
521-
start_time = monotonic.time()
521+
start_time = time.monotonic()
522522
while True:
523523
self.start_transaction(
524524
read_concern, write_concern, read_preference,
@@ -797,7 +797,7 @@ def _txn_read_preference(self):
797797
def _apply_to(self, command, is_retryable, read_preference):
798798
self._check_ended()
799799

800-
self._server_session.last_use = monotonic.time()
800+
self._server_session.last_use = time.monotonic()
801801
command['lsid'] = self._server_session.session_id
802802

803803
if not self.in_transaction:
@@ -842,7 +842,7 @@ class _ServerSession(object):
842842
def __init__(self, generation):
843843
# Ensure id is type 4, regardless of CodecOptions.uuid_representation.
844844
self.session_id = {'id': Binary(uuid.uuid4().bytes, 4)}
845-
self.last_use = monotonic.time()
845+
self.last_use = time.monotonic()
846846
self._transaction_id = 0
847847
self.dirty = False
848848
self.generation = generation
@@ -856,7 +856,7 @@ def mark_dirty(self):
856856
self.dirty = True
857857

858858
def timed_out(self, session_timeout_minutes):
859-
idle_seconds = monotonic.time() - self.last_use
859+
idle_seconds = time.monotonic() - self.last_use
860860

861861
# Timed out if we have less than a minute to live.
862862
return idle_seconds > (session_timeout_minutes - 1) * 60

pymongo/monitor.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616

1717
import atexit
1818
import threading
19+
import time
1920
import weakref
2021

2122
from pymongo import common, periodic_executor
2223
from pymongo.errors import (NotMasterError,
2324
OperationFailure,
2425
_OperationCancelled)
2526
from pymongo.ismaster import IsMaster
26-
from pymongo.monotonic import time as _time
2727
from pymongo.periodic_executor import _shutdown_executors
2828
from pymongo.read_preferences import MovingAverage
2929
from pymongo.server_description import ServerDescription
@@ -208,7 +208,7 @@ def _check_server(self):
208208
209209
Returns a ServerDescription.
210210
"""
211-
start = _time()
211+
start = time.monotonic()
212212
try:
213213
try:
214214
return self._check_once()
@@ -223,7 +223,7 @@ def _check_server(self):
223223
_sanitize(error)
224224
sd = self._server_description
225225
address = sd.address
226-
duration = _time() - start
226+
duration = time.monotonic() - start
227227
if self._publish:
228228
awaited = sd.is_server_type_known and sd.topology_version
229229
self._listeners.publish_server_heartbeat_failed(
@@ -265,7 +265,7 @@ def _check_with_socket(self, conn):
265265
Can raise ConnectionFailure or OperationFailure.
266266
"""
267267
cluster_time = self._topology.max_cluster_time()
268-
start = _time()
268+
start = time.monotonic()
269269
if conn.more_to_come:
270270
# Read the next streaming isMaster (MongoDB 4.4+).
271271
response = IsMaster(conn._next_reply(), awaitable=True)
@@ -280,7 +280,7 @@ def _check_with_socket(self, conn):
280280
else:
281281
# New connection handshake or polling isMaster (MongoDB <4.4).
282282
response = conn._ismaster(cluster_time, None, None, None)
283-
return response, _time() - start
283+
return response, time.monotonic() - start
284284

285285

286286
class SrvMonitor(MonitorBase):
@@ -388,9 +388,9 @@ def _ping(self):
388388
with self._pool.get_socket({}) as sock_info:
389389
if self._executor._stopped:
390390
raise Exception('_RttMonitor closed')
391-
start = _time()
391+
start = time.monotonic()
392392
sock_info.ismaster()
393-
return _time() - start
393+
return time.monotonic() - start
394394

395395

396396
# Close monitors to cancel any in progress streaming checks before joining

pymongo/monotonic.py

Lines changed: 0 additions & 38 deletions
This file was deleted.

pymongo/network.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import errno
1919
import socket
2020
import struct
21+
import time
2122

2223

2324
from bson import _decode_all_selective
@@ -31,7 +32,6 @@
3132
ProtocolError,
3233
_OperationCancelled)
3334
from pymongo.message import _UNPACK_REPLY, _OpMsg
34-
from pymongo.monotonic import time
3535
from pymongo.socket_checker import _errno_from_exception
3636

3737

@@ -185,7 +185,7 @@ def receive_message(sock_info, request_id, max_message_size=MAX_MESSAGE_SIZE):
185185
"""Receive a raw BSON message or raise socket.error."""
186186
timeout = sock_info.sock.gettimeout()
187187
if timeout:
188-
deadline = time() + timeout
188+
deadline = time.monotonic() + timeout
189189
else:
190190
deadline = None
191191
# Ignore the response's request id.
@@ -236,7 +236,7 @@ def wait_for_read(sock_info, deadline):
236236
# Wait up to 500ms for the socket to become readable and then
237237
# check for cancellation.
238238
if deadline:
239-
timeout = max(min(deadline - time(), _POLL_TIMEOUT), 0.001)
239+
timeout = max(min(deadline - time.monotonic(), _POLL_TIMEOUT), 0.001)
240240
else:
241241
timeout = _POLL_TIMEOUT
242242
readable = sock_info.socket_checker.select(
@@ -245,7 +245,7 @@ def wait_for_read(sock_info, deadline):
245245
raise _OperationCancelled('isMaster cancelled')
246246
if readable:
247247
return
248-
if deadline and time() > deadline:
248+
if deadline and time.monotonic() > deadline:
249249
raise socket.timeout("timed out")
250250

251251
def _receive_data_on_socket(sock_info, length, deadline):

pymongo/periodic_executor.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
import time
1919
import weakref
2020

21-
from pymongo.monotonic import time as _time
22-
2321

2422
class PeriodicExecutor(object):
2523
def __init__(self, interval, min_interval, target, name=None):
@@ -135,8 +133,8 @@ def _run(self):
135133
if self._skip_sleep:
136134
self._skip_sleep = False
137135
else:
138-
deadline = _time() + self._interval
139-
while not self._stopped and _time() < deadline:
136+
deadline = time.monotonic() + self._interval
137+
while not self._stopped and time.monotonic() < deadline:
140138
time.sleep(self._min_interval)
141139
if self._event:
142140
break # Early wake.

pymongo/pool.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# implied. See the License for the specific language governing
1313
# permissions and limitations under the License.
1414

15+
import collections
1516
import contextlib
1617
import copy
1718
import ipaddress
@@ -20,7 +21,7 @@
2021
import socket
2122
import sys
2223
import threading
23-
import collections
24+
import time
2425

2526
from bson import DEFAULT_CODEC_OPTIONS
2627
from bson.son import SON
@@ -48,7 +49,6 @@
4849
OperationFailure,
4950
PyMongoError)
5051
from pymongo.ismaster import IsMaster
51-
from pymongo.monotonic import time as _time
5252
from pymongo.monitoring import (ConnectionCheckOutFailedReason,
5353
ConnectionClosedReason)
5454
from pymongo.network import (command,
@@ -250,7 +250,7 @@ def _raise_connection_failure(address, error, msg_prefix=None):
250250

251251

252252
def _cond_wait(condition, deadline):
253-
timeout = deadline - _time() if deadline else None
253+
timeout = deadline - time.monotonic() if deadline else None
254254
return condition.wait(timeout)
255255

256256

@@ -502,7 +502,7 @@ def __init__(self, sock, pool, address, id):
502502
self.id = id
503503
self.authset = set()
504504
self.closed = False
505-
self.last_checkin_time = _time()
505+
self.last_checkin_time = time.monotonic()
506506
self.performed_handshake = False
507507
self.is_writable = False
508508
self.max_wire_version = MAX_WIRE_VERSION
@@ -862,14 +862,14 @@ def add_server_api(self, command, session):
862862
_add_to_command(command, self.opts.server_api)
863863

864864
def update_last_checkin_time(self):
865-
self.last_checkin_time = _time()
865+
self.last_checkin_time = time.monotonic()
866866

867867
def update_is_writable(self, is_writable):
868868
self.is_writable = is_writable
869869

870870
def idle_time_seconds(self):
871871
"""Seconds since this socket was last checked into its pool."""
872-
return _time() - self.last_checkin_time
872+
return time.monotonic() - self.last_checkin_time
873873

874874
def _raise_connection_failure(self, error):
875875
# Catch *all* exceptions from socket methods and close the socket. In
@@ -1336,7 +1336,7 @@ def _get_socket(self, all_credentials):
13361336

13371337
# Get a free socket or create one.
13381338
if self.opts.wait_queue_timeout:
1339-
deadline = _time() + self.opts.wait_queue_timeout
1339+
deadline = time.monotonic() + self.opts.wait_queue_timeout
13401340
else:
13411341
deadline = None
13421342

pymongo/pyopenssl_context.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import socket as _socket
2020
import ssl as _stdlibssl
21+
import time
2122

2223
from errno import EINTR as _EINTR
2324

@@ -33,7 +34,6 @@
3334
VerificationError as _SIVerificationError)
3435

3536
from pymongo.errors import CertificateError as _CertificateError
36-
from pymongo.monotonic import time as _time
3737
from pymongo.ocsp_support import (
3838
_load_trusted_ca_certs,
3939
_ocsp_callback)
@@ -98,14 +98,14 @@ def __init__(self, ctx, sock, suppress_ragged_eofs):
9898
def _call(self, call, *args, **kwargs):
9999
timeout = self.gettimeout()
100100
if timeout:
101-
start = _time()
101+
start = time.monotonic()
102102
while True:
103103
try:
104104
return call(*args, **kwargs)
105105
except _RETRY_ERRORS:
106106
self.socket_checker.select(
107107
self, True, True, timeout)
108-
if timeout and _time() - start > timeout:
108+
if timeout and time.monotonic() - start > timeout:
109109
raise _socket.timeout("timed out")
110110
continue
111111

pymongo/server_description.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@
1414

1515
"""Represent one server the driver is connected to."""
1616

17+
import time
18+
1719
from bson import EPOCH_NAIVE
1820
from pymongo.server_type import SERVER_TYPE
1921
from pymongo.ismaster import IsMaster
20-
from pymongo.monotonic import time as _time
2122

2223

2324
class ServerDescription(object):
@@ -67,7 +68,7 @@ def __init__(
6768
self._ls_timeout_minutes = ismaster.logical_session_timeout_minutes
6869
self._round_trip_time = round_trip_time
6970
self._me = ismaster.me
70-
self._last_update_time = _time()
71+
self._last_update_time = time.monotonic()
7172
self._error = error
7273
self._topology_version = ismaster.topology_version
7374
if error:

0 commit comments

Comments
 (0)