fix: add thread-safe service cache in manager

This commit is contained in:
sankalp committed 2026-10-04 07:24:02 +05:45
1 parent a67de8d16b
commit 8022f37ae2
1 file changed
+4
+4
View File
@@ -1,4 +1,5 @@
from typing import Any, TypeVar
import threading
from nepali_payment.enums import PaymentMethod, PaymentMode
from nepali_payment.exceptions import ValidationError
@@ -45,6 +46,7 @@ class PaymentManager:
self._api = api
self._config = config
self._service_cache = None
self._lock = threading.Lock()
def _api_service(self):
"""Return the shared HTTP client, creating it on the first call.
@@ -65,6 +67,8 @@ class PaymentManager:
ConnectIPS it avoids re-parsing the RSA certificate on every call) and
keeps a stable HTTP client across operations.
"""
if self._service_cache is None:
with self._lock:
if self._service_cache is None:
self._service_cache = get_payment_service(
self._method, self._secret_key, self._mode, api=self._api_service(), config=self._config