From c3ced4c6cec165ba1fb9b9a760140ad3df4862ec Mon Sep 17 00:00:00 2001 From: blueoom <4945756+anexplore@users.noreply.github.com> Date: Fri, 9 Aug 2024 23:31:20 +0800 Subject: [PATCH] core[patch]: use time.monotonic() instead time.time() in InMemoryRateLimiter **Description:** The get time point method in the _consume() method of core.rate_limiters.InMemoryRateLimiter uses time.time(), which can be affected by system time backwards. Therefore, it is recommended to use the monotonically increasing monotonic() to obtain the time ```python with self._consume_lock: now = time.time() # time.time() -> time.monotonic() # initialize on first call to avoid a burst if self.last is None: self.last = now elapsed = now - self.last # when use time.time(), elapsed may be negative when system time backwards ``` --- libs/core/langchain_core/rate_limiters.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/core/langchain_core/rate_limiters.py b/libs/core/langchain_core/rate_limiters.py index b38702103fc..043e54cc38f 100644 --- a/libs/core/langchain_core/rate_limiters.py +++ b/libs/core/langchain_core/rate_limiters.py @@ -181,7 +181,7 @@ class InMemoryRateLimiter(BaseRateLimiter): the caller should try again later. """ with self._consume_lock: - now = time.time() + now = time.monotonic() # initialize on first call to avoid a burst if self.last is None: