Skip to content
Merged

Dev #38

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ DANYAPI_LOG_BACKUP_COUNT=3
DANYAPI_USAGE_ENABLED=1
DANYAPI_USAGE_MAX_RECORDS=1000
DANYAPI_AUTO_UPDATE=1
DANYAPI_CORS_ORIGINS=
DANYAPI_RESPONSES_MAX_RECORDS=1024
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ venv/
coverage.xml
htmlcov/
*.exe
*.obj
pow_solver
.playwright-mcp/
.idea/
Expand All @@ -23,3 +24,4 @@ pow_solver
.DS_Store
Thumbs.db
references
collected.xml
10 changes: 5 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
FROM python:3.14-slim
FROM python:3.12-slim

WORKDIR /app

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
RUN apt-get update \
&& apt-get install -y --no-install-recommends gcc libc6-dev nodejs \
&& pip install --no-cache-dir -r requirements.txt

COPY danyapi ./danyapi
COPY web ./web
COPY docs ./docs

RUN apt-get update \
&& apt-get install -y --no-install-recommends gcc libc6-dev nodejs \
&& gcc -O3 -pthread -funroll-loops -flto -fomit-frame-pointer -o danyapi/deepseek/pow_solver danyapi/deepseek/pow_solver.c \
RUN gcc -O3 -pthread -funroll-loops -flto -fomit-frame-pointer -o danyapi/deepseek/pow_solver danyapi/deepseek/pow_solver.c \
&& apt-get purge -y gcc libc6-dev \
&& apt-get autoremove -y \
&& rm -rf /var/lib/apt/lists/*
Expand Down
10 changes: 9 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def build_solver() -> None:
ROOT / "pow_solver.c",
ROOT / "danyapi" / "pow_solver.c",
ROOT / "danyapi" / "solver" / "pow_solver.c",
ROOT / "danyapi" / "deepseek" / "pow_solver.c",
]
src_path: Path | None = None
for candidate in src_candidates:
Expand Down Expand Up @@ -100,14 +101,21 @@ def build_solver() -> None:
if success:
if not is_win:
os.chmod(bin_path, 0o755)
dest_dirs = [ROOT, ROOT / "danyapi", ROOT / "danyapi" / "solver"]
dest_dirs = [ROOT, ROOT / "danyapi", ROOT / "danyapi" / "solver", ROOT / "danyapi" / "deepseek"]
for d in dest_dirs:
if d.exists():
dst = d / bin_name
if dst.resolve() != bin_path.resolve():
shutil.copy2(bin_path, dst)
if not is_win:
os.chmod(dst, 0o755)
for d in [ROOT, src_path.parent, bin_path.parent]:
obj = d / "pow_solver.obj"
if obj.exists():
try:
obj.unlink()
except OSError:
pass
print(f"Native pow_solver compiled via {chosen_compiler} ({bin_name})")


Expand Down
1 change: 1 addition & 0 deletions collecter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
".hypothesis",
".coverage",
"egg-info",
".env",
}

EXCLUDE_EXTS = {".pyc", ".db", ".cache", ".wasm", ".exe", ".dll", ".so"}
Expand Down
121 changes: 86 additions & 35 deletions danyapi/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
import logging
import threading
import time
from collections.abc import Sequence
from contextlib import asynccontextmanager
from typing import Any, Generic, Protocol, TypeVar

from .deepseek.client import DeepSeekClient
from .pow import PowManager
from .sessions import SessionRegistry
from .store import JsonStore
from .store import _MAX_AFFINITY, JsonStore

log = logging.getLogger("danyapi.accounts")

Expand Down Expand Up @@ -161,7 +162,7 @@ def _touch(self, session_id: str, now: float) -> None:


class DeepSeekAccount:
__slots__ = ("broken", "client", "index", "pow", "pow_upload", "sem", "sessions", "stable_id")
__slots__ = ("broken", "broken_at", "client", "index", "pow", "pow_upload", "sem", "sessions", "stable_id")

def __init__(
self,
Expand All @@ -180,10 +181,12 @@ def __init__(
self.sessions = SessionRegistry(client, session_cache_size, ttl, store=store, key_prefix=f"{index}:")
self.stable_id = stable_id
self.broken = False
self.broken_at: float | None = None

def mark_broken(self) -> None:
if not self.broken:
self.broken = True
self.broken_at = time.monotonic()
log.warning("account #%d marked broken (invalid/expired token)", self.index)

@property
Expand All @@ -193,23 +196,29 @@ def label(self) -> str:

class _PoolAccount(Protocol):
broken: bool
broken_at: float | None
sem: asyncio.Semaphore

@property
def label(self) -> str: ...


AccountT = TypeVar("AccountT", bound=_PoolAccount)


class AccountPool(Generic[AccountT]):
_REVIVE_COOLDOWN = 300.0

def __init__(
self,
accounts: list[AccountT],
accounts: Sequence[AccountT],
label: str = "deepseek",
session_cache_size: int = 128,
ttl: float = 0.0,
context_store: JsonStore | None = None,
affinity_store: JsonStore | None = None,
) -> None:
self.accounts = accounts
self.accounts = list(accounts)
self.label = label
self._by_session: dict[str, tuple[int, float]] = {}
self._stable_to_idx: dict[str, int] = {}
Expand All @@ -220,6 +229,7 @@ def __init__(
self._rr = 0
self._ttl = max(0.0, ttl)
self._affinity_store = affinity_store
self._affinity_lock = threading.Lock()
self._contexts = ContextIndex(session_cache_size, ttl, store=context_store)
self._restore_affinities()

Expand Down Expand Up @@ -259,18 +269,27 @@ def healthy(self) -> list[AccountT]:

def register(self, account_index: int, session_id: str) -> None:
now = time.monotonic()
self._by_session[session_id] = (account_index, now)
if self._affinity_store is not None:
self._affinity_store.set(session_id, self._affinity_record(account_index))
if self._ttl > 0 and len(self._by_session) > max(4096, len(self.accounts) * 1024):
stale = [sid for sid, (_, ts) in self._by_session.items() if now - ts > self._ttl]
for sid in stale:
self._by_session.pop(sid, None)
record = self._affinity_record(account_index)
with self._affinity_lock:
self._by_session[session_id] = (account_index, now)
if self._affinity_store is not None:
if self._affinity_store.get(session_id) != record:
self._affinity_store.set(session_id, record)
while len(self._by_session) > _MAX_AFFINITY:
oldest = next(iter(self._by_session))
self._by_session.pop(oldest, None)
if self._affinity_store is not None:
self._affinity_store.discard(sid)
self._affinity_store.discard(oldest)
if self._ttl > 0 and len(self._by_session) > max(4096, len(self.accounts) * 1024):
stale = [sid for sid, (_, ts) in self._by_session.items() if now - ts > self._ttl]
for sid in stale:
self._by_session.pop(sid, None)
if self._affinity_store is not None:
self._affinity_store.discard(sid)

def forget(self, session_id: str) -> None:
self._by_session.pop(session_id, None)
with self._affinity_lock:
self._by_session.pop(session_id, None)
if self._affinity_store is not None:
self._affinity_store.discard(session_id)

Expand All @@ -284,35 +303,38 @@ def forget_context(self, session_id: str) -> None:
self._contexts.forget(session_id)

def account_for_session(self, session_id: str) -> AccountT | None:
entry = self._by_session.get(session_id)
if entry is None:
return None
idx, ts = entry
now = time.monotonic()
if self._ttl > 0 and now - ts > self._ttl:
self._by_session.pop(session_id, None)
self._contexts.forget(session_id)
if self._affinity_store is not None:
self._affinity_store.discard(session_id)
return None
acct = self.accounts[idx]
if acct is None or acct.broken:
self._by_session.pop(session_id, None)
self._contexts.forget(session_id)
if self._affinity_store is not None:
self._affinity_store.discard(session_id)
return None
if self._ttl > 0 and now != ts:
self._by_session[session_id] = (idx, now)
return acct
with self._affinity_lock:
entry = self._by_session.get(session_id)
if entry is None:
return None
idx, ts = entry
now = time.monotonic()
if self._ttl > 0 and now - ts > self._ttl:
self._by_session.pop(session_id, None)
self._contexts.forget(session_id)
if self._affinity_store is not None:
self._affinity_store.discard(session_id)
return None
acct = self.accounts[idx]
if acct is None or acct.broken:
self._by_session.pop(session_id, None)
self._contexts.forget(session_id)
if self._affinity_store is not None:
self._affinity_store.discard(session_id)
return None
if self._ttl > 0 and now != ts:
self._by_session[session_id] = (idx, now)
return acct

def stats(self) -> dict[str, Any]:
with self._affinity_lock:
affinities = len(self._by_session)
return {
"label": self.label,
"accounts": len(self.accounts),
"healthy": len(self.healthy),
"broken": len(self.accounts) - len(self.healthy),
"session_affinities": len(self._by_session),
"session_affinities": affinities,
"context_entries": self._contexts.size,
"context_hits": self._contexts.hits,
"context_misses": self._contexts.misses,
Expand All @@ -323,6 +345,11 @@ def stats(self) -> dict[str, Any]:
async def acquire(self, session_id: str | None, max_wait: float | None = None) -> tuple[AccountT, str | None]:
healthy = self.healthy
if not healthy:
revived = await self.revive_broken()
if revived is not None:
healthy = [revived]
elif any(getattr(acct, "broken_at", None) is not None for acct in self.accounts):
raise AccountPoolBusy()
raise RuntimeError(f"all {self.label} accounts are unavailable")
if session_id:
acct = self.account_for_session(session_id)
Expand Down Expand Up @@ -367,6 +394,30 @@ async def _wait_free(
raise AccountPoolBusy()
await asyncio.sleep(0.05)

async def revive_broken(self) -> AccountT | None:
now = time.monotonic()
candidates: list[AccountT] = []
for acct in self.accounts:
if not acct.broken:
continue
broken_at = getattr(acct, "broken_at", None)
if broken_at is not None and now - broken_at >= self._REVIVE_COOLDOWN:
candidates.append(acct)
for acct in candidates:
client = getattr(acct, "client", None)
if client is None:
continue
try:
ok = await client.check_auth()
except Exception:
ok = False
if ok:
acct.broken = False
acct.broken_at = None
log.info("%s revived after auth recheck", acct.label)
return acct
return None

def add_account(self, account: AccountT) -> None:
idx = len(self.accounts)
self.accounts.append(account)
Expand Down
Loading
Loading