Spaces:
Paused
Paused
dushyantsingh0 commited on
Commit ·
0b7baba
1
Parent(s): a4c074e
perf: reduce redis EXISTS pressure with L1 token blocklist cache
Browse files- core/token_blocklist.py +19 -5
- scratch/purge_stale_redis.py +35 -0
- scratch/test_redis_ping.py +16 -0
core/token_blocklist.py
CHANGED
|
@@ -64,21 +64,35 @@ async def blocklist_token(jti: str, expires_in_seconds: int) -> bool:
|
|
| 64 |
return False
|
| 65 |
|
| 66 |
|
|
|
|
|
|
|
| 67 |
async def is_token_blocked(jti: str) -> bool:
|
| 68 |
"""
|
| 69 |
Check if a token's JTI has been revoked.
|
| 70 |
-
|
| 71 |
-
Returns:
|
| 72 |
-
True if the token is blocklisted, False otherwise.
|
| 73 |
-
Returns False on Redis failure (fail-open to prevent lockout).
|
| 74 |
"""
|
| 75 |
if not jti:
|
| 76 |
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
try:
|
| 78 |
r = await _get_redis()
|
| 79 |
key = f"{_BLOCKLIST_PREFIX}{jti}"
|
| 80 |
result = await r.exists(key)
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
except Exception as e:
|
| 83 |
# Fail-open: if Redis is down, do NOT block all users
|
| 84 |
logger.error(f"Token blocklist check failed (fail-open): {e}")
|
|
|
|
| 64 |
return False
|
| 65 |
|
| 66 |
|
| 67 |
+
_blocklist_cache = {}
|
| 68 |
+
|
| 69 |
async def is_token_blocked(jti: str) -> bool:
|
| 70 |
"""
|
| 71 |
Check if a token's JTI has been revoked.
|
| 72 |
+
Uses a local 60s memory cache to prevent pounding Redis on every API call.
|
|
|
|
|
|
|
|
|
|
| 73 |
"""
|
| 74 |
if not jti:
|
| 75 |
return False
|
| 76 |
+
|
| 77 |
+
import time
|
| 78 |
+
now = time.time()
|
| 79 |
+
|
| 80 |
+
# Check L1 Cache
|
| 81 |
+
if jti in _blocklist_cache:
|
| 82 |
+
cached_val, expiry = _blocklist_cache[jti]
|
| 83 |
+
if now < expiry:
|
| 84 |
+
return cached_val
|
| 85 |
+
|
| 86 |
try:
|
| 87 |
r = await _get_redis()
|
| 88 |
key = f"{_BLOCKLIST_PREFIX}{jti}"
|
| 89 |
result = await r.exists(key)
|
| 90 |
+
blocked = bool(result)
|
| 91 |
+
|
| 92 |
+
# Update L1 Cache (Cache for 60s)
|
| 93 |
+
_blocklist_cache[jti] = (blocked, now + 60)
|
| 94 |
+
|
| 95 |
+
return blocked
|
| 96 |
except Exception as e:
|
| 97 |
# Fail-open: if Redis is down, do NOT block all users
|
| 98 |
logger.error(f"Token blocklist check failed (fail-open): {e}")
|
scratch/purge_stale_redis.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
import redis.asyncio as redis
|
| 3 |
+
from core.config import get_redis_settings
|
| 4 |
+
|
| 5 |
+
async def purge_stale_queue():
|
| 6 |
+
conf = get_redis_settings()
|
| 7 |
+
client = redis.Redis(host=conf.host, port=conf.port, password=conf.password, db=0)
|
| 8 |
+
|
| 9 |
+
# 1. IDENTIFY THE PHANTOM QUEUE KEY
|
| 10 |
+
# ARQ sorted set for the queue usually looks like 'arq:queue'
|
| 11 |
+
queue_key = 'arq:queue'
|
| 12 |
+
|
| 13 |
+
# 2. CHECK SIZE
|
| 14 |
+
size = await client.zcard(queue_key)
|
| 15 |
+
print(f"Detected {size} stale jobs in '{queue_key}'")
|
| 16 |
+
|
| 17 |
+
if size > 0:
|
| 18 |
+
# 3. PURGE
|
| 19 |
+
await client.delete(queue_key)
|
| 20 |
+
print(f"SUCCESS: Purged '{queue_key}'")
|
| 21 |
+
|
| 22 |
+
# 4. ALSO PURGE JOB KEYS (arq:job:*)
|
| 23 |
+
keys = await client.keys("arq:job:*")
|
| 24 |
+
# Filter: only those that aren't in regular or heavy?
|
| 25 |
+
# Actually it's safer to just delete those specifically from the phantom queue if we have IDs
|
| 26 |
+
# But deleting arq:job:* keys is fine if they are truly stale.
|
| 27 |
+
print(f"Found {len(keys)} potential stale job data keys.")
|
| 28 |
+
if len(keys) > 0:
|
| 29 |
+
await client.delete(*keys)
|
| 30 |
+
print("Purged all job data keys.")
|
| 31 |
+
|
| 32 |
+
await client.close()
|
| 33 |
+
|
| 34 |
+
if __name__ == "__main__":
|
| 35 |
+
asyncio.run(purge_stale_queue())
|
scratch/test_redis_ping.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
import redis.asyncio as redis
|
| 3 |
+
from core.config import get_redis_settings
|
| 4 |
+
|
| 5 |
+
async def test_ping():
|
| 6 |
+
conf = get_redis_settings()
|
| 7 |
+
try:
|
| 8 |
+
client = redis.Redis(host=conf.host, port=conf.port, password=conf.password, db=0)
|
| 9 |
+
pong = await client.ping()
|
| 10 |
+
print(f"PONG: {pong}")
|
| 11 |
+
await client.close()
|
| 12 |
+
except Exception as e:
|
| 13 |
+
print(f"PING FAILED: {e}")
|
| 14 |
+
|
| 15 |
+
if __name__ == "__main__":
|
| 16 |
+
asyncio.run(test_ping())
|