mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-21 07:59:58 -04:00
Hold repeated update checks back, and say when GitHub takes them again.
Every selection of the Updates row started a request to GitHub, which allows an address 60 an hour: a player who selected it 25 times in 90 seconds had every check of the next hour refused. The row and F5 now start no check within a minute of the last one. A refused check said "try again in an hour" whatever the answer held. It now names the time from the answer's X-RateLimit-Reset or Retry-After. A failed check wrote two tracebacks, 22 lines, into launcher.log each time; a FeedError is now one line with its message and its cause. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
@@ -14,6 +14,7 @@ The feed (app/<version>/feed.json, or the player's override in settings) is eith
|
||||
"""
|
||||
import json
|
||||
import re
|
||||
import time
|
||||
import urllib.parse
|
||||
from pathlib import Path
|
||||
|
||||
@@ -78,6 +79,30 @@ def _check_manifest(m, where):
|
||||
return m
|
||||
|
||||
|
||||
def retry_text(headers, now=None):
|
||||
"""When GitHub takes requests again, for the message of a refused one: "after 14:32 (in 25 minutes)". From
|
||||
X-RateLimit-Reset, the time the hourly allowance is whole again, when the response says the allowance is used
|
||||
up (X-RateLimit-Remaining: 0), else from Retry-After, the seconds to wait. '' when the response says neither
|
||||
or says something that cannot be right (more than a day)."""
|
||||
now = time.time() if now is None else now
|
||||
try:
|
||||
if headers.get('X-RateLimit-Remaining') == '0' and headers.get('X-RateLimit-Reset'):
|
||||
wait = int(headers.get('X-RateLimit-Reset')) - now
|
||||
elif headers.get('Retry-After'):
|
||||
wait = int(headers.get('Retry-After'))
|
||||
else:
|
||||
return ''
|
||||
except (AttributeError, TypeError, ValueError):
|
||||
return ''
|
||||
if wait > 86400:
|
||||
return ''
|
||||
if wait <= 0: # the allowance is whole again by this PC's clock
|
||||
return 'in a minute'
|
||||
minutes = int(-(-wait // 60))
|
||||
return (f'after {time.strftime("%H:%M", time.localtime(now + wait))} '
|
||||
f'(in {minutes} minute{"" if minutes == 1 else "s"})')
|
||||
|
||||
|
||||
def _github(feed, channel, token, version):
|
||||
api = feed.get('api', GITHUB_API).rstrip('/')
|
||||
headers = {'Accept': 'application/vnd.github+json', 'X-GitHub-Api-Version': '2022-11-28'}
|
||||
@@ -89,8 +114,9 @@ def _github(feed, channel, token, version):
|
||||
code = getattr(e, 'code', None)
|
||||
if code == 404:
|
||||
raise FeedError(f'no releases found for {feed["repo"]} (a private repository needs a token)') from e
|
||||
if code == 403:
|
||||
raise FeedError('GitHub refused the request (rate limit); try again in an hour') from e
|
||||
if code in (403, 429): # GitHub answers either when the allowance of requests is used up
|
||||
when = retry_text(getattr(e, 'headers', None))
|
||||
raise FeedError(f'GitHub refused the request (rate limit); try again {when or "in an hour"}') from e
|
||||
raise FeedError(f'GitHub could not be reached: {e}') from e
|
||||
# The API lists the newest release first: the first one the channel takes that has a manifest is the answer.
|
||||
for r in releases:
|
||||
|
||||
@@ -13,6 +13,16 @@ RECHECK = 3600 # seconds between update checks while the launcher stays open,
|
||||
|
||||
PENDING, RUNNING, DONE, SKIPPED, FAILED = 'pending', 'running', 'done', 'skipped', 'failed'
|
||||
|
||||
# Failures whose message says all there is to say: the log gets one line for them (failure_line), not a traceback.
|
||||
# A refused update check wrote 22 lines of two tracebacks into launcher.log each time.
|
||||
PLAIN_FAILURES = (feed.FeedError,)
|
||||
|
||||
|
||||
def failure_line(e):
|
||||
"""A failure in one line for the log: its kind, its message and what it came from."""
|
||||
cause = e.__cause__
|
||||
return f'{type(e).__name__}: {e}' + (f' ({type(cause).__name__}: {cause})' if cause else '')
|
||||
|
||||
STEPS = {
|
||||
'tools': ('Gather Build Tools', 'Downloading the compiler and libraries the build uses. This happens once and '
|
||||
'takes about 170 MB.'),
|
||||
@@ -230,7 +240,7 @@ class Job:
|
||||
self.result = 'cancelled'
|
||||
else:
|
||||
self.result, self.error = 'failed', str(e) or type(e).__name__
|
||||
self.ctx.log(traceback.format_exc())
|
||||
self.ctx.log(failure_line(e) if isinstance(e, PLAIN_FAILURES) else traceback.format_exc())
|
||||
finally:
|
||||
for s in self.steps:
|
||||
if s.state == RUNNING:
|
||||
|
||||
@@ -184,6 +184,11 @@ def drop_partial_image(game_dir):
|
||||
pass
|
||||
|
||||
|
||||
# Seconds after an update check before the Updates row or F5 starts another. Each check is a request to GitHub,
|
||||
# which allows 60 an hour for an address: a player who selected the row 25 times in 90 seconds used the allowance
|
||||
# up, and every check of the next hour was refused.
|
||||
FORCED_CHECK_PAUSE = 60
|
||||
|
||||
# How long an update's rebuild takes, for the Updates row. No single figure holds: a version that changed little
|
||||
# compiles a few files and links, minutes; one that changed the lifter translates and compiles the game's code again,
|
||||
# and one that changed what the shaders are built with builds all of them into the graphics driver's cache again
|
||||
@@ -1312,6 +1317,11 @@ class App:
|
||||
return
|
||||
if not force and time.time() - self.settings.get('last_check', 0) < 600 and self.release:
|
||||
return
|
||||
if force and time.time() - self.check_started < FORCED_CHECK_PAUSE:
|
||||
self.message = 'NX2navi looked for updates less than a minute ago.'
|
||||
if self.screen == 'home':
|
||||
self.show('home', self.focus)
|
||||
return
|
||||
self.check_error = ''
|
||||
self.check_started = time.time()
|
||||
self.check = jobs.CheckJob(self.ctx).start()
|
||||
|
||||
Reference in New Issue
Block a user