-
Notifications
You must be signed in to change notification settings - Fork 90
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
Conversation
Do need some help as to why this test doesn't pass:
|
It fails because the test patches I understand why in theory this could be faster in the right situation, but did you do any benchmarking? In my extreamly quick and crude benchmark there was no improvement. |
Haven't done any benchmarks, other than knowing that The bigger "issue" which arose in my setup is Connection timeout's due to the quantity of connections were being made to my Meilisearch Server, probably nginx settings. But I also got There is no reason to not reuse a TCP connection when possible. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have an issue with it in principal. It's just that it is a change for the sake of a change if we can't show it makes any difference. @curquiza I'll let you make the call here, I don't have a preference either way.
If we do implement it I think there are a few changes we should make.
meilisearch/index.py
Outdated
@@ -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, |
There was a problem hiding this comment.
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
http: Optional[HttpRequests] = None, | |
session: Optional[Session] = None, |
meilisearch/index.py
Outdated
@@ -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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self.http = http if http is not None else HttpRequests(config) | |
self.session = session if session else Session() |
meilisearch/client.py
Outdated
@@ -130,6 +130,7 @@ def get_indexes(self, parameters: Optional[Mapping[str, Any]] = None) -> Dict[st | |||
index["primaryKey"], | |||
index["createdAt"], | |||
index["updatedAt"], | |||
self.http, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self.http, | |
self.session = Session() |
meilisearch/client.py
Outdated
@@ -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() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return Index(self.config, uid, http=self.http).fetch_info() | |
return Index(self.config, uid, http=self.session).fetch_info() |
meilisearch/client.py
Outdated
@@ -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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return Index(self.config, uid=uid, http=self.http) | |
return Index(self.config, uid=uid, http=self.session) |
meilisearch/_httprequests.py
Outdated
@@ -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() |
There was a problem hiding this comment.
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.
self.session = requests.Session() | |
self.session = session |
Currently trying to apply your changes, but not passing around the HttpRequests also affects how tasks are created. Perhaps it is easier to: global_session = None
def get_global_session():
global global_session
if global_session:
return global_session
global_session = requests.Session()
return global_session
class HttpRequests:
def __init__(self, config: Config) -> None:
self.config = config
self.headers = {
"Authorization": f"Bearer {self.config.api_key}",
"User-Agent": _build_user_agent(config.client_agents),
}
self.session = get_global_session() This way when creating HttpRequests it just fetches the session. |
Good point. I think what we could do is have the |
Another option would be to create a http sigelton in |
@sanders41 I think the singleton pattern is probably the best choice here. Don't see any issues with this implementation. |
Closing this for lack of activity and the tests are still failing. Feel free to re-open of course if you want to keep working on it |
Pull Request
Related issue
Fixes #<issue_number>
What does this PR do?
PR checklist
Please check if your PR fulfills the following requirements: