discordjs/discord.js · error · Error

Cannot set an interval greater than 4 hours

Error message

Cannot set an interval greater than 4 hours

What it means

REST's setupSweepers() enforces that the hashSweepInterval and handlerSweepInterval options never exceed 14,400,000 ms (4 hours). Intervals longer than that would let stale rate-limit hash data linger too long, so validateMaxInterval throws this Error at construction time if RESTOptions.hashSweepInterval exceeds the cap.

Source

Thrown at packages/rest/src/lib/REST.ts:94

	public readonly options: RESTOptions;

	public constructor(options: Partial<RESTOptions> = {}) {
		super();
		this.cdn = new CDN(options);
		this.options = { ...DefaultRestOptions, ...options };
		this.globalRemaining = Math.max(1, this.options.globalRequestsPerSecond);
		this.agent = options.agent ?? null;

		// Start sweepers
		this.setupSweepers();
	}

	private setupSweepers() {
		// eslint-disable-next-line unicorn/consistent-function-scoping
		const validateMaxInterval = (interval: number) => {
			if (interval > 14_400_000) {
				throw new Error('Cannot set an interval greater than 4 hours');
			}
		};

		if (this.options.hashSweepInterval !== 0 && this.options.hashSweepInterval !== Number.POSITIVE_INFINITY) {
			validateMaxInterval(this.options.hashSweepInterval);
			this.hashTimer = setInterval(() => {
				const sweptHashes = new Collection<string, HashData>();
				const currentDate = Date.now();

				// Begin sweeping hash based on lifetimes
				this.hashes.sweep((val, key) => {
					// `-1` indicates a global hash
					if (val.lastAccess === -1) return false;

					// Check if lifetime has been exceeded
					const shouldSweep = Math.floor(currentDate - val.lastAccess) > this.options.hashLifetime;

					// Add hash to collection of swept hashes

View on GitHub (pinned to a81ed8a306)

Solutions

  1. Lower hashSweepInterval to at most 14_400_000 (4 hours); the default is exactly 14_400_000, so simply omit the option.
  2. To disable hash sweeping, set hashSweepInterval to 0 or Number.POSITIVE_INFINITY — those are the only exempt values.
  3. Check unit conversion: values must be in milliseconds, so 4 hours = 4 * 60 * 60 * 1000.
  4. Validate any config-driven interval with Math.min(interval, 14_400_000) before passing it to the REST constructor.

Example fix

// before
const rest = new REST({ hashSweepInterval: 86_400_000 }); // throws: > 4 hours
// after
const rest = new REST({ hashSweepInterval: Math.min(config.sweepMs, 14_400_000) });
Defensive patterns

Strategy: validation

Validate before calling

const MAX_SWEEP_INTERVAL = 14_400_000;
function assertSweepInterval(ms: number) {
  if (ms !== 0 && ms !== Number.POSITIVE_INFINITY && ms > MAX_SWEEP_INTERVAL) {
    throw new RangeError(`hashSweepInterval must be <= 4 hours (${MAX_SWEEP_INTERVAL} ms); got ${ms}`);
  }
}

Type guard

const isValidSweepInterval = (v: unknown): v is number =>
  typeof v === 'number' && (v === 0 || v === Number.POSITIVE_INFINITY || (v > 0 && v <= 14_400_000));

Try / catch

try {
  const rest = new REST({ hashSweepInterval: config.hashSweepMs });
} catch (error) {
  if (error.message.includes('greater than 4 hours')) {
    logger.warn('hashSweepInterval too large, falling back to default');
    rest = new REST();
  } else {
    throw error;
  }
}

Prevention

When it happens

Trigger: Constructing `new REST({ hashSweepInterval: <value > 14_400_000 })` — e.g. 24 hours (86_400_000), Infinity (note: only exact Number.POSITIVE_INFINITY is exempted, any larger finite number throws), or a computed interval from config in seconds/minutes that wasn't converted to milliseconds correctly (e.g. passing 24 * 60 * 60 for hours as if seconds... or passing seconds where ms expected such as 50_000_000).

Common situations: Configuring sweep intervals in minutes/seconds but forgetting to multiply by 1000; copying hashLifetime (24h) into hashSweepInterval; trying to effectively disable sweeping with a huge number instead of 0 or Infinity.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


AI-assisted analysis of discordjs/discord.js@a81ed8a306 (2026-08-30). Data as JSON: /api/errors/aa3cffe02dc35a51. Report an issue: GitHub.