Refactored get_connection() method of ConnectionPool.
@@ -882,15 +882,11 @@ class ConnectionPool(ABC, Generic[TConnection]):
# print("writer getting connection")
with self._get_semaphore:
- if is_writer is not None:
+ if is_writer is True:
remaining = self._remaining_timeout(timeout, started)
- if self._mutually_exclusive_read_write:
if not self._writer_lock.acquire(timeout=remaining):
- if is_writer:
raise WriterBlockedByWriter()
- else:
- raise ReaderBlockedByWriter()
- if is_writer:
+ if self._mutually_exclusive_read_write:
with self._no_readers:
if self._num_readers > 0:
remaining = self._remaining_timeout(timeout, started)
@@ -898,17 +894,20 @@ class ConnectionPool(ABC, Generic[TConnection]):
if not self._no_readers.wait(timeout=remaining):
self._writer_lock.release()
raise WriterBlockedByReaders()
- else:
+ elif is_writer is False:
+ remaining = self._remaining_timeout(timeout, started)
+ if self._mutually_exclusive_read_write:
+ if not self._writer_lock.acquire(timeout=remaining):
+ raise ReaderBlockedByWriter()
with self._no_readers:
self._num_readers += 1
self._writer_lock.release()
- elif is_writer:
- if not self._writer_lock.acquire(timeout=remaining):
- raise WriterBlockedByWriter()
conn = self._get_connection(
timeout=self._remaining_timeout(timeout, started)
)
+ if is_writer is not None:
+ conn.is_writer = is_writer
# if is_writer is None:
# for_writer = ""
@@ -925,9 +924,6 @@ class ConnectionPool(ABC, Generic[TConnection]):
# "used",
# )
- if is_writer is not None:
- conn.is_writer = is_writer
-
return conn
def _get_connection(self, timeout: float = 0.0) -> TConnection:
@@ -1003,10 +999,9 @@ class ConnectionPool(ABC, Generic[TConnection]):
finally:
conn.in_use.release()
- if is_writer is not None:
- if is_writer:
+ if is_writer is True:
self._writer_lock.release()
- elif self._mutually_exclusive_read_write:
+ elif is_writer is False and self._mutually_exclusive_read_write:
with self._no_readers:
self._num_readers -= 1
if self._num_readers == 0:
