Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

INTPYTHON-456 Fix handling of server time #45

Merged
merged 5 commits into from
Dec 19, 2024
Merged
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
19 changes: 16 additions & 3 deletions libs/langchain-mongodb/langchain_mongodb/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
from __future__ import annotations

import functools
import warnings
from typing import Any, Dict, List, Optional, Sequence

from langchain_core.indexing.base import RecordManager
from langchain_core.runnables.config import run_in_executor
from pymongo import MongoClient
from pymongo.collection import Collection
from pymongo.errors import OperationFailure


class MongoDBRecordManager(RecordManager):
Expand Down Expand Up @@ -87,9 +89,20 @@ async def aupdate(

def get_time(self) -> float:
"""Get the current server time as a timestamp."""
server_info = self._collection.database.command("hostInfo")
local_time = server_info["system"]["currentTime"]
timestamp = local_time.timestamp()
try:
server_info = self._collection.database.command("hostInfo")
local_time = server_info["system"]["currentTime"]
timestamp = local_time.timestamp()
except OperationFailure:
with warnings.catch_warnings():
warnings.simplefilter("once")
warnings.warn(
"Could not get high-resolution timestamp, falling back to low-resolution",
stacklevel=2,
)
ping = self._collection.database.command("ping")
local_time = ping["operationTime"]
timestamp = float(local_time.time)
return timestamp

async def aget_time(self) -> float:
Expand Down
Loading