Skip to content

Commit

Permalink
Cache rejections
Browse files Browse the repository at this point in the history
  • Loading branch information
jdemaeyer committed Feb 8, 2024
1 parent 7151929 commit 8cb3606
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions spoqify/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ async def startup():
app.session = aiohttp.ClientSession(raise_for_status=True)
app.tasks = {}
app.recent_reqs = RecentCounter()
app.rejected_urls = {}


@app.after_serving
Expand Down
12 changes: 12 additions & 0 deletions spoqify/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,14 @@ def _get_url():
@app.route('/redirect')
async def redirect():
url = _get_url()
if e := app.rejected_urls.get(url):
app.recent_reqs.record('cached')
return quart.abort(400, str(e))
try:
task = _get_task(url)
except (Rejected, ValueError) as e:
if isinstance(e, Rejected):
app.rejected_urls[url] = e
return quart.abort(400, str(e))
result_url = await asyncio.shield(task)
return quart.redirect(result_url)
Expand All @@ -103,6 +108,12 @@ async def anonymize():


async def stream_task_status(url):
if e := app.rejected_urls.get(url):
# Most of our rejections are bots requesting the same URL over and
# over, no need to bother Spotify every time
app.recent_reqs.record('cached')
yield encode_event('error', str(e))
return
try:
task = _get_task(url)
except ValueError as e:
Expand All @@ -122,6 +133,7 @@ async def stream_task_status(url):
yield encode_event('queued', task_idx)
except Rejected as e:
app.logger.info("Rejected request for %s: %s", url, e)
app.rejected_urls[url] = e
app.recent_reqs.record('rejected')
yield encode_event('error', str(e))
break
Expand Down

0 comments on commit 8cb3606

Please sign in to comment.