Skip to content

Commit f27ce06

Browse files
committed
review fixes
1 parent 1d26890 commit f27ce06

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

ydb/query/pool.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,15 @@ def __init__(self, driver: common_utils.SupportedDriverType, size: int = 100):
4040
self._should_stop = threading.Event()
4141
self._lock = threading.RLock()
4242

43-
def _create_new_session(self, timeout: float):
43+
def _create_new_session(self, timeout: Optional[float]):
4444
session = QuerySession(self._driver)
4545
session.create(settings=BaseRequestSettings().with_timeout(timeout))
4646
logger.debug(f"New session was created for pool. Session id: {session._state.session_id}")
4747
return session
4848

49-
def acquire(self, timeout: float) -> QuerySession:
50-
acquired = self._lock.acquire(timeout=timeout)
49+
def acquire(self, timeout: Optional[float] = None) -> QuerySession:
50+
lock_acquire_timeout = timeout if timeout is not None else -1
51+
acquired = self._lock.acquire(timeout=lock_acquire_timeout)
5152
try:
5253
if self._should_stop.is_set():
5354
logger.error("An attempt to take session from closed session pool.")
@@ -76,7 +77,7 @@ def acquire(self, timeout: float) -> QuerySession:
7677

7778
logger.debug(f"Session pool is not large enough: {self._current_size} < {self._size}, will create new one.")
7879
finish = time.monotonic()
79-
time_left = timeout - (finish - start)
80+
time_left = timeout - (finish - start) if timeout is not None else None
8081
session = self._create_new_session(time_left)
8182

8283
self._current_size += 1
@@ -89,7 +90,7 @@ def release(self, session: QuerySession) -> None:
8990
self._queue.put_nowait(session)
9091
logger.debug("Session returned to queue: %s", session._state.session_id)
9192

92-
def checkout(self, timeout: float = 10) -> "SimpleQuerySessionCheckout":
93+
def checkout(self, timeout: Optional[float] = None) -> "SimpleQuerySessionCheckout":
9394
"""WARNING: This API is experimental and could be changed.
9495
Return a Session context manager, that opens session on enter and closes session on exit.
9596
"""
@@ -109,7 +110,7 @@ def retry_operation_sync(self, callee: Callable, retry_settings: Optional[RetryS
109110
retry_settings = RetrySettings() if retry_settings is None else retry_settings
110111

111112
def wrapped_callee():
112-
with self.checkout() as session:
113+
with self.checkout(timeout=retry_settings.max_session_acquire_timeout) as session:
113114
return callee(session, *args, **kwargs)
114115

115116
return retry_operation_sync(wrapped_callee, retry_settings)
@@ -137,14 +138,15 @@ def execute_with_retries(
137138
retry_settings = RetrySettings() if retry_settings is None else retry_settings
138139

139140
def wrapped_callee():
140-
with self.checkout() as session:
141+
with self.checkout(timeout=retry_settings.max_session_acquire_timeout) as session:
141142
it = session.execute(query, parameters, *args, **kwargs)
142143
return [result_set for result_set in it]
143144

144145
return retry_operation_sync(wrapped_callee, retry_settings)
145146

146-
def stop(self, timeout=-1):
147-
acquired = self._lock.acquire(timeout=timeout)
147+
def stop(self, timeout=None):
148+
acquire_timeout = timeout if timeout is not None else -1
149+
acquired = self._lock.acquire(timeout=acquire_timeout)
148150
try:
149151
self._should_stop.set()
150152
while True:
@@ -170,7 +172,7 @@ def __del__(self):
170172

171173

172174
class SimpleQuerySessionCheckout:
173-
def __init__(self, pool: QuerySessionPool, timeout: float):
175+
def __init__(self, pool: QuerySessionPool, timeout: Optional[float]):
174176
self._pool = pool
175177
self._timeout = timeout
176178
self._session = None

0 commit comments

Comments
 (0)