r/protonvpn: watchdog: Improve error handling
This resolves two issues with fetching the Proton VPNserver list: 1. If a connection error occurs when fetching the list, it will be ignored, just as with HTTP errors 2. If any errors are encountered when fetching the list, and a valid cache was loaded, its contents are returned, regardless of the timestamp of the cache file.collectd-buildroot
parent
5485fc6f93
commit
5e2cfee8a1
|
@ -111,34 +111,36 @@ class AsyncDaemon(BaseAsyncDaemon):
|
||||||
self.bad_servers: Set[str] = set()
|
self.bad_servers: Set[str] = set()
|
||||||
|
|
||||||
async def get_serverlist(self) -> Optional[Json]:
|
async def get_serverlist(self) -> Optional[Json]:
|
||||||
|
data: Optional[Json] = None
|
||||||
try:
|
try:
|
||||||
f = open(self.SERVER_LIST, encoding='utf-8')
|
f = open(self.SERVER_LIST, encoding='utf-8')
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
with f:
|
with f:
|
||||||
|
try:
|
||||||
|
data = json.load(f)
|
||||||
|
except ValueError as e:
|
||||||
|
log.warning('Failed to parse server list: %s', e)
|
||||||
st = os.fstat(f.fileno())
|
st = os.fstat(f.fileno())
|
||||||
now = time.time()
|
now = time.time()
|
||||||
if st.st_mtime > now - 3600:
|
if st.st_mtime > now - 3600:
|
||||||
log.info('Using cached server list from %s', f.name)
|
log.info('Using cached server list from %s', f.name)
|
||||||
try:
|
return data
|
||||||
return json.load(f)
|
|
||||||
except ValueError as e:
|
|
||||||
log.warning('Failed to parse server list: %s', e)
|
|
||||||
async with httpx.AsyncClient() as client:
|
async with httpx.AsyncClient() as client:
|
||||||
log.info('Fetching server list from %s', self.SERVER_LIST_URL)
|
log.info('Fetching server list from %s', self.SERVER_LIST_URL)
|
||||||
r: httpx.Response = await client.get(self.SERVER_LIST_URL)
|
r: httpx.Response
|
||||||
if r.status_code != HTTPStatus.OK:
|
try:
|
||||||
log.error(
|
r = await client.get(self.SERVER_LIST_URL)
|
||||||
'Failed to fetch server list: HTTP status %s',
|
r.raise_for_status()
|
||||||
r.status_code,
|
except httpx.HTTPError as e:
|
||||||
)
|
log.error('Failed to fetch server list: %s', e)
|
||||||
return None
|
return data
|
||||||
try:
|
try:
|
||||||
data = r.json()
|
data = r.json()
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
log.error('Failed to parse server list: %s', e)
|
log.error('Failed to parse server list: %s', e)
|
||||||
return None
|
return data
|
||||||
path = Path(self.SERVER_LIST)
|
path = Path(self.SERVER_LIST)
|
||||||
path.parent.mkdir(parents=True, exist_ok=True)
|
path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
with path.open('w', encoding='utf-8') as f:
|
with path.open('w', encoding='utf-8') as f:
|
||||||
|
|
Loading…
Reference in New Issue