Some checks failed
build-images / web (push) Has been cancelled
vision2026-ci / Python SDK resource tests (push) Has been cancelled
build-images / api (push) Has been cancelled
build-images / worker-gpu (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Has been cancelled
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Has been cancelled
codeql / Analyze (javascript-typescript) (push) Has been cancelled
codeql / Analyze (python) (push) Has been cancelled
perf / API hot-path micro-smoke (budgeted) (push) Has been cancelled
perf / Desktop engine crates (release build check) (push) Has been cancelled
vision2026-ci / API phase modules (ruff + pytest) (push) Has been cancelled
vision2026-ci / Alembic upgrade + drift check (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Has been cancelled
Paid, hosted product at menivor.com, now retired as a company project. Fresh squashed history of the cleaned working tree (secret-scanned, no .env, no customer data, no outreach logs). Built on MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
115 lines
3.7 KiB
Python
115 lines
3.7 KiB
Python
"""Concurrent streams per viewer (MV5): a third stream on a two-stream plan is refused.
|
|
|
|
Each playback session is a Redis key with a short TTL that the stream renews
|
|
on every manifest, key or segment request. Starting playback counts the
|
|
viewer's other live sessions. If Redis is unreachable the check lets the
|
|
stream start and logs it (playback stays up).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import time
|
|
from typing import Any, Protocol
|
|
|
|
from loguru import logger
|
|
|
|
SESSION_TTL_S = 90
|
|
DEFAULT_MAX_STREAMS = 2
|
|
|
|
|
|
class Store(Protocol):
|
|
def active(self, viewer: str, now: float) -> list[str]: ...
|
|
def touch(self, viewer: str, session_id: str, now: float) -> None: ...
|
|
def drop(self, viewer: str, session_id: str) -> None: ...
|
|
|
|
|
|
class RedisStore:
|
|
"""``ott:streams:<viewer>`` is a sorted set of session id -> last seen."""
|
|
|
|
def __init__(self, client: Any = None) -> None:
|
|
self._client = client
|
|
|
|
def _redis(self) -> Any:
|
|
if self._client is None:
|
|
from app.services.billing.service import _get_redis
|
|
|
|
self._client = _get_redis()
|
|
return self._client
|
|
|
|
def active(self, viewer: str, now: float) -> list[str]:
|
|
client = self._redis()
|
|
key = f"ott:streams:{viewer}"
|
|
client.zremrangebyscore(key, 0, now - SESSION_TTL_S)
|
|
return [m.decode() if isinstance(m, bytes) else str(m) for m in client.zrange(key, 0, -1)]
|
|
|
|
def touch(self, viewer: str, session_id: str, now: float) -> None:
|
|
client = self._redis()
|
|
key = f"ott:streams:{viewer}"
|
|
client.zadd(key, {session_id: now})
|
|
client.expire(key, SESSION_TTL_S * 2)
|
|
|
|
def drop(self, viewer: str, session_id: str) -> None:
|
|
self._redis().zrem(f"ott:streams:{viewer}", session_id)
|
|
|
|
|
|
class MemoryStore:
|
|
def __init__(self) -> None:
|
|
self.sessions: dict[str, dict[str, float]] = {}
|
|
|
|
def active(self, viewer: str, now: float) -> list[str]:
|
|
live = {s: t for s, t in self.sessions.get(viewer, {}).items() if t > now - SESSION_TTL_S}
|
|
self.sessions[viewer] = live
|
|
return list(live)
|
|
|
|
def touch(self, viewer: str, session_id: str, now: float) -> None:
|
|
self.sessions.setdefault(viewer, {})[session_id] = now
|
|
|
|
def drop(self, viewer: str, session_id: str) -> None:
|
|
self.sessions.get(viewer, {}).pop(session_id, None)
|
|
|
|
|
|
_STORE: Store | None = None
|
|
|
|
|
|
def store() -> Store:
|
|
global _STORE
|
|
if _STORE is None:
|
|
_STORE = RedisStore()
|
|
return _STORE
|
|
|
|
|
|
def max_streams() -> int:
|
|
try:
|
|
return max(1, int(os.getenv("OTT_MAX_STREAMS") or DEFAULT_MAX_STREAMS))
|
|
except ValueError:
|
|
return DEFAULT_MAX_STREAMS
|
|
|
|
|
|
def admit(viewer: str, session_id: str, *, limit: int | None = None, now: float | None = None,
|
|
backend: Store | None = None) -> bool:
|
|
"""Register a new stream; ``False`` when the viewer already has ``limit`` other live streams."""
|
|
moment = now or time.time()
|
|
cap = limit or max_streams()
|
|
try:
|
|
chosen = backend or store()
|
|
others = [s for s in chosen.active(viewer, moment) if s != session_id]
|
|
if len(others) >= cap:
|
|
return False
|
|
chosen.touch(viewer, session_id, moment)
|
|
return True
|
|
except Exception as exc: # noqa: BLE001 - a limiter outage never stops playback
|
|
logger.warning(f"ott stream limit unavailable: {type(exc).__name__}")
|
|
return True
|
|
|
|
|
|
def renew(viewer: str, session_id: str, *, now: float | None = None, backend: Store | None = None) -> None:
|
|
try:
|
|
(backend or store()).touch(viewer, session_id, now or time.time())
|
|
except Exception: # noqa: BLE001 - renewal is best-effort
|
|
pass
|
|
|
|
|
|
__all__ = ["DEFAULT_MAX_STREAMS", "MemoryStore", "RedisStore", "SESSION_TTL_S", "admit", "max_streams", "renew",
|
|
"store"]
|