Skip to content

Commit d8b2afa

Browse files
authored
Got rid of time.time()- replacing it with time.monotonic(). (#3551)
1 parent 50943e0 commit d8b2afa

File tree

6 files changed

+23
-23
lines changed

6 files changed

+23
-23
lines changed

redis/client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,7 @@ def check_health(self) -> None:
957957
"did you forget to call subscribe() or psubscribe()?"
958958
)
959959

960-
if conn.health_check_interval and time.time() > conn.next_health_check:
960+
if conn.health_check_interval and time.monotonic() > conn.next_health_check:
961961
conn.send_command("PING", self.HEALTH_CHECK_MESSAGE, check_health=False)
962962
self.health_check_response_counter += 1
963963

@@ -1107,12 +1107,12 @@ def get_message(
11071107
"""
11081108
if not self.subscribed:
11091109
# Wait for subscription
1110-
start_time = time.time()
1110+
start_time = time.monotonic()
11111111
if self.subscribed_event.wait(timeout) is True:
11121112
# The connection was subscribed during the timeout time frame.
11131113
# The timeout should be adjusted based on the time spent
11141114
# waiting for the subscription
1115-
time_spent = time.time() - start_time
1115+
time_spent = time.monotonic() - start_time
11161116
timeout = max(0.0, timeout - time_spent)
11171117
else:
11181118
# The connection isn't subscribed to any channels or patterns,

redis/commands/search/commands.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ def search(
500500
For more information see `FT.SEARCH <https://redis.io/commands/ft.search>`_.
501501
""" # noqa
502502
args, query = self._mk_query_args(query, query_params=query_params)
503-
st = time.time()
503+
st = time.monotonic()
504504

505505
options = {}
506506
if get_protocol_version(self.client) not in ["3", 3]:
@@ -512,7 +512,7 @@ def search(
512512
return res
513513

514514
return self._parse_results(
515-
SEARCH_CMD, res, query=query, duration=(time.time() - st) * 1000.0
515+
SEARCH_CMD, res, query=query, duration=(time.monotonic() - st) * 1000.0
516516
)
517517

518518
def explain(
@@ -602,7 +602,7 @@ def profile(
602602
Each parameter has a name and a value.
603603
604604
"""
605-
st = time.time()
605+
st = time.monotonic()
606606
cmd = [PROFILE_CMD, self.index_name, ""]
607607
if limited:
608608
cmd.append("LIMITED")
@@ -621,7 +621,7 @@ def profile(
621621
res = self.execute_command(*cmd)
622622

623623
return self._parse_results(
624-
PROFILE_CMD, res, query=query, duration=(time.time() - st) * 1000.0
624+
PROFILE_CMD, res, query=query, duration=(time.monotonic() - st) * 1000.0
625625
)
626626

627627
def spellcheck(self, query, distance=None, include=None, exclude=None):
@@ -940,7 +940,7 @@ async def search(
940940
For more information see `FT.SEARCH <https://redis.io/commands/ft.search>`_.
941941
""" # noqa
942942
args, query = self._mk_query_args(query, query_params=query_params)
943-
st = time.time()
943+
st = time.monotonic()
944944

945945
options = {}
946946
if get_protocol_version(self.client) not in ["3", 3]:
@@ -952,7 +952,7 @@ async def search(
952952
return res
953953

954954
return self._parse_results(
955-
SEARCH_CMD, res, query=query, duration=(time.time() - st) * 1000.0
955+
SEARCH_CMD, res, query=query, duration=(time.monotonic() - st) * 1000.0
956956
)
957957

958958
async def aggregate(

redis/connection.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
import ssl
55
import sys
66
import threading
7+
import time
78
import weakref
89
from abc import abstractmethod
910
from itertools import chain
1011
from queue import Empty, Full, LifoQueue
11-
from time import time
1212
from typing import Any, Callable, Dict, List, Optional, Type, TypeVar, Union
1313
from urllib.parse import parse_qs, unquote, urlparse
1414

@@ -542,7 +542,7 @@ def _ping_failed(self, error):
542542

543543
def check_health(self):
544544
"""Check the health of the connection with a PING/PONG"""
545-
if self.health_check_interval and time() > self.next_health_check:
545+
if self.health_check_interval and time.monotonic() > self.next_health_check:
546546
self.retry.call_with_retry(self._send_ping, self._ping_failed)
547547

548548
def send_packed_command(self, command, check_health=True):
@@ -632,7 +632,7 @@ def read_response(
632632
raise
633633

634634
if self.health_check_interval:
635-
self.next_health_check = time() + self.health_check_interval
635+
self.next_health_check = time.monotonic() + self.health_check_interval
636636

637637
if isinstance(response, ResponseError):
638638
try:

tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ def wait_for_cluster_creation(redis_url, cluster_nodes, timeout=60):
237237
:param cluster_nodes: The number of nodes in the cluster
238238
:param timeout: the amount of time to wait (in seconds)
239239
"""
240-
now = time.time()
240+
now = time.monotonic()
241241
end_time = now + timeout
242242
client = None
243243
print(f"Waiting for {cluster_nodes} cluster nodes to become available")
@@ -250,7 +250,7 @@ def wait_for_cluster_creation(redis_url, cluster_nodes, timeout=60):
250250
except RedisClusterException:
251251
pass
252252
time.sleep(1)
253-
now = time.time()
253+
now = time.monotonic()
254254
if now >= end_time:
255255
available_nodes = 0 if client is None else len(client.get_nodes())
256256
raise RedisClusterException(

tests/test_connection_pool.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,11 @@ def test_connection_pool_blocks_until_timeout(self, master_host):
167167
)
168168
pool.get_connection()
169169

170-
start = time.time()
170+
start = time.monotonic()
171171
with pytest.raises(redis.ConnectionError):
172172
pool.get_connection()
173173
# we should have waited at least 0.1 seconds
174-
assert time.time() - start >= 0.1
174+
assert time.monotonic() - start >= 0.1
175175

176176
def test_connection_pool_blocks_until_conn_available(self, master_host):
177177
"""
@@ -188,10 +188,10 @@ def target():
188188
time.sleep(0.1)
189189
pool.release(c1)
190190

191-
start = time.time()
191+
start = time.monotonic()
192192
Thread(target=target).start()
193193
pool.get_connection()
194-
assert time.time() - start >= 0.1
194+
assert time.monotonic() - start >= 0.1
195195

196196
def test_reuse_previously_released_connection(self, master_host):
197197
connection_kwargs = {"host": master_host[0], "port": master_host[1]}
@@ -679,18 +679,18 @@ def r(self, request):
679679
return _get_client(redis.Redis, request, health_check_interval=self.interval)
680680

681681
def assert_interval_advanced(self, connection):
682-
diff = connection.next_health_check - time.time()
682+
diff = connection.next_health_check - time.monotonic()
683683
assert self.interval > diff > (self.interval - 1)
684684

685685
def test_health_check_runs(self, r):
686-
r.connection.next_health_check = time.time() - 1
686+
r.connection.next_health_check = time.monotonic() - 1
687687
r.connection.check_health()
688688
self.assert_interval_advanced(r.connection)
689689

690690
def test_arbitrary_command_invokes_health_check(self, r):
691691
# invoke a command to make sure the connection is entirely setup
692692
r.get("foo")
693-
r.connection.next_health_check = time.time()
693+
r.connection.next_health_check = time.monotonic()
694694
with mock.patch.object(
695695
r.connection, "send_command", wraps=r.connection.send_command
696696
) as m:

tests/test_pubsub.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
def wait_for_message(
2424
pubsub, timeout=0.5, ignore_subscribe_messages=False, node=None, func=None
2525
):
26-
now = time.time()
26+
now = time.monotonic()
2727
timeout = now + timeout
2828
while now < timeout:
2929
if node:
@@ -39,7 +39,7 @@ def wait_for_message(
3939
if message is not None:
4040
return message
4141
time.sleep(0.01)
42-
now = time.time()
42+
now = time.monotonic()
4343
return None
4444

4545

0 commit comments

Comments
 (0)