From b46d56acad7633a2ead23a0022cc38179e227d57 Mon Sep 17 00:00:00 2001 From: AllinolCP Date: Sun, 1 Aug 2021 10:56:50 +0700 Subject: [PATCH] fix AIORedis v2.0.0 #2 --- houdini/handlers/login/__init__.py | 10 ++++++---- houdini/handlers/login/login.py | 17 +++++++++-------- houdini/handlers/login/world.py | 18 +++++++++--------- houdini/handlers/redemption/__init__.py | 14 +++++++------- houdini/handlers/redemption/book.py | 8 ++++---- 5 files changed, 35 insertions(+), 32 deletions(-) diff --git a/houdini/handlers/login/__init__.py b/houdini/handlers/login/__init__.py index 4000a86..935cdfa 100644 --- a/houdini/handlers/login/__init__.py +++ b/houdini/handlers/login/__init__.py @@ -45,10 +45,12 @@ async def get_server_presence(p, pdata): if await p.server.redis.scard(server_key): async with p.server.db.transaction(): buddies = BuddyList.select('buddy_id').where(BuddyList.penguin_id == pdata.id).gino.iterate() - tr = p.server.redis.multi_exec() - async for buddy_id, in buddies: - tr.sismember(server_key, buddy_id) - online_buddies = await tr.execute() + + async with p.server.redis.pipeline(transaction=True) as tr: + async for buddy_id, in buddies: + tr.sismember(server_key, buddy_id) + online_buddies = await tr.execute() + if any(online_buddies): buddy_worlds.append(str(int(server_id))) diff --git a/houdini/handlers/login/login.py b/houdini/handlers/login/login.py index d8223fa..0aa863a 100644 --- a/houdini/handlers/login/login.py +++ b/houdini/handlers/login/login.py @@ -41,10 +41,10 @@ async def handle_login(p, credentials: Credentials): p.logger.info(f'{username} failed to login: incorrect password') if await p.server.redis.exists(flood_key): - tr = p.server.redis.multi_exec() - tr.incr(flood_key) - tr.expire(flood_key, p.server.config.login_failure_timer) - failure_count, _ = await tr.execute() + async with p.server.redis.pipeline(transaction=True) as tr: + tr.incr(flood_key) + tr.expire(flood_key, p.server.config.login_failure_timer) + failure_count, _ = await tr.execute() if failure_count >= p.server.config.login_failure_limit: return await p.send_error_and_disconnect(150) @@ -100,10 +100,11 @@ async def handle_login(p, credentials: Credentials): login_key = Crypto.hash(random_key[::-1]) confirmation_hash = Crypto.hash(os.urandom(24)) - tr = p.server.redis.multi_exec() - tr.setex(f'{data.username}.lkey', p.server.config.auth_ttl, login_key) - tr.setex(f'{data.username}.ckey', p.server.config.auth_ttl, confirmation_hash) - await tr.execute() + + async with p.server.redis.pipeline(transaction=True) as tr: + tr.setex(f'{data.username}.lkey', p.server.config.auth_ttl, login_key) + tr.setex(f'{data.username}.ckey', p.server.config.auth_ttl, confirmation_hash) + await tr.execute() world_populations, buddy_presence = await get_server_presence(p, data) diff --git a/houdini/handlers/login/world.py b/houdini/handlers/login/world.py index 4107a05..961e3f7 100644 --- a/houdini/handlers/login/world.py +++ b/houdini/handlers/login/world.py @@ -41,11 +41,11 @@ async def world_login(p, data): @handlers.allow_once @handlers.depends_on_packet(XMLPacket('verChk'), XMLPacket('rndK')) async def handle_login(p, credentials: WorldCredentials): - tr = p.server.redis.multi_exec() - tr.get(f'{credentials.username}.lkey') - tr.get(f'{credentials.username}.ckey') - tr.delete(f'{credentials.username}.lkey', f'{credentials.username}.ckey') - login_key, confirmation_hash, _ = await tr.execute() + async with p.server.redis.pipeline(transaction=True) as tr: + tr.get(f'{credentials.username}.lkey') + tr.get(f'{credentials.username}.ckey') + tr.delete(f'{credentials.username}.lkey', f'{credentials.username}.ckey') + login_key, confirmation_hash, _ = await tr.execute() if login_key is None or confirmation_hash is None: return await p.close() @@ -69,10 +69,10 @@ async def handle_login(p, credentials: WorldCredentials): @handlers.allow_once @handlers.depends_on_packet(XMLPacket('verChk'), XMLPacket('rndK')) async def handle_legacy_login(p, credentials: Credentials): - tr = p.server.redis.multi_exec() - tr.get(f'{credentials.username}.lkey') - tr.delete(f'{credentials.username}.lkey', f'{credentials.username}.ckey') - login_key, _ = await tr.execute() + async with p.server.redis.pipeline(transaction=True) as tr: + tr.get(f'{credentials.username}.lkey') + tr.delete(f'{credentials.username}.lkey', f'{credentials.username}.ckey') + login_key, _ = await tr.execute() try: login_key = login_key.decode() diff --git a/houdini/handlers/redemption/__init__.py b/houdini/handlers/redemption/__init__.py index 872dec8..0a74509 100644 --- a/houdini/handlers/redemption/__init__.py +++ b/houdini/handlers/redemption/__init__.py @@ -12,10 +12,10 @@ async def handle_join_redemption_server_vanilla(p, credentials: str, confirmatio if login_key != p.login_key: return await p.close() - tr = p.server.redis.multi_exec() - tr.setex(f'{username}.lkey', p.server.config.auth_ttl, login_key) - tr.setex(f'{username}.ckey', p.server.config.auth_ttl, confirmation_hash) - await tr.execute() + async with p.server.redis.pipeline(transaction=True) as tr: + tr.setex(f'{username}.lkey', p.server.config.auth_ttl, login_key) + tr.setex(f'{username}.ckey', p.server.config.auth_ttl, confirmation_hash) + await tr.execute() redeemed_books = await PenguinRedemptionBook.query.where(PenguinRedemptionBook.penguin_id == p.id).gino.all() await p.send_xt('rjs', ','.join(str(redeemed_book.book_id) for redeemed_book in redeemed_books), 'houdini', @@ -28,9 +28,9 @@ async def handle_join_redemption_server_legacy(p, _, login_key: str): if login_key != p.login_key: return await p.close() - tr = p.server.redis.multi_exec() - tr.setex(f'{p.username}.lkey', p.server.config.auth_ttl, login_key) - await tr.execute() + async with p.server.redis.pipeline(transaction=True) as tr: + tr.setex(f'{p.username}.lkey', p.server.config.auth_ttl, login_key) + await tr.execute() redeemed_books = await PenguinRedemptionBook.query.where(PenguinRedemptionBook.penguin_id == p.id).gino.all() await p.send_xt('rjs', ','.join(str(redeemed_book.book_id) for redeemed_book in redeemed_books), 'houdini', diff --git a/houdini/handlers/redemption/book.py b/houdini/handlers/redemption/book.py index 76ee32d..2fbc346 100644 --- a/houdini/handlers/redemption/book.py +++ b/houdini/handlers/redemption/book.py @@ -37,10 +37,10 @@ async def handle_send_book_answer(p, book: int, question_id: int, answer: str): redemption_attempts_key = f'{p.id}.redemption_attempts' if await p.server.redis.exists(redemption_attempts_key): - tr = p.server.redis.multi_exec() - tr.incr(redemption_attempts_key) - tr.expire(redemption_attempts_key, p.server.config.login_failure_timer) - failure_count, _ = await tr.execute() + async with p.server.redis.pipeline(transaction=True) as tr: + tr.incr(redemption_attempts_key) + tr.expire(redemption_attempts_key, p.server.config.login_failure_timer) + failure_count, _ = await tr.execute() if failure_count >= 5: return await p.send_error(713)