config.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. from __future__ import annotations
  2. import json
  3. from dataclasses import dataclass
  4. from pathlib import Path
  5. from typing import Literal
  6. Strategy = Literal["broadcast", "top2", "top3", "top4", "backup"]
  7. @dataclass
  8. class RelayNode:
  9. name: str
  10. host: str
  11. port: int
  12. token: str
  13. weight: int = 100
  14. @dataclass
  15. class Config:
  16. relays: list[RelayNode]
  17. strategy: Strategy = "top3"
  18. redundancy: int = 3
  19. tcp_warmup_bytes: int = 1048576
  20. probe_interval: float = 15.0
  21. tcp_loser_grace_ms: int = 1500
  22. @classmethod
  23. def load(cls, path: str) -> "Config":
  24. raw = json.loads(Path(path).read_text())
  25. relays = [RelayNode(**item) for item in raw["relays"]]
  26. return cls(
  27. relays=relays,
  28. strategy=raw.get("strategy", "top3"),
  29. redundancy=raw.get("redundancy", 3),
  30. tcp_warmup_bytes=raw.get("tcp_warmup_bytes", 1048576),
  31. probe_interval=raw.get("probe_interval", 15.0),
  32. tcp_loser_grace_ms=raw.get("tcp_loser_grace_ms", 1500),
  33. )