Skip to content

Commit

Permalink
Update app.py
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinebou12 authored Mar 2, 2024
1 parent 1a0d001 commit 3af5b1e
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from fastapi.security import HTTPBasic, HTTPBasicCredentials
import hashlib
from cachetools import LRUCache
from cachetools.sync import RLock
import os

import asyncio
Expand Down Expand Up @@ -696,7 +695,8 @@ class APIResponse(BaseModel):
data: Optional[dict] = None


user_instances_cache = LRUCache(maxsize=100, lock=RLock())
user_instances_cache = LRUCache(maxsize=100)
cache_lock = asyncio.Lock()

async def get_user_hash(credentials: HTTPBasicCredentials):
"""
Expand All @@ -710,20 +710,18 @@ async def get_user_hash(credentials: HTTPBasicCredentials):
async def get_current_user(credentials: HTTPBasicCredentials = Depends(security)):
user_hash = await get_user_hash(credentials)

# Try to get an existing user instance from the cache
user = user_instances_cache.get(user_hash)

if not user:
try:
# Create a new user instance and authenticate
user = RenphoWeight(email=credentials.username, password=credentials.password)
await user.auth() # Ensure that user can authenticate

# Store the authenticated user instance in the cache
user_instances_cache[user_hash] = user
except Exception as e:
print(f"Authentication failed: {e}") # Adjust the logging mechanism as needed
raise HTTPException(status_code=401, detail="Authentication failed")
async with cache_lock:
# Access the cache within the locked context to ensure thread safety
user = user_instances_cache.get(user_hash)

if not user:
try:
user = RenphoWeight(email=credentials.username, password=credentials.password)
await user.auth()
user_instances_cache[user_hash] = user
except Exception as e:
print(f"Authentication failed: {e}") # Adjust the logging mechanism as needed
raise HTTPException(status_code=401, detail="Authentication failed")

return user

Expand Down

0 comments on commit 3af5b1e

Please sign in to comment.