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

Using Request.Sessions for faster and less open connections #923

Closed
Closed
Show file tree
Hide file tree
Changes from 2 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
11 changes: 6 additions & 5 deletions meilisearch/_httprequests.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def __init__(self, config: Config) -> None:
"Authorization": f"Bearer {self.config.api_key}",
"User-Agent": _build_user_agent(config.client_agents),
}
self.session = requests.Session()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And add session: Session to the __init__ parameters.

Suggested change
self.session = requests.Session()
self.session = session


def send_request(
self,
Expand Down Expand Up @@ -64,7 +65,7 @@ def send_request(
raise MeilisearchCommunicationError(str(err)) from err

def get(self, path: str) -> Any:
return self.send_request(requests.get, path)
return self.send_request(self.session.get, path)

def post(
self,
Expand All @@ -74,7 +75,7 @@ def post(
] = None,
content_type: Optional[str] = "application/json",
) -> Any:
return self.send_request(requests.post, path, body, content_type)
return self.send_request(self.session.post, path, body, content_type)

def patch(
self,
Expand All @@ -84,7 +85,7 @@ def patch(
] = None,
content_type: Optional[str] = "application/json",
) -> Any:
return self.send_request(requests.patch, path, body, content_type)
return self.send_request(self.session.patch, path, body, content_type)

def put(
self,
Expand All @@ -94,14 +95,14 @@ def put(
] = None,
content_type: Optional[str] = "application/json",
) -> Any:
return self.send_request(requests.put, path, body, content_type)
return self.send_request(self.session.put, path, body, content_type)

def delete(
self,
path: str,
body: Optional[Union[Mapping[str, Any], Sequence[Mapping[str, Any]], List[str]]] = None,
) -> Any:
return self.send_request(requests.delete, path, body)
return self.send_request(self.session.delete, path, body)

@staticmethod
def __to_json(request: requests.Response) -> Any:
Expand Down
5 changes: 3 additions & 2 deletions meilisearch/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ def get_indexes(self, parameters: Optional[Mapping[str, Any]] = None) -> Dict[st
index["primaryKey"],
index["createdAt"],
index["updatedAt"],
self.http,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.http,
self.session = Session()

)
for index in response["results"]
]
Expand Down Expand Up @@ -178,7 +179,7 @@ def get_index(self, uid: str) -> Index:
MeilisearchApiError
An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://www.meilisearch.com/docs/reference/errors/error_codes#meilisearch-errors
"""
return Index(self.config, uid).fetch_info()
return Index(self.config, uid, http=self.http).fetch_info()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return Index(self.config, uid, http=self.http).fetch_info()
return Index(self.config, uid, http=self.session).fetch_info()


def get_raw_index(self, uid: str) -> Dict[str, Any]:
"""Get the index as a dictionary.
Expand Down Expand Up @@ -216,7 +217,7 @@ def index(self, uid: str) -> Index:
An Index instance.
"""
if uid is not None:
return Index(self.config, uid=uid)
return Index(self.config, uid=uid, http=self.http)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return Index(self.config, uid=uid, http=self.http)
return Index(self.config, uid=uid, http=self.session)

raise ValueError("The index UID should not be None")

def multi_search(self, queries: Sequence[Mapping[str, Any]]) -> Dict[str, List[Dict[str, Any]]]:
Expand Down
3 changes: 2 additions & 1 deletion meilisearch/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def __init__(
primary_key: Optional[str] = None,
created_at: Optional[Union[datetime, str]] = None,
updated_at: Optional[Union[datetime, str]] = None,
http: Optional[HttpRequests] = None,
Copy link
Collaborator

@sanders41 sanders41 Feb 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since HttpRequests comes from a private module it's not great to pass it around like this. I think it would be better to pass around the session because of this.

session also needs to be added to the docstring

Suggested change
http: Optional[HttpRequests] = None,
session: Optional[Session] = None,

) -> None:
"""
Parameters
Expand All @@ -43,7 +44,7 @@ def __init__(
Primary-key of the index.
"""
self.config = config
self.http = HttpRequests(config)
self.http = http if http is not None else HttpRequests(config)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.http = http if http is not None else HttpRequests(config)
self.session = session if session else Session()

self.task_handler = TaskHandler(config)
self.uid = uid
self.primary_key = primary_key
Expand Down