Add expiry to redis ratelimiting

This commit is contained in:
rubenwardy 2024-01-22 18:09:26 +00:00
parent 6bb6a7ae05
commit cbe232ca0c
3 changed files with 9 additions and 6 deletions

@ -33,7 +33,7 @@ from wtforms_sqlalchemy.fields import QuerySelectField, QuerySelectMultipleField
from app.logic.LogicError import LogicError from app.logic.LogicError import LogicError
from app.logic.packages import do_edit_package from app.logic.packages import do_edit_package
from app.querybuilder import QueryBuilder from app.querybuilder import QueryBuilder
from app.rediscache import has_key, set_key from app.rediscache import has_key, set_temp_key
from app.tasks.importtasks import import_repo_screenshot, check_zip_release from app.tasks.importtasks import import_repo_screenshot, check_zip_release
from app.tasks.webhooktasks import post_discord_webhook from app.tasks.webhooktasks import post_discord_webhook
from app.logic.game_support import GameSupportResolver from app.logic.game_support import GameSupportResolver
@ -65,7 +65,7 @@ def list_all():
edited = True edited = True
key = "tag/{}/{}".format(ip, tag.name) key = "tag/{}/{}".format(ip, tag.name)
if not has_key(key): if not has_key(key):
set_key(key, "true") set_temp_key(key, "true")
Tag.query.filter_by(id=tag.id).update({ Tag.query.filter_by(id=tag.id).update({
"views": Tag.views + 1 "views": Tag.views + 1
}) })

@ -26,7 +26,7 @@ from wtforms_sqlalchemy.fields import QuerySelectField
from app.logic.releases import do_create_vcs_release, LogicError, do_create_zip_release from app.logic.releases import do_create_vcs_release, LogicError, do_create_zip_release
from app.models import Package, db, User, PackageState, Permission, UserRank, PackageDailyStats, MinetestRelease, \ from app.models import Package, db, User, PackageState, Permission, UserRank, PackageDailyStats, MinetestRelease, \
PackageRelease, PackageUpdateTrigger, PackageUpdateConfig PackageRelease, PackageUpdateTrigger, PackageUpdateConfig
from app.rediscache import has_key, set_key, make_download_key from app.rediscache import has_key, set_temp_key, make_download_key
from app.tasks.importtasks import check_update_config from app.tasks.importtasks import check_update_config
from app.utils import is_user_bot, is_package_page, nonempty_or_none from app.utils import is_user_bot, is_package_page, nonempty_or_none
from . import bp, get_package_tabs from . import bp, get_package_tabs
@ -122,7 +122,7 @@ def download_release(package, id):
key = make_download_key(ip, release.package) key = make_download_key(ip, release.package)
if not has_key(key): if not has_key(key):
set_key(key, "true") set_temp_key(key, "true")
bonus = 0 bonus = 0
if reason == "new": if reason == "new":

@ -20,12 +20,15 @@ from . import redis_client
# and also means that the rest of the code avoids knowing about `app` # and also means that the rest of the code avoids knowing about `app`
EXPIRY_TIME_S = 2*7*24*60*60 # 2 weeks
def make_download_key(ip, package): def make_download_key(ip, package):
return "{}/{}/{}".format(ip, package.author.username, package.name) return "{}/{}/{}".format(ip, package.author.username, package.name)
def set_key(key, v): def set_temp_key(key, v):
redis_client.set(key, v) redis_client.set(key, v, ex=EXPIRY_TIME_S)
def has_key(key): def has_key(key):