fix(scrape): sleep after failure too, back off longer on 429 - #192
Merged
Conversation
Japan/Twitrans の Handler は Sleep::for(1)->second() が try 節の成功 パスにしかなく、失敗時は間隔を空けずに次の URL へ進んでいた。 wikiwiki.jp (Twitrans) で一度 429 を受けると、間隔なしの連続アクセス がそのまま続き、以降ほぼ全URLが 429 で失敗する連鎖が起きていた (prod 8/19 ログで286件失敗)。 wikiwiki.jp 側に公開されたレート制限値は無い(robots.txt に Crawl-delay 指定なし、Cloudflare 経由)ため、固定の「安全な間隔」を 決め打ちするのではなく、失敗時にも通常間隔(2秒)を必ず空け、429を 受けた直後だけ長めのクールダウン(15秒)を挟むようにした。 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
ローカルから php artisan app:scrape twitrans を実際に実行して検証した ところ、2秒間隔+429後15秒クールダウンでは不十分だった。最初の4件は バーストで通るが、それ以降は2秒間隔だとトークンが回復しきらず断続的 に429になり、15秒待っても直後の1件しか通らない(トークンバケット的な 挙動)。 Twitrans (wikiwiki.jp) のみ通常間隔を10秒、429後クールダウンを60秒に 引き上げる。Japan (japanese.simutrans.com) 側は429が一度も観測されて いないため据え置き。 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
…tests コードレビュー指摘への対応。 - Japan/Twitrans の Handler に一字一句同じ sleepAfterFailure() が重複 していたため、App\Actions\Scrape\SleepBetweenRequests に抽出。間隔/ クールダウン秒数は各 Handler の定数のまま呼び出し時に渡す形にし、 サイトごとの値の違い(Japan 2/15秒、Twitrans 10/60秒)は維持。 あわせて定数名を IntervalSeconds → INTERVAL_SECONDS 等、同ディレク トリの既存定数(TOP_URL 等)に合わせた UPPER_SNAKE_CASE に統一。 - Scrape/Japan の HandlerFailureTest・HandlerIsolationTest が Sleep::fake() していなかったため、失敗時にも sleep するようになった 前回PRの変更で実際に数秒待つようになっていた。Sleep::fake() を追加 して解消(該当テストの実行時間 ~8秒 → ~0.1秒)。 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
HandlerはSleep::for(1)->second()が成功パスにしかなく、失敗時は間隔なしで次の URL に進んでいたTest plan
Twitrans/HandlerRateLimitTestで、成功時2秒・429直後15秒のスリープ順序をSleep::fake()/assertSequenceで検証php artisan test --filter=Scrape全14件パスphpstan analyseエラーなしpint --testパス🤖 Generated with Claude Code