mirror of https://github.com/fail2ban/fail2ban
explicitly close cursor if not needed anymore (GC can grab it late)
parent
45ef36276f
commit
485c50228a
|
@ -104,7 +104,11 @@ def commitandrollback(f):
|
||||||
def wrapper(self, *args, **kwargs):
|
def wrapper(self, *args, **kwargs):
|
||||||
with self._lock: # Threading lock
|
with self._lock: # Threading lock
|
||||||
with self._db: # Auto commit and rollback on exception
|
with self._db: # Auto commit and rollback on exception
|
||||||
return f(self, self._db.cursor(), *args, **kwargs)
|
cur = self._db.cursor()
|
||||||
|
try:
|
||||||
|
return f(self, cur, *args, **kwargs)
|
||||||
|
finally:
|
||||||
|
cur.close()
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
|
@ -747,34 +751,36 @@ class Fail2BanDb(object):
|
||||||
query += " GROUP BY ip ORDER BY ip, timeofban DESC"
|
query += " GROUP BY ip ORDER BY ip, timeofban DESC"
|
||||||
else:
|
else:
|
||||||
query += " ORDER BY timeofban DESC LIMIT 1"
|
query += " ORDER BY timeofban DESC LIMIT 1"
|
||||||
cur = self._db.cursor()
|
|
||||||
return cur.execute(query, queryArgs)
|
return cur.execute(query, queryArgs)
|
||||||
|
|
||||||
def getCurrentBans(self, jail = None, ip = None, forbantime=None, fromtime=None, maxmatches=None):
|
def getCurrentBans(self, jail = None, ip = None, forbantime=None, fromtime=None, maxmatches=None):
|
||||||
tickets = []
|
tickets = []
|
||||||
ticket = None
|
ticket = None
|
||||||
|
|
||||||
with self._lock:
|
try:
|
||||||
results = list(self._getCurrentBans(self._db.cursor(),
|
with self._lock:
|
||||||
jail=jail, ip=ip, forbantime=forbantime, fromtime=fromtime))
|
cur = self._db.cursor()
|
||||||
|
results = list(self._getCurrentBans(self._db.cursor(),
|
||||||
if results:
|
jail=jail, ip=ip, forbantime=forbantime, fromtime=fromtime))
|
||||||
for banip, timeofban, data in results:
|
|
||||||
# logSys.debug('restore ticket %r, %r, %r', banip, timeofban, data)
|
|
||||||
ticket = FailTicket(banip, timeofban, data=data)
|
|
||||||
# filter matches if expected (current count > as maxmatches specified):
|
|
||||||
if maxmatches is None:
|
|
||||||
maxmatches = self.maxMatches
|
|
||||||
if maxmatches:
|
|
||||||
matches = ticket.getMatches()
|
|
||||||
if matches and len(matches) > maxmatches:
|
|
||||||
ticket.setMatches(matches[-maxmatches:])
|
|
||||||
else:
|
|
||||||
ticket.setMatches(None)
|
|
||||||
# logSys.debug('restored ticket: %r', ticket)
|
|
||||||
if ip is not None: return ticket
|
|
||||||
tickets.append(ticket)
|
|
||||||
|
|
||||||
|
if results:
|
||||||
|
for banip, timeofban, data in results:
|
||||||
|
# logSys.debug('restore ticket %r, %r, %r', banip, timeofban, data)
|
||||||
|
ticket = FailTicket(banip, timeofban, data=data)
|
||||||
|
# filter matches if expected (current count > as maxmatches specified):
|
||||||
|
if maxmatches is None:
|
||||||
|
maxmatches = self.maxMatches
|
||||||
|
if maxmatches:
|
||||||
|
matches = ticket.getMatches()
|
||||||
|
if matches and len(matches) > maxmatches:
|
||||||
|
ticket.setMatches(matches[-maxmatches:])
|
||||||
|
else:
|
||||||
|
ticket.setMatches(None)
|
||||||
|
# logSys.debug('restored ticket: %r', ticket)
|
||||||
|
if ip is not None: return ticket
|
||||||
|
tickets.append(ticket)
|
||||||
|
finally:
|
||||||
|
cur.close()
|
||||||
return tickets
|
return tickets
|
||||||
|
|
||||||
@commitandrollback
|
@commitandrollback
|
||||||
|
|
Loading…
Reference in New Issue