From 4993c3a8890f4145061ceb02fb65d677a3c604dc Mon Sep 17 00:00:00 2001 From: Aevann Date: Sun, 20 Aug 2023 05:00:35 +0300 Subject: [PATCH] remove auto-flush to reduce average lock duration --- files/helpers/config/const.py | 2 +- files/routes/asset_submissions.py | 20 ++++++++++---------- files/routes/hats.py | 6 +++--- files/routes/posts.py | 6 +++--- files/routes/users.py | 8 +++++--- files/routes/votes.py | 1 + 6 files changed, 23 insertions(+), 20 deletions(-) diff --git a/files/helpers/config/const.py b/files/helpers/config/const.py index ea9eb3132..9d2ee62d7 100644 --- a/files/helpers/config/const.py +++ b/files/helpers/config/const.py @@ -1156,4 +1156,4 @@ from sqlalchemy.engine.create import create_engine from sqlalchemy.orm import scoped_session, sessionmaker engine = create_engine(environ.get("DATABASE_URL").strip(), connect_args={"options": "-c statement_timeout=5000 -c idle_in_transaction_session_timeout=40000"}) -db_session = scoped_session(sessionmaker(bind=engine)) +db_session = scoped_session(sessionmaker(bind=engine, autoflush=False)) diff --git a/files/routes/asset_submissions.py b/files/routes/asset_submissions.py index f2014f429..7fd823420 100644 --- a/files/routes/asset_submissions.py +++ b/files/routes/asset_submissions.py @@ -154,30 +154,30 @@ def approve_emoji(v, name): if emoji.kind == "Marsey": all_by_author = g.db.query(Emoji).filter_by(kind="Marsey", author_id=author.id).count() - if all_by_author >= 100: + if all_by_author >= 99: badge_grant(badge_id=143, user=author) - elif all_by_author >= 10: + elif all_by_author >= 9: badge_grant(badge_id=16, user=author) else: badge_grant(badge_id=17, user=author) elif emoji.kind == "Capy": all_by_author = g.db.query(Emoji).filter_by(kind="Capy", author_id=author.id).count() - if all_by_author >= 10: + if all_by_author >= 9: badge_grant(badge_id=115, user=author) badge_grant(badge_id=114, user=author) elif emoji.kind == "Carp": all_by_author = g.db.query(Emoji).filter_by(kind="Carp", author_id=author.id).count() - if all_by_author >= 10: + if all_by_author >= 9: badge_grant(badge_id=288, user=author) badge_grant(badge_id=287, user=author) elif emoji.kind == "Wolf": all_by_author = g.db.query(Emoji).filter_by(kind="Wolf", author_id=author.id).count() - if all_by_author >= 10: + if all_by_author >= 9: badge_grant(badge_id=111, user=author) badge_grant(badge_id=110, user=author) elif emoji.kind == "Platy": all_by_author = g.db.query(Emoji).filter_by(kind="Platy", author_id=author.id).count() - if all_by_author >= 10: + if all_by_author >= 9: badge_grant(badge_id=113, user=author) badge_grant(badge_id=112, user=author) @@ -353,13 +353,13 @@ def approve_hat(v, name): all_by_author = g.db.query(HatDef).filter_by(author_id=author.id).count() - if all_by_author >= 250: + if all_by_author >= 249: badge_grant(badge_id=166, user=author) - elif all_by_author >= 100: + elif all_by_author >= 99: badge_grant(badge_id=165, user=author) - elif all_by_author >= 50: + elif all_by_author >= 49: badge_grant(badge_id=164, user=author) - elif all_by_author >= 10: + elif all_by_author >= 9: badge_grant(badge_id=163, user=author) hat_copy = Hat( diff --git a/files/routes/hats.py b/files/routes/hats.py index 735834eb9..79ee13202 100644 --- a/files/routes/hats.py +++ b/files/routes/hats.py @@ -106,11 +106,11 @@ def buy_hat(v, hat_id): f":marseycapitalistmanlet: @{v.username} has just bought `{hat.name}`, you have received your 10% cut ({int(hat.price * 0.1)} coins) :!marseycapitalistmanlet:" ) - if v.num_of_owned_hats >= 250: + if v.num_of_owned_hats >= 249: badge_grant(user=v, badge_id=154) - elif v.num_of_owned_hats >= 100: + elif v.num_of_owned_hats >= 99: badge_grant(user=v, badge_id=153) - elif v.num_of_owned_hats >= 25: + elif v.num_of_owned_hats >= 24: badge_grant(user=v, badge_id=152) return {"message": f"'{hat.name}' bought!"} diff --git a/files/routes/posts.py b/files/routes/posts.py index 2a1ab6152..94bde74d8 100644 --- a/files/routes/posts.py +++ b/files/routes/posts.py @@ -657,7 +657,7 @@ def submit_post(v, sub=None): autojanny.comment_count += 1 g.db.add(autojanny) - v.post_count = g.db.query(Post).filter_by(author_id=v.id, deleted_utc=0).count() + v.post_count += 1 g.db.add(v) execute_lawlz_actions(v, p) @@ -708,7 +708,7 @@ def delete_post_pid(pid, v): cache.delete_memoized(frontlist) cache.delete_memoized(userpagelisting) - v.post_count = g.db.query(Post).filter_by(author_id=v.id, deleted_utc=0).count() + v.post_count -= 1 g.db.add(v) for sort in COMMENT_SORTS: @@ -733,7 +733,7 @@ def undelete_post_pid(pid, v): cache.delete_memoized(frontlist) cache.delete_memoized(userpagelisting) - v.post_count = g.db.query(Post).filter_by(author_id=v.id, deleted_utc=0).count() + v.post_count += 1 g.db.add(v) for sort in COMMENT_SORTS: diff --git a/files/routes/users.py b/files/routes/users.py index aea073198..b0b19c6be 100644 --- a/files/routes/users.py +++ b/files/routes/users.py @@ -42,6 +42,7 @@ def claim_rewards_all_users(): emails = [x[0] for x in g.db.query(Transaction.email).filter_by(claimed=None)] users = g.db.query(User).filter(User.email.in_(emails)).order_by(User.truescore.desc()).all() for user in users: + g.db.flush() transactions = g.db.query(Transaction).filter_by(email=user.email, claimed=None).all() highest_tier = 0 @@ -81,6 +82,7 @@ def claim_rewards_all_users(): for x in range(22, badge_id+1): badge_grant(badge_id=x, user=user) + g.db.flush() user.lifetimedonated = g.db.query(func.sum(Transaction.amount)).filter_by(email=user.email).scalar() if user.lifetimedonated >= 100: @@ -1205,7 +1207,7 @@ def follow_user(username, v): new_follow = Follow(user_id=v.id, target_id=target.id) g.db.add(new_follow) - target.stored_subscriber_count = g.db.query(Follow).filter_by(target_id=target.id).count() + target.stored_subscriber_count += 1 g.db.add(target) if not v.shadowbanned: @@ -1229,7 +1231,7 @@ def unfollow_user(username, v): if follow: g.db.delete(follow) - target.stored_subscriber_count = g.db.query(Follow).filter_by(target_id=target.id).count() + target.stored_subscriber_count -= 1 g.db.add(target) if not v.shadowbanned: @@ -1256,7 +1258,7 @@ def remove_follow(username, v): g.db.delete(follow) - v.stored_subscriber_count = g.db.query(Follow).filter_by(target_id=v.id).count() + v.stored_subscriber_count -= 1 g.db.add(v) send_repeatable_notification(target.id, f"@{v.username} has removed your follow!") diff --git a/files/routes/votes.py b/files/routes/votes.py index a85025088..67e1bb2ea 100644 --- a/files/routes/votes.py +++ b/files/routes/votes.py @@ -96,6 +96,7 @@ def vote_post_comment(target_id, new, v, cls, vote_cls): # this is hacky but it works, we should probably do better later def get_vote_count(dir, real_instead_of_dir): + g.db.flush() votes = g.db.query(vote_cls) if real_instead_of_dir: votes = votes.filter(vote_cls.real == True)