-
Notifications
You must be signed in to change notification settings - Fork 157
perf(client): add session pooling for connection reuse TAV-4105 #151
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,6 +38,11 @@ def __init__(self, api_key: Optional[str] = None, proxies: Optional[dict[str, st | |
| **({"X-Project-ID": tavily_project} if tavily_project else {}) | ||
| } | ||
|
|
||
| self.session = requests.Session() | ||
| self.session.headers.update(self.headers) | ||
| if self.proxies: | ||
| self.session.proxies.update(self.proxies) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Session pooling introduced without cleanup mechanismLow Severity The |
||
|
|
||
| def _search(self, | ||
| query: str, | ||
| search_depth: Literal["basic", "advanced", "fast", "ultra-fast"] = None, | ||
|
|
@@ -91,7 +96,7 @@ def _search(self, | |
| timeout = min(timeout, 120) | ||
|
|
||
| try: | ||
| response = requests.post(self.base_url + "/search", data=json.dumps(data), headers=self.headers, timeout=timeout, proxies=self.proxies) | ||
| response = self.session.post(self.base_url + "/search", data=json.dumps(data), timeout=timeout) | ||
| except requests.exceptions.Timeout: | ||
| raise TimeoutError(timeout) | ||
|
|
||
|
|
@@ -202,7 +207,7 @@ def _extract(self, | |
| data.update(kwargs) | ||
|
|
||
| try: | ||
| response = requests.post(self.base_url + "/extract", data=json.dumps(data), headers=self.headers, timeout=timeout, proxies=self.proxies) | ||
| response = self.session.post(self.base_url + "/extract", data=json.dumps(data), timeout=timeout) | ||
| except requests.exceptions.Timeout: | ||
| raise TimeoutError(timeout) | ||
|
|
||
|
|
@@ -310,8 +315,7 @@ def _crawl(self, | |
| data = {k: v for k, v in data.items() if v is not None} | ||
|
|
||
| try: | ||
| response = requests.post( | ||
| self.base_url + "/crawl", data=json.dumps(data), headers=self.headers, timeout=timeout, proxies=self.proxies) | ||
| response = self.session.post(self.base_url + "/crawl", data=json.dumps(data), timeout=timeout) | ||
| except requests.exceptions.Timeout: | ||
| raise TimeoutError(timeout) | ||
|
|
||
|
|
@@ -421,8 +425,7 @@ def _map(self, | |
| data = {k: v for k, v in data.items() if v is not None} | ||
|
|
||
| try: | ||
| response = requests.post( | ||
| self.base_url + "/map", data=json.dumps(data), headers=self.headers, timeout=timeout, proxies=self.proxies) | ||
| response = self.session.post(self.base_url + "/map", data=json.dumps(data), timeout=timeout) | ||
| except requests.exceptions.Timeout: | ||
| raise TimeoutError(timeout) | ||
|
|
||
|
|
@@ -631,12 +634,10 @@ def _research(self, | |
|
|
||
| if stream: | ||
| try: | ||
| response = requests.post( | ||
| response = self.session.post( | ||
| self.base_url + "/research", | ||
| data=json.dumps(data), | ||
| headers=self.headers, | ||
| timeout=timeout, | ||
| proxies=self.proxies, | ||
| stream=True | ||
| ) | ||
| except requests.exceptions.Timeout: | ||
|
|
@@ -671,12 +672,10 @@ def stream_generator() -> Generator[bytes, None, None]: | |
| return stream_generator() | ||
| else: | ||
| try: | ||
| response = requests.post( | ||
| response = self.session.post( | ||
| self.base_url + "/research", | ||
| data=json.dumps(data), | ||
| headers=self.headers, | ||
| timeout=timeout, | ||
| proxies=self.proxies | ||
| timeout=timeout | ||
| ) | ||
| except requests.exceptions.Timeout: | ||
| raise TimeoutError(timeout) | ||
|
|
@@ -752,11 +751,7 @@ def get_research(self, | |
| dict: Research response containing request_id, created_at, completed_at, status, content, and sources. | ||
| """ | ||
| try: | ||
| response = requests.get( | ||
| self.base_url + f"/research/{request_id}", | ||
| headers=self.headers, | ||
| proxies=self.proxies, | ||
| ) | ||
| response = self.session.get(self.base_url + f"/research/{request_id}") | ||
| except Exception as e: | ||
| raise Exception(f"Error getting research: {e}") | ||
|
|
||
|
|
||
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.
Shared session used concurrently is not thread-safe
Medium Severity
The new
requests.Sessionobject is shared across threads inget_company_info, which usesThreadPoolExecutorto make parallel API calls.requests.Sessionis not documented as thread-safe, and the previous implementation using independentrequests.post()calls was explicitly safe for concurrent use. This change introduces potential race conditions when multiple threads access the session simultaneously.Additional Locations (1)
tavily/tavily.py#L590-L594