"""Bounded Google I/O and last-known-good settings, independent of the ASGI loop.""" import asyncio import copy import functools import json import logging import os import tempfile import threading import time from concurrent.futures import ThreadPoolExecutor from pathlib import Path import requests from fastapi import HTTPException from google.auth.transport.requests import AuthorizedSession, Request logger = logging.getLogger(__name__) GOOGLE_CONNECT_TIMEOUT = float(os.getenv("GOOGLE_CONNECT_TIMEOUT", "3")) GOOGLE_READ_TIMEOUT = float(os.getenv("GOOGLE_READ_TIMEOUT", "10")) GOOGLE_TIMEOUT = (GOOGLE_CONNECT_TIMEOUT, GOOGLE_READ_TIMEOUT) GOOGLE_WORKERS = max(1, int(os.getenv("GOOGLE_WORKERS", "4"))) _executor = ThreadPoolExecutor(max_workers=GOOGLE_WORKERS, thread_name_prefix="google-io") _slots = threading.BoundedSemaphore(GOOGLE_WORKERS) _settings_write_lock = threading.RLock() _history_lock = threading.RLock() def _bounded_timeout(timeout): if timeout is None: return GOOGLE_TIMEOUT if isinstance(timeout, tuple): connect, read = timeout else: connect = read = timeout return ( min(connect, GOOGLE_CONNECT_TIMEOUT) if connect is not None else GOOGLE_CONNECT_TIMEOUT, min(read, GOOGLE_READ_TIMEOUT) if read is not None else GOOGLE_READ_TIMEOUT, ) class GoogleTimeoutSession(requests.Session): """Also bound token refresh calls, which otherwise default to 120 seconds.""" def request(self, method, url, **kwargs): kwargs["timeout"] = _bounded_timeout(kwargs.get("timeout")) return super().request(method, url, **kwargs) class GoogleAuthorizedSession(AuthorizedSession): def request(self, method, url, **kwargs): kwargs["timeout"] = _bounded_timeout(kwargs.get("timeout")) return super().request(method, url, **kwargs) def create_google_session(credentials): return GoogleAuthorizedSession( credentials, auth_request=Request(session=GoogleTimeoutSession()), refresh_timeout=GOOGLE_CONNECT_TIMEOUT + GOOGLE_READ_TIMEOUT, ) def create_google_client(credentials): import gspread client = gspread.Client(auth=credentials, session=create_google_session(credentials)) client.set_timeout(GOOGLE_TIMEOUT) return client async def run_google_io(function, *args, **kwargs): # No unbounded executor queue, and no consumption of FastAPI's DB worker pool. if not _slots.acquire(blocking=False): raise HTTPException( status_code=503, detail="구글 연동 작업이 진행 중입니다. 잠시 후 다시 시도해주세요.", headers={"Retry-After": "3"}, ) try: future = _executor.submit(functools.partial(function, *args, **kwargs)) except BaseException: _slots.release() raise future.add_done_callback(lambda _: _slots.release()) # Disconnecting the browser must not free a slot while its write still runs. return await asyncio.shield(asyncio.wrap_future(future)) def google_worker(function): @functools.wraps(function) async def wrapped(*args, **kwargs): return await run_google_io(function, *args, **kwargs) return wrapped def _serialized(lock): def decorate(function): @functools.wraps(function) def wrapped(*args, **kwargs): with lock: return function(*args, **kwargs) return wrapped return decorate settings_update = _serialized(_settings_write_lock) history_access = _serialized(_history_lock) class SettingsCache: """Serve snapshots immediately; refresh stale data once, in a separate worker.""" def __init__(self, path, spreadsheet_id, ttl=60, retry_delay=15): self.path = Path(path) self.spreadsheet_id = spreadsheet_id self.ttl = ttl self.retry_delay = retry_delay self._lock = threading.RLock() self._refresh_lock = threading.Lock() self._executor = ThreadPoolExecutor(max_workers=1, thread_name_prefix="settings-refresh") self._pending = None self._rows = None self._loaded_at = float("-inf") self._retry_at = 0.0 self._generation = 0 self._restore() @property def has_value(self): with self._lock: return self._rows is not None @property def stale(self): with self._lock: return time.monotonic() - self._loaded_at >= self.ttl def _restore(self): try: payload = json.loads(self.path.read_text(encoding="utf-8")) rows = payload["rows"] if payload["spreadsheet_id"] != self.spreadsheet_id: return if not isinstance(rows, list) or not all( isinstance(row, list) and all(isinstance(cell, str) for cell in row) for row in rows ): return self._rows = rows # A restored snapshot is usable immediately, but always revalidated. except (OSError, ValueError, KeyError, TypeError): pass def _persist_locked(self): temporary = None try: self.path.parent.mkdir(parents=True, exist_ok=True) with tempfile.NamedTemporaryFile( mode="w", encoding="utf-8", dir=self.path.parent, delete=False ) as target: temporary = target.name json.dump( {"spreadsheet_id": self.spreadsheet_id, "rows": self._rows}, target, ensure_ascii=False, ) os.replace(temporary, self.path) except OSError as exc: logger.warning("Settings snapshot persistence failed (%s)", type(exc).__name__) finally: if temporary and os.path.exists(temporary): os.unlink(temporary) def publish(self, rows): with self._lock: self._rows = copy.deepcopy(rows) self._loaded_at = time.monotonic() self._retry_at = 0.0 self._generation += 1 self._persist_locked() def invalidate(self): with self._lock: self._loaded_at = float("-inf") self._retry_at = 0.0 self._generation += 1 def _load(self, loader, force=False): with self._refresh_lock: with self._lock: if not force: if self._rows is not None and not self.stale: return copy.deepcopy(self._rows) if time.monotonic() < self._retry_at: if self._rows is not None: return copy.deepcopy(self._rows) raise RuntimeError("Settings refresh is cooling down") generation = self._generation try: rows = loader() except Exception: with self._lock: self._retry_at = time.monotonic() + self.retry_delay raise with self._lock: # An earlier read must never replace a subsequently saved setting. if generation == self._generation: self.publish(rows) return copy.deepcopy(self._rows) def _background_load(self, loader): try: self._load(loader) except Exception as exc: # Do not log URLs, credential contents or settings data. logger.warning("Google settings refresh failed (%s); retaining snapshot", type(exc).__name__) def refresh_in_background(self, loader): with self._lock: if time.monotonic() < self._retry_at: return if self._pending is None or self._pending.done(): self._pending = self._executor.submit(self._background_load, loader) def read(self, loader, force=False): if force: # Editors must not overwrite Google using a stale cached snapshot. return self._load(loader, force=True) with self._lock: if self._rows is not None: rows = copy.deepcopy(self._rows) if self.stale: self.refresh_in_background(loader) return rows return self._load(loader) def close(self): self._executor.shutdown(wait=True)