Skip to content

Commit

Permalink
allows to init requests helper without runtime injection, uses re-ent…
Browse files Browse the repository at this point in the history
…rant locks when injecting context
  • Loading branch information
rudolfix committed Oct 14, 2024
1 parent 0dc4d2f commit 281559a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
5 changes: 3 additions & 2 deletions dlt/common/configuration/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Container:

thread_contexts: Dict[int, Dict[Type[ContainerInjectableContext], ContainerInjectableContext]]
"""A thread aware mapping of injection context """
_context_container_locks: Dict[str, threading.Lock]
_context_container_locks: Dict[str, threading.RLock]
"""Locks for container types on threads."""

main_context: Dict[Type[ContainerInjectableContext], ContainerInjectableContext]
Expand Down Expand Up @@ -159,8 +159,9 @@ def injectable_context(
if lock_context:
lock_key = f"{id(context)}"
if (lock := self._context_container_locks.get(lock_key)) is None:
# use multi-entrant locks so same thread can acquire this context several times
with Container._LOCK:
self._context_container_locks[lock_key] = lock = threading.Lock()
self._context_container_locks[lock_key] = lock = threading.RLock()
else:
lock = nullcontext()

Expand Down
6 changes: 5 additions & 1 deletion dlt/common/runtime/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def initialize_runtime(
)

# Init or update default requests client config
requests.init(runtime_config)
# requests.init(runtime_config)


def restore_run_context(
Expand All @@ -46,6 +46,10 @@ def restore_run_context(
def init_telemetry(runtime_config: RuntimeConfiguration) -> None:
"""Starts telemetry only once"""
from dlt.common.runtime.telemetry import start_telemetry
from dlt.sources.helpers import requests

# Init or update default requests client config
requests.init(runtime_config)

global _INITIALIZED
# initialize only once
Expand Down
6 changes: 5 additions & 1 deletion dlt/sources/helpers/requests/retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,11 @@ def __init__(
)

@with_config(spec=RuntimeConfiguration)
def update_from_config(self, config: RuntimeConfiguration = ConfigValue) -> None:
def configure(self, config: RuntimeConfiguration = ConfigValue) -> None:
"""Update session/retry settings via injected RunConfiguration"""
self.update_from_config(config)

def update_from_config(self, config: RuntimeConfiguration) -> None:
"""Update session/retry settings from RunConfiguration"""
self._session_kwargs["timeout"] = config.request_timeout
self._retry_kwargs["backoff_factor"] = config.request_backoff_factor
Expand Down
2 changes: 1 addition & 1 deletion tests/sources/helpers/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def test_client_instance_with_config(existing_session: bool) -> None:
os.environ.update({key: str(value) for key, value in cfg.items()})

client = default_client if existing_session else Client()
client.update_from_config()
client.configure()

session = client.session
assert session.timeout == cfg["RUNTIME__REQUEST_TIMEOUT"]
Expand Down

0 comments on commit 281559a

Please sign in to comment.