Buckets:

download
raw
5.46 kB
# backend/utils.py
import socket
from contextlib import closing
import psutil
import time
import json
from typing import Dict, List, Optional, Any
from sqlalchemy.orm import Session
from backend.settings import settings
from backend.db.models.config import GlobalConfig
# Global in-memory tracking for service usage
# Format: { service_name: { "total_hits": int, "users": { user_id: int } } }
service_usage_stats: Dict[str, Dict] = {
"openai": {"total_hits": 0, "users": {}},
"ollama": {"total_hits": 0, "users": {}},
"lollms": {"total_hits": 0, "users": {}}
}
# Rate limiting tracking: { (identifier, service): [timestamps] }
rate_limit_store: Dict[tuple, list] = {}
def track_service_usage(service: str, user_id: int):
if service not in service_usage_stats:
service_usage_stats[service] = {"total_hits": 0, "users": {}}
stats = service_usage_stats[service]
stats["total_hits"] += 1
stats["users"][user_id] = stats["users"].get(user_id, 0) + 1
def check_rate_limit(identifier: str, service: str) -> bool:
"""
Checks if the given identifier (API Key or IP) has exceeded
the rate limit configured in settings.
"""
if not settings.get("rate_limit_enabled", False):
return True
max_reqs = settings.get("rate_limit_max_requests", 60)
window = settings.get("rate_limit_window_seconds", 60)
now = time.time()
key = (identifier, service)
if key not in rate_limit_store:
rate_limit_store[key] = [now]
return True
# Clean up old timestamps
rate_limit_store[key] = [t for t in rate_limit_store[key] if now - t < window]
if len(rate_limit_store[key]) >= max_reqs:
return False
rate_limit_store[key].append(now)
return True
def get_local_ip_addresses():
"""Gets all local IPv4 addresses of the machine, including localhost."""
ip_addresses = []
try:
for _, addrs in psutil.net_if_addrs().items():
for addr in addrs:
if addr.family == socket.AF_INET:
ip_addresses.append(addr.address)
unique_ips = sorted(list(set(ip for ip in ip_addresses)))
if '127.0.0.1' in unique_ips:
unique_ips.remove('127.0.0.1')
unique_ips.insert(0, '127.0.0.1')
return unique_ips
except Exception:
try:
hostname = socket.gethostname()
ips = socket.gethostbyname_ex(hostname)[2]
local_ips = [ip for ip in ips if not ip.startswith("127.")]
local_ips.insert(0, "127.0.0.1")
return sorted(list(set(local_ips)))
except Exception:
return ["127.0.0.1"]
def get_public_ip():
"""Tries to determine the primary public IP address of the machine."""
try:
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s.settimeout(2)
s.connect(("8.8.8.8", 80))
ip_address = s.getsockname()[0]
return ip_address
except Exception:
return None
def get_accessible_host() -> str:
"""Determines the accessible host for services based on server config."""
main_server_host = settings.get("host", "0.0.0.0")
if main_server_host not in ["0.0.0.0", "::"]:
return main_server_host
public_domain = settings.get("public_domain_name")
if public_domain and public_domain.strip():
return public_domain.strip()
public_ip = get_public_ip()
if public_ip:
return public_ip
return "localhost"
def find_next_available_port(start_port: int, host: str = "127.0.0.1") -> int:
"""Finds the next available network port starting from a given port."""
port = start_port
while True:
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
try:
s.bind((host, port))
return port
except OSError:
port += 1
# --- Caching Helpers ---
def get_system_cache(db: Session, key: str, default=None):
"""Retrieves a cached value from GlobalConfig."""
try:
config = db.query(GlobalConfig).filter(GlobalConfig.key == key).first()
if config:
data = json.loads(config.value)
if isinstance(data, dict) and "value" in data and "type" in data and data["type"] == "cache":
return data["value"]
# Backward compatibility or direct value
return data
except Exception as e:
print(f"Cache read error for {key}: {e}")
return default
def set_system_cache(db: Session, key: str, value: Any):
"""Saves a value to GlobalConfig as a cache."""
json_val = json.dumps({"value": value, "type": "cache"})
try:
config = db.query(GlobalConfig).filter(GlobalConfig.key == key).first()
if config:
config.value = json_val
else:
config = GlobalConfig(key=key, value=json_val, category="System Cache", description="Auto-generated cache")
db.add(config)
db.commit()
except Exception as e:
db.rollback()
# Concurrency safety: if another thread/process inserted the key in the meantime, try updating it quietly
try:
config = db.query(GlobalConfig).filter(GlobalConfig.key == key).first()
if config:
config.value = json_val
db.commit()
except Exception:
db.rollback()

Xet Storage Details

Size:
5.46 kB
·
Xet hash:
229846bfae9ae4f01e57942f9fd50dde110b8e054e06665ccdda3a0ec073cbf6

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.