added a default timeout to database connections

gunner/thrimbletrimmer-improvements
Christopher Usher 5 years ago
parent d1e2957198
commit 632c5fae2f

@ -25,9 +25,10 @@ class DBManager(object):
Returned conns are set to seralizable isolation level, autocommit, and use Returned conns are set to seralizable isolation level, autocommit, and use
NamedTupleCursor cursors.""" NamedTupleCursor cursors."""
def __init__(self, **connect_kwargs): def __init__(self, connect_timeout=30, **connect_kwargs):
patch_psycopg() patch_psycopg()
self.conns = [] self.conns = []
self.connect_timeout = connect_timeout
self.connect_kwargs = connect_kwargs self.connect_kwargs = connect_kwargs
# get a connection to test whether connection is working. # get a connection to test whether connection is working.
conn = self.get_conn() conn = self.get_conn()
@ -39,7 +40,8 @@ class DBManager(object):
def get_conn(self): def get_conn(self):
if self.conns: if self.conns:
return self.conns.pop(0) return self.conns.pop(0)
conn = psycopg2.connect(cursor_factory=psycopg2.extras.NamedTupleCursor, **self.connect_kwargs) conn = psycopg2.connect(cursor_factory=psycopg2.extras.NamedTupleCursor,
connect_timeout=self.connect_timeout, **self.connect_kwargs)
# We use serializable because it means less issues to think about, # We use serializable because it means less issues to think about,
# we don't care about the performance concerns and everything we do is easily retryable. # we don't care about the performance concerns and everything we do is easily retryable.
# This shouldn't matter in practice anyway since everything we're doing is either read-only # This shouldn't matter in practice anyway since everything we're doing is either read-only

Loading…
Cancel
Save