Skip to content

Commit

Permalink
Document connection classes and settings.
Browse files Browse the repository at this point in the history
Signed-off-by: dblock <[email protected]>
  • Loading branch information
dblock committed Oct 12, 2023
1 parent 6461043 commit 35a8443
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 2 deletions.
1 change: 1 addition & 0 deletions USER_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ print(response)
- [Point in Time](guides/point_in_time.md)
- [Using a Proxy](guides/proxy.md)
- [Index Templates](guides/index_template.md)
- [Connection Classes](guides/connection_classes.md)

## Plugins

Expand Down
1 change: 0 additions & 1 deletion guides/auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ client = OpenSearch(
['htps://...'],
use_ssl=True,
verify_certs=True,
connection_class=RequestsHttpConnection,
http_auth=HTTPKerberosAuth(mutual_authentication=OPTIONAL)
)

Expand Down
81 changes: 81 additions & 0 deletions guides/connection_classes.md
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 synchrnous 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,
)
```
1 change: 0 additions & 1 deletion guides/proxy.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ OpenSearch(
hosts=["htps://..."],
use_ssl=True,
verify_certs=True,
connection_class=RequestsHttpConnection,
trust_env=True,
)
```
Expand Down

0 comments on commit 35a8443

Please sign in to comment.