Skip to content

Commit

Permalink
Respect Retry-After header in Torznab queries (cross-seed#481)
Browse files Browse the repository at this point in the history
# ToDo

- [x] cleanup initial chatgpt code 😅
- [ ] Test
- [ ] verbose logging?


# Git Ref
- Fixes cross-seed#375
  • Loading branch information
mmgoodnow authored Sep 12, 2023
2 parents 3fb2a49 + 618c853 commit aa0d1f0
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/torznab.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import ms from "ms";
import fetch from "node-fetch";
import xml2js from "xml2js";
import { EP_REGEX, SEASON_REGEX } from "./constants.js";
import { EP_REGEX, SEASON_REGEX, USER_AGENT } from "./constants.js";
import { db } from "./db.js";
import { CrossSeedError } from "./errors.js";
import {
Expand All @@ -21,7 +21,6 @@ import {
reformatTitleForSearching,
stripExtension,
} from "./utils.js";
import { USER_AGENT } from "./constants.js";

interface TorznabParams {
t: "caps" | "search" | "tvsearch" | "movie";
Expand Down Expand Up @@ -463,26 +462,35 @@ async function makeRequests(
})
.then((response) => {
if (!response.ok) {
if (response.status === 429) {
const retryAfterSeconds = Number(
response.headers.get("Retry-After")
);

if (!Number.isNaN(retryAfterSeconds)) {
updateIndexerStatus(
IndexerStatus.RATE_LIMITED,
Date.now() + ms("1 hour"),
response.status === 429
? IndexerStatus.RATE_LIMITED
: IndexerStatus.UNKNOWN_ERROR,
Date.now() + ms(`${retryAfterSeconds} seconds`),
[indexers[i].id]
);
} else {
updateIndexerStatus(
IndexerStatus.UNKNOWN_ERROR,
Date.now() + ms("1 hour"),
response.status === 429
? IndexerStatus.RATE_LIMITED
: IndexerStatus.UNKNOWN_ERROR,
response.status === 429
? Date.now() + ms("1 hour")
: Date.now() + ms("10 minutes"),
[indexers[i].id]
);
}
throw new Error(
`request failed with code: ${response.status}`
);
}
return response;
return response.text();
})
.then((r) => r.text())
.then(xml2js.parseStringPromise)
.then(parseTorznabResults)
)
Expand Down

0 comments on commit aa0d1f0

Please sign in to comment.