Enhance PIA rate limit handling in gluetun WireGuard rotator. Introduce persistent cooldown management and improve retry logic for token requests. Update README to reflect new RATE_LIMIT_PATH variable and clarify cooldown behavior.
Build and Push Docker Images / build-and-push (push) Successful in 20s
Build and Push Docker Images / build-and-push (push) Successful in 20s
This commit is contained in:
@@ -13,8 +13,9 @@ from zoneinfo import ZoneInfo
|
||||
|
||||
from croniter import croniter
|
||||
|
||||
# Matches rotate.py / pia.py EXIT_RATE_LIMITED (EX_TEMPFAIL)
|
||||
EXIT_RATE_LIMITED = 75
|
||||
import pia
|
||||
|
||||
EXIT_RATE_LIMITED = pia.EXIT_RATE_LIMITED
|
||||
|
||||
CRON_MACROS = {
|
||||
"@yearly": "0 0 1 1 *",
|
||||
@@ -79,16 +80,25 @@ def sleep_until_next_rotate(expr: str) -> None:
|
||||
time.sleep(wait_s)
|
||||
|
||||
|
||||
def wait_for_rate_limit_cooldown() -> None:
|
||||
remaining = pia.rate_limit_remaining_seconds()
|
||||
if remaining <= 0:
|
||||
return
|
||||
log(f"PIA rate-limit cooldown active; waiting {remaining}s before retry")
|
||||
time.sleep(remaining)
|
||||
|
||||
|
||||
def run_rotation(reason: str) -> None:
|
||||
wait_s = int(os.environ.get("RATE_LIMIT_WAIT_SECONDS", "3600"))
|
||||
while True:
|
||||
wait_for_rate_limit_cooldown()
|
||||
log(reason)
|
||||
result = subprocess.run(["/usr/local/bin/rotate.py"], check=False)
|
||||
if result.returncode == 0:
|
||||
return
|
||||
if result.returncode == EXIT_RATE_LIMITED:
|
||||
log(f"PIA rate-limited (too many attempts); waiting {wait_s}s before retry")
|
||||
time.sleep(wait_s)
|
||||
# rotate.py / pia.py already marked the cooldown file.
|
||||
if pia.rate_limit_remaining_seconds() <= 0:
|
||||
pia.mark_rate_limited(reason="rotation exit code 75")
|
||||
reason = "Retrying rotation after rate-limit wait"
|
||||
continue
|
||||
raise SystemExit(f"Rotation failed with exit code {result.returncode}")
|
||||
|
||||
Reference in New Issue
Block a user