-
Notifications
You must be signed in to change notification settings - Fork 186
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
Align pool_maxsize for different connection pool implementations. #535
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
- [Connection Classes](#connection-classes) | ||
- [Selecting a Connection Class](#selecting-a-connection-class) | ||
- [Urllib3HttpConnection](#urllib3httpconnection) | ||
- [RequestsHttpConnection](#requestshttpconnection) | ||
- [AsyncHttpConnection](#asynchttpconnection) | ||
- [Connection Pooling](#connection-pooling) | ||
|
||
# Connection Classes | ||
|
||
The OpenSearch Python synchronous client supports both the `Urllib3HttpConnection` connection class (default) from the [urllib3](https://pypi.org/project/urllib3/) library, and `RequestsHttpConnection` from the [requests](https://pypi.org/project/requests/) library. We recommend you use the default, unless your application is standardized on `requests`. | ||
|
||
The faster, asynchronous client, implements a class called `AsyncHttpConnection`, which uses [aiohttp](https://pypi.org/project/aiohttp/). | ||
|
||
## Selecting a Connection Class | ||
|
||
### Urllib3HttpConnection | ||
|
||
```python | ||
from opensearchpy import OpenSearch, Urllib3HttpConnection | ||
|
||
client = OpenSearch( | ||
hosts = [{'host': 'localhost', 'port': 9200}], | ||
http_auth = ('admin', 'admin'), | ||
use_ssl = True, | ||
verify_certs = False, | ||
ssl_show_warn = False, | ||
connection_class = Urllib3HttpConnection | ||
) | ||
``` | ||
|
||
### RequestsHttpConnection | ||
|
||
```python | ||
from opensearchpy import OpenSearch, RequestsHttpConnection | ||
|
||
client = OpenSearch( | ||
hosts = [{'host': 'localhost', 'port': 9200}], | ||
http_auth = ('admin', 'admin'), | ||
use_ssl = True, | ||
verify_certs = False, | ||
ssl_show_warn = False, | ||
connection_class = RequestsHttpConnection | ||
) | ||
``` | ||
|
||
### AsyncHttpConnection | ||
|
||
```python | ||
from opensearchpy import AsyncOpenSearch, AsyncHttpConnection | ||
|
||
async def main(): | ||
client = AsyncOpenSearch( | ||
hosts = [{'host': 'localhost', 'port': 9200}], | ||
http_auth = ('admin', 'admin'), | ||
use_ssl = True, | ||
verify_certs = False, | ||
ssl_show_warn = False, | ||
connection_class = AsyncHttpConnection | ||
) | ||
``` | ||
|
||
## Connection Pooling | ||
|
||
The OpenSearch Python client has a connection pool for each `host` value specified during initialization, and a connection pool for HTTP connections to each host implemented in the underlying HTTP libraries. You can adjust the max size of the latter connection pool with `pool_maxsize`. | ||
|
||
If you don't set this value, each connection library implementation will provide its default, which is typically `10`. Changing the pool size may improve performance in some multithreaded scenarios. | ||
|
||
The following example sets the number of connections in the connection pool to 12. | ||
|
||
```python | ||
from opensearchpy import OpenSearch | ||
|
||
client = OpenSearch( | ||
hosts = [{'host': 'localhost', 'port': 9200}], | ||
http_auth = ('admin', 'admin'), | ||
use_ssl = True, | ||
verify_certs = False, | ||
ssl_show_warn = False, | ||
pool_maxsize = 12, | ||
) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# The OpenSearch Contributors require contributions made to | ||
# this file be licensed under the Apache-2.0 license or a | ||
# compatible open source license. | ||
# | ||
# Modifications Copyright OpenSearch Contributors. See | ||
# GitHub history for details. | ||
|
||
from unittest import TestCase | ||
|
||
from opensearchpy import OpenSearch, RequestsHttpConnection | ||
|
||
|
||
class TestRequests(TestCase): | ||
def test_connection_class(self): | ||
client = OpenSearch(connection_class=RequestsHttpConnection) | ||
self.assertEqual(client.transport.pool_maxsize, None) | ||
self.assertEqual(client.transport.connection_class, RequestsHttpConnection) | ||
self.assertIsInstance( | ||
client.transport.connection_pool.connections[0], RequestsHttpConnection | ||
) | ||
|
||
def test_pool_maxsize(self): | ||
client = OpenSearch(connection_class=RequestsHttpConnection, pool_maxsize=42) | ||
self.assertEqual(client.transport.pool_maxsize, 42) | ||
self.assertEqual( | ||
client.transport.connection_pool.connections[0] | ||
.session.adapters["https://"] | ||
._pool_maxsize, | ||
42, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# The OpenSearch Contributors require contributions made to | ||
# this file be licensed under the Apache-2.0 license or a | ||
# compatible open source license. | ||
# | ||
# Modifications Copyright OpenSearch Contributors. See | ||
# GitHub history for details. | ||
|
||
from unittest import TestCase | ||
|
||
from urllib3.connectionpool import HTTPConnectionPool | ||
|
||
from opensearchpy import OpenSearch, Urllib3HttpConnection | ||
|
||
|
||
class TestUrlLib3(TestCase): | ||
def test_default(self): | ||
client = OpenSearch() | ||
self.assertEqual(client.transport.connection_class, Urllib3HttpConnection) | ||
self.assertEqual(client.transport.pool_maxsize, None) | ||
|
||
def test_connection_class(self): | ||
client = OpenSearch(connection_class=Urllib3HttpConnection) | ||
self.assertEqual(client.transport.connection_class, Urllib3HttpConnection) | ||
self.assertIsInstance( | ||
client.transport.connection_pool.connections[0], Urllib3HttpConnection | ||
) | ||
self.assertIsInstance( | ||
client.transport.connection_pool.connections[0].pool, HTTPConnectionPool | ||
) | ||
|
||
def test_pool_maxsize(self): | ||
client = OpenSearch(connection_class=Urllib3HttpConnection, pool_maxsize=42) | ||
self.assertEqual(client.transport.pool_maxsize, 42) | ||
# https://github.com/python/cpython/blob/3.12/Lib/queue.py#L35 | ||
self.assertEqual( | ||
client.transport.connection_pool.connections[0].pool.pool.maxsize, 42 | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should we make
Urllib3HttpConnection
default in code unless aconnection_class
is explicitly passed in?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.
It's already default here:
opensearch-py/opensearchpy/transport.py
Line 70 in 84ac172
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.
Ah I missed it, so only the documentation was a bit misleading.