Skip to content

Commit

Permalink
Do not initialize a new client for each operation, but use a thread l…
Browse files Browse the repository at this point in the history
…ocal cached one.
  • Loading branch information
jensens committed Nov 16, 2023
1 parent 622a86b commit e838b29
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ Changelog
- Add tox, Github Actions, CI and CD. [jensens]
- Refactor field-map loading to not happen on startup. [jensens]
- Remove Support for OpenSearch 1.x and ElasticSearch < 8 [jensens]
- rename .elastic.get_ingest_client to .client.get_client [jensens]
- Rename .elastic.get_ingest_client to .client.get_client [jensens]
- Do not initialize a new client for each operation, but use a thread local cached one.
This speeds up indexing a lot. [jensens]


1.4 (2023-08-17)
Expand Down
7 changes: 7 additions & 0 deletions src/collective/elastic/ingest/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@
from .logging import logger

import os
import threading


if OPENSEARCH:
from opensearchpy import OpenSearch
else:
from elasticsearch import Elasticsearch

_local_storage = threading.local()

def get_client(index_server_baseurl: str = ""):
"""index client for query or ingest
either OpenSearch or Elasticsearch client, depending on OPENSEARCH env var
"""

client = getattr(_local_storage, "client", None)
if client is not None:
return _local_storage.client

raw_addr = index_server_baseurl or os.environ.get("INDEX_SERVER", "")
use_ssl = bool(int(os.environ.get("INDEX_USE_SSL", "0")))
addresses = [x for x in raw_addr.split(",") if x.strip()]
Expand Down Expand Up @@ -50,4 +56,5 @@ def get_client(index_server_baseurl: str = ""):
basic_auth=auth,
verify_certs=False,
)
setattr(_local_storage, "client", client)
return client

0 comments on commit e838b29

Please sign in to comment.