Improve the frontend 2
This commit is contained in:
@@ -463,7 +463,7 @@ class AuthMiddleware(BaseHTTPMiddleware):
|
||||
|
||||
async def _get_tenant_subscription_tier(self, tenant_id: str, request: Request) -> Optional[str]:
|
||||
"""
|
||||
Get tenant subscription tier from tenant service
|
||||
Get tenant subscription tier using fast cached endpoint
|
||||
|
||||
Args:
|
||||
tenant_id: Tenant ID
|
||||
@@ -473,48 +473,27 @@ class AuthMiddleware(BaseHTTPMiddleware):
|
||||
Subscription tier string or None
|
||||
"""
|
||||
try:
|
||||
# Check cache first
|
||||
if self.redis_client:
|
||||
cache_key = f"tenant:tier:{tenant_id}"
|
||||
try:
|
||||
cached_tier = await self.redis_client.get(cache_key)
|
||||
if cached_tier:
|
||||
if isinstance(cached_tier, bytes):
|
||||
cached_tier = cached_tier.decode()
|
||||
logger.debug("Subscription tier from cache", tenant_id=tenant_id, tier=cached_tier)
|
||||
return cached_tier
|
||||
except Exception as e:
|
||||
logger.warning(f"Cache lookup failed for tenant tier: {e}")
|
||||
|
||||
# Get from tenant service
|
||||
async with httpx.AsyncClient(timeout=5.0) as client:
|
||||
# Use fast cached subscription tier endpoint (has its own Redis caching)
|
||||
async with httpx.AsyncClient(timeout=3.0) as client:
|
||||
headers = {"Authorization": request.headers.get("Authorization", "")}
|
||||
response = await client.get(
|
||||
f"{settings.TENANT_SERVICE_URL}/api/v1/tenants/{tenant_id}",
|
||||
f"{settings.TENANT_SERVICE_URL}/api/v1/subscriptions/{tenant_id}/tier",
|
||||
headers=headers
|
||||
)
|
||||
|
||||
if response.status_code == 200:
|
||||
tenant_data = response.json()
|
||||
subscription_tier = tenant_data.get("subscription_tier", "basic")
|
||||
tier_data = response.json()
|
||||
subscription_tier = tier_data.get("tier", "starter")
|
||||
|
||||
# Cache for 5 minutes
|
||||
if self.redis_client:
|
||||
try:
|
||||
await self.redis_client.setex(
|
||||
f"tenant:tier:{tenant_id}",
|
||||
300, # 5 minutes
|
||||
subscription_tier
|
||||
)
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to cache tenant tier: {e}")
|
||||
|
||||
logger.debug("Subscription tier from service", tenant_id=tenant_id, tier=subscription_tier)
|
||||
logger.debug("Subscription tier from cached endpoint",
|
||||
tenant_id=tenant_id,
|
||||
tier=subscription_tier,
|
||||
cached=tier_data.get("cached", False))
|
||||
return subscription_tier
|
||||
else:
|
||||
logger.warning(f"Failed to get tenant subscription tier: {response.status_code}")
|
||||
return "basic" # Default to basic
|
||||
return "starter" # Default to starter
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error getting tenant subscription tier: {e}")
|
||||
return "basic" # Default to basic on error
|
||||
return "starter" # Default to starter on error
|
||||
@@ -224,7 +224,7 @@ class SubscriptionMiddleware(BaseHTTPMiddleware):
|
||||
allowed_tiers: List[str]
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Validate subscription tier access using tenant service
|
||||
Validate subscription tier access using cached subscription lookup
|
||||
|
||||
Args:
|
||||
request: FastAPI request
|
||||
@@ -250,27 +250,27 @@ class SubscriptionMiddleware(BaseHTTPMiddleware):
|
||||
headers["x-user-full-name"] = str(user.get('full_name', ''))
|
||||
headers["x-tenant-id"] = str(user.get('tenant_id', ''))
|
||||
|
||||
# Call tenant service to get subscription tier with gateway-appropriate timeout
|
||||
# Call tenant service fast tier endpoint with caching
|
||||
timeout_config = httpx.Timeout(
|
||||
connect=2.0, # Connection timeout - short for gateway
|
||||
read=10.0, # Read timeout
|
||||
write=2.0, # Write timeout
|
||||
pool=2.0 # Pool timeout
|
||||
connect=1.0, # Connection timeout - very short for cached endpoint
|
||||
read=5.0, # Read timeout - short for cached lookup
|
||||
write=1.0, # Write timeout
|
||||
pool=1.0 # Pool timeout
|
||||
)
|
||||
|
||||
async with httpx.AsyncClient(timeout=timeout_config) as client:
|
||||
# Get tenant subscription information
|
||||
tenant_response = await client.get(
|
||||
f"{settings.TENANT_SERVICE_URL}/api/v1/tenants/{tenant_id}",
|
||||
# Use fast cached tier endpoint
|
||||
tier_response = await client.get(
|
||||
f"{settings.TENANT_SERVICE_URL}/api/v1/subscriptions/{tenant_id}/tier",
|
||||
headers=headers
|
||||
)
|
||||
|
||||
if tenant_response.status_code != 200:
|
||||
if tier_response.status_code != 200:
|
||||
logger.warning(
|
||||
"Failed to get tenant subscription",
|
||||
"Failed to get subscription tier from cache",
|
||||
tenant_id=tenant_id,
|
||||
status_code=tenant_response.status_code,
|
||||
response_text=tenant_response.text
|
||||
status_code=tier_response.status_code,
|
||||
response_text=tier_response.text
|
||||
)
|
||||
# Fail open for availability
|
||||
return {
|
||||
@@ -279,14 +279,15 @@ class SubscriptionMiddleware(BaseHTTPMiddleware):
|
||||
'current_tier': 'unknown'
|
||||
}
|
||||
|
||||
tenant_data = tenant_response.json()
|
||||
current_tier = tenant_data.get('subscription_tier', 'starter').lower()
|
||||
tier_data = tier_response.json()
|
||||
current_tier = tier_data.get('tier', 'starter').lower()
|
||||
|
||||
logger.debug("Subscription tier check",
|
||||
logger.debug("Subscription tier check (cached)",
|
||||
tenant_id=tenant_id,
|
||||
current_tier=current_tier,
|
||||
minimum_tier=minimum_tier,
|
||||
allowed_tiers=allowed_tiers)
|
||||
allowed_tiers=allowed_tiers,
|
||||
cached=tier_data.get('cached', False))
|
||||
|
||||
# Check if current tier is in allowed tiers
|
||||
if current_tier not in [tier.lower() for tier in allowed_tiers]:
|
||||
|
||||
Reference in New Issue
Block a user