Skip to content

Commit

Permalink
Simplify and add checks for simple code
Browse files Browse the repository at this point in the history
  • Loading branch information
Cito committed Apr 29, 2023
1 parent 236b581 commit 13fffe3
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 122 deletions.
33 changes: 16 additions & 17 deletions dbutils/pooled_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
Licensed under the MIT license.
"""

from contextlib import suppress
from functools import total_ordering
from threading import Condition

Expand Down Expand Up @@ -349,11 +350,10 @@ def unshare(self, con):
with self._lock:
con.unshare()
shared = con.shared
if not shared: # connection is idle,
try: # so try to remove it
self._shared_cache.remove(con) # from shared cache
except ValueError:
pass # pool has already been closed
if not shared: # connection is idle
# try to remove it from shared cache
with suppress(ValueError): # if pool has already been closed
self._shared_cache.remove(con)
if not shared: # connection has become idle,
self.cache(con.con) # so add it to the idle cache

Expand All @@ -374,25 +374,22 @@ def close(self):
with self._lock:
while self._idle_cache: # close all idle connections
con = self._idle_cache.pop(0)
try:
with suppress(Exception):
con.close()
except Exception:
pass
if self._maxshared: # close all shared connections
while self._shared_cache:
con = self._shared_cache.pop(0).con
try:
with suppress(Exception):
con.close()
except Exception:
pass
self._connections -= 1
self._lock.notify_all()

def __del__(self):
"""Delete the pool."""
try:
# builtins (including Exceptions) might not exist anymore
try: # noqa: SIM105
self.close()
except: # noqa: E722 - builtin Exceptions might not exist anymore
except: # noqa: E722, S110
pass

def _wait_lock(self):
Expand Down Expand Up @@ -437,9 +434,10 @@ def __getattr__(self, name):

def __del__(self):
"""Delete the pooled connection."""
try:
# builtins (including Exceptions) might not exist anymore
try: # noqa: SIM105
self.close()
except: # noqa: E722 - builtin Exceptions might not exist anymore
except: # noqa: E722, S110
pass

def __enter__(self):
Expand Down Expand Up @@ -518,9 +516,10 @@ def __getattr__(self, name):

def __del__(self):
"""Delete the pooled connection."""
try:
# builtins (including Exceptions) might not exist anymore
try: # noqa: SIM105
self.close()
except: # noqa: E722 - builtin Exceptions might not exist anymore
except: # noqa: E722, S110
pass

def __enter__(self):
Expand Down
29 changes: 13 additions & 16 deletions dbutils/pooled_pg.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
Licensed under the MIT license.
"""

from contextlib import suppress
from queue import Empty, Full, Queue

from . import __version__
Expand Down Expand Up @@ -186,9 +187,8 @@ def __init__(
maxcached = 0
if maxconnections is None:
maxconnections = 0
if maxcached:
if maxcached < mincached:
maxcached = mincached
if maxcached and maxcached < mincached:
maxcached = mincached
if maxconnections:
if maxconnections < maxcached:
maxconnections = maxcached
Expand All @@ -211,9 +211,8 @@ def steady_connection(self):

def connection(self):
"""Get a steady, cached PostgreSQL connection from the pool."""
if self._connections:
if not self._connections.acquire(self._blocking):
raise TooManyConnectionsError
if self._connections and not self._connections.acquire(self._blocking):
raise TooManyConnectionsError
try:
con = self._cache.get_nowait()
except Empty:
Expand All @@ -226,10 +225,8 @@ def cache(self, con):
if self._reset == RESET_COMPLETELY:
con.reset() # reset the connection completely
elif self._reset == RESET_ALWAYS_ROLLBACK or con._transaction:
try:
with suppress(Exception):
con.rollback() # rollback a possible transaction
except Exception:
pass
self._cache.put_nowait(con) # and then put it back into the cache
except Full:
con.close()
Expand All @@ -241,20 +238,19 @@ def close(self):
while 1:
try:
con = self._cache.get_nowait()
try:
with suppress(Exception):
con.close()
except Exception:
pass
if self._connections:
self._connections.release()
except Empty:
break

def __del__(self):
"""Delete the pool."""
try:
# builtins (including Exceptions) might not exist anymore
try: # noqa: SIM105
self.close()
except: # noqa: E722 - builtin Exceptions might not exist anymore
except: # noqa: E722, S110
pass


Expand Down Expand Up @@ -298,9 +294,10 @@ def __getattr__(self, name):

def __del__(self):
"""Delete the pooled connection."""
try:
# builtins (including Exceptions) might not exist anymore
try: # noqa: SIM105
self.close()
except: # noqa: E722 - builtin Exceptions might not exist anymore
except: # noqa: E722, S110
pass

def __enter__(self):
Expand Down
Loading

0 comments on commit 13fffe3

Please sign in to comment.