获取一个启用代理:优先默认代理,否则使用最早启用的代理。
(db: Session)
| 632 | |
| 633 | |
| 634 | def get_random_proxy(db: Session) -> Optional[Proxy]: |
| 635 | """获取一个启用代理:优先默认代理,否则使用最早启用的代理。""" |
| 636 | _ensure_single_default_proxy(db) |
| 637 | |
| 638 | # 优先返回启用状态下的默认代理 |
| 639 | default_proxy = ( |
| 640 | db.query(Proxy) |
| 641 | .filter(Proxy.enabled == True, Proxy.is_default == True) |
| 642 | .order_by(asc(Proxy.id)) |
| 643 | .first() |
| 644 | ) |
| 645 | if default_proxy: |
| 646 | return default_proxy |
| 647 | |
| 648 | # 默认代理不可用时,回退到最早启用的代理(稳定而可预期) |
| 649 | return ( |
| 650 | db.query(Proxy) |
| 651 | .filter(Proxy.enabled == True) |
| 652 | .order_by(asc(Proxy.id)) |
| 653 | .first() |
| 654 | ) |
| 655 | |
| 656 | |
| 657 | def set_proxy_default(db: Session, proxy_id: int) -> Optional[Proxy]: |
nothing calls this directly
no test coverage detected