forked from kenzuya/unshackle
perf(api): stop rebuilding proxy catalogues, vaults and cdms per request
Cache proxy provider catalogues (10 min), reuse vaults and CDMs across one batch licence call, and cache the health update check, /api/services entries and parsed service config.yaml.
This commit is contained in:
@@ -15,7 +15,7 @@ from urllib.parse import urlsplit
|
||||
import requests
|
||||
from requests.adapters import HTTPAdapter
|
||||
|
||||
from unshackle.core.proxies.proxy import Proxy
|
||||
from unshackle.core.proxies.proxy import Proxy, cached_catalogue
|
||||
from unshackle.core.utils.redact import REDACTED
|
||||
|
||||
log = logging.getLogger("ControlD")
|
||||
@@ -369,6 +369,7 @@ class ControlD(Proxy):
|
||||
|
||||
return f"{countries} Countr{['ies', 'y'][countries == 1]} ({servers} Server{['s', ''][servers == 1]})"
|
||||
|
||||
@cached_catalogue
|
||||
def get_locations(self) -> list[dict]:
|
||||
"""Get Control D's proxy locations. Public data, so no token is needed."""
|
||||
try:
|
||||
|
||||
@@ -4,7 +4,7 @@ import subprocess
|
||||
from typing import Optional
|
||||
|
||||
from unshackle.core import binaries
|
||||
from unshackle.core.proxies.proxy import Proxy
|
||||
from unshackle.core.proxies.proxy import Proxy, cached_catalogue
|
||||
|
||||
|
||||
class Hola(Proxy):
|
||||
@@ -71,6 +71,7 @@ class Hola(Proxy):
|
||||
|
||||
return random.choice(proxies) if proxies else None
|
||||
|
||||
@cached_catalogue
|
||||
def get_countries(self) -> list[dict[str, str]]:
|
||||
"""Get a list of available Countries."""
|
||||
p = subprocess.run(
|
||||
|
||||
@@ -6,7 +6,7 @@ from typing import Optional, Union
|
||||
import requests
|
||||
|
||||
from unshackle.core.proxies.asn import describe, is_ip, mapped_query, select, split_selector
|
||||
from unshackle.core.proxies.proxy import Proxy
|
||||
from unshackle.core.proxies.proxy import Proxy, cached_catalogue
|
||||
from unshackle.core.utilities import COUNTRY_CODE_ALIASES
|
||||
|
||||
|
||||
@@ -186,6 +186,7 @@ class NordVPN(Proxy):
|
||||
raise ValueError("Could not decode list of NordVPN countries, not JSON data.")
|
||||
|
||||
@staticmethod
|
||||
@cached_catalogue
|
||||
def get_countries() -> list[dict]:
|
||||
"""Get a list of available Countries and their metadata."""
|
||||
res = requests.get(url="https://api.nordvpn.com/v1/servers/countries", timeout=10)
|
||||
|
||||
@@ -1,5 +1,31 @@
|
||||
import functools
|
||||
import time
|
||||
from abc import abstractmethod
|
||||
from typing import Any, Optional
|
||||
from typing import Any, Callable, Optional, TypeVar
|
||||
|
||||
CATALOGUE_TTL = 600.0
|
||||
CATALOGUES: dict[str, tuple[float, Any]] = {}
|
||||
|
||||
F = TypeVar("F", bound=Callable[..., Any])
|
||||
|
||||
|
||||
def cached_catalogue(fetch: F) -> F:
|
||||
"""Keep a proxy provider's server catalogue for ``CATALOGUE_TTL`` seconds; the next call fetches an empty answer again.
|
||||
|
||||
A server builds its proxy providers on every request, and each build fetches the catalogue.
|
||||
"""
|
||||
|
||||
@functools.wraps(fetch)
|
||||
def wrapper(*args: Any, **kwargs: Any) -> Any:
|
||||
hit = CATALOGUES.get(fetch.__qualname__)
|
||||
if hit and time.monotonic() - hit[0] < CATALOGUE_TTL:
|
||||
return hit[1]
|
||||
value = fetch(*args, **kwargs)
|
||||
if value:
|
||||
CATALOGUES[fetch.__qualname__] = (time.monotonic(), value)
|
||||
return value
|
||||
|
||||
return wrapper # type: ignore[return-value]
|
||||
|
||||
|
||||
class Proxy:
|
||||
|
||||
@@ -14,7 +14,7 @@ import requests
|
||||
|
||||
from unshackle.core.proxies.asn import describe, fold, is_ip, mapped_query, select, split_selector
|
||||
from unshackle.core.proxies.controld import build_query, parse_a_records
|
||||
from unshackle.core.proxies.proxy import Proxy
|
||||
from unshackle.core.proxies.proxy import Proxy, cached_catalogue
|
||||
from unshackle.core.utilities import COUNTRY_CODE_ALIASES
|
||||
|
||||
log = logging.getLogger("proxies.surfsharkvpn")
|
||||
@@ -241,6 +241,7 @@ class SurfsharkVPN(Proxy):
|
||||
return servers
|
||||
|
||||
@staticmethod
|
||||
@cached_catalogue
|
||||
def get_countries() -> list[dict]:
|
||||
"""Get a list of available Countries and their metadata."""
|
||||
res = requests.get(
|
||||
|
||||
@@ -6,7 +6,7 @@ from typing import Iterator, Optional
|
||||
import requests
|
||||
|
||||
from unshackle.core.proxies.asn import describe, mapped_query, resolve_all, select, split_selector
|
||||
from unshackle.core.proxies.proxy import Proxy
|
||||
from unshackle.core.proxies.proxy import Proxy, cached_catalogue
|
||||
|
||||
|
||||
class WindscribeVPN(Proxy):
|
||||
@@ -163,6 +163,7 @@ class WindscribeVPN(Proxy):
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
@cached_catalogue
|
||||
def get_countries() -> list[dict]:
|
||||
"""Get a list of available Countries and their metadata."""
|
||||
res = requests.get(
|
||||
|
||||
Reference in New Issue
Block a user