Fix backend db session leak, pass session to filter

This commit is contained in:
nolash 2021-02-18 23:55:49 +01:00
parent 7718c8e3d4
commit c509dd8f6c
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
4 changed files with 24 additions and 8 deletions

View File

@ -32,7 +32,8 @@ class SyncerBackend:
def connect(self):
"""Loads the state of the syncer session with the given id.
"""
self.db_session = SessionBase.create_session()
if self.db_session == None:
self.db_session = SessionBase.create_session()
q = self.db_session.query(BlockchainSync)
q = q.filter(BlockchainSync.id==self.object_id)
self.db_object = q.first()
@ -46,6 +47,7 @@ class SyncerBackend:
self.db_session.add(self.db_object)
self.db_session.commit()
self.db_session.close()
self.db_session = None
def chain(self):

View File

@ -28,7 +28,7 @@ class Syncer:
self.cursor = None
self.running = True
self.backend = backend
self.filter = SyncFilter()
self.filter = SyncFilter(backend)
self.progress_callback = progress_callback

View File

@ -6,3 +6,7 @@ class LoopDone(Exception):
class RequestError(Exception):
pass
class BackendError(Exception):
pass

View File

@ -1,13 +1,17 @@
# standard imports
import logging
# local imports
from .error import BackendError
logg = logging.getLogger(__name__)
class SyncFilter:
def __init__(self, safe=True):
def __init__(self, backend, safe=True):
self.safe = safe
self.filters = []
self.backend = backend
def add(self, fltr):
@ -19,15 +23,21 @@ class SyncFilter:
def apply(self, conn, block, tx):
session = None
try:
session = self.backend.connect()
except sqlalchemy.exc.TimeoutError as e:
self.backend.disconnect()
raise BackendError('database connection fail: {}'.format(e))
for f in self.filters:
logg.debug('applying filter {}'.format(str(f)))
f.filter(conn, block, tx)
f.filter(conn, block, tx, self.backend.db_session)
self.backend.disconnect()
class NoopFilter(SyncFilter):
class NoopFilter:
def filter(self, conn, block, tx):
logg.debug('noop filter :received\n{} {}'.format(block, tx))
def filter(self, conn, block, tx, db_session=None):
logg.debug('noop filter :received\n{} {} {}'.format(block, tx, id(db_session)))
def __str__(self):