server/db: make query counter thread-local
This commit is contained in:
parent
c64453a15c
commit
280a55046a
3 changed files with 22 additions and 20 deletions
|
@ -1,29 +1,27 @@
|
|||
import threading
|
||||
import sqlalchemy
|
||||
from szurubooru import config
|
||||
|
||||
|
||||
class QueryCounter(object):
|
||||
_query_count = 0
|
||||
|
||||
@staticmethod
|
||||
def bump():
|
||||
QueryCounter._query_count += 1
|
||||
|
||||
@staticmethod
|
||||
def reset():
|
||||
QueryCounter._query_count = 0
|
||||
|
||||
@staticmethod
|
||||
def get():
|
||||
return QueryCounter._query_count
|
||||
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
_engine = sqlalchemy.create_engine(config.config['database'])
|
||||
sessionmaker = sqlalchemy.orm.sessionmaker(bind=_engine)
|
||||
session = sqlalchemy.orm.scoped_session(sessionmaker)
|
||||
reset_query_count = QueryCounter.reset
|
||||
get_query_count = QueryCounter.get
|
||||
|
||||
_data = threading.local()
|
||||
|
||||
|
||||
def reset_query_count():
|
||||
_data.query_count = 0
|
||||
|
||||
|
||||
def get_query_count():
|
||||
return _data.query_count
|
||||
|
||||
|
||||
def _bump_query_count():
|
||||
_data.query_count = getattr(_data, 'query_count', 0) + 1
|
||||
|
||||
|
||||
sqlalchemy.event.listen(
|
||||
_engine, 'after_execute', lambda *args: QueryCounter.bump())
|
||||
_engine, 'after_execute', lambda *args: _bump_query_count())
|
||||
|
|
|
@ -5,7 +5,6 @@ from szurubooru.rest import middleware
|
|||
@middleware.pre_hook
|
||||
def _process_request(ctx):
|
||||
ctx.session = db.session()
|
||||
db.reset_query_count()
|
||||
|
||||
|
||||
@middleware.post_hook
|
||||
|
|
|
@ -6,6 +6,11 @@ from szurubooru.rest import middleware
|
|||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@middleware.pre_hook
|
||||
def process_request(_ctx):
|
||||
db.reset_query_count()
|
||||
|
||||
|
||||
@middleware.post_hook
|
||||
def process_response(ctx):
|
||||
logger.info(
|
||||
|
|
Loading…
Reference in a new issue