fix: Exit when a Redis connection stays unavailable (#14114)

Fixes OUTLINE-CLOUD-BZ6
This commit is contained in:
Tom Moor authored and GitHub committed 2026-10-17 09:37:03 -04:00
1 parent dc6e05aab1
commit 00c37669c4
3 files changed
+103 -5

No files matched your search

+11
View File
@@ -221,6 +221,17 @@ export class Environment {
10
);
/**
* The time in milliseconds that a redis connection can stay unavailable
* before the process exits, so that the process manager can restart it.
*/
@IsNumber()
@Min(1000)
public REDIS_UNAVAILABLE_TIMEOUT = parseInt(
environment.REDIS_UNAVAILABLE_TIMEOUT || "300000",
10
);
/**
* The fully qualified, external facing domain name of the server.
* If not set, will be derived from HEROKU_APP_NAME for Heroku deployments.
+53
View File
@@ -1,4 +1,6 @@
import { randomUUID } from "node:crypto";
import env from "@server/env";
import Logger from "@server/logging/Logger";
import Redis from "./redis";
describe("RedisAdapter#zaddWithSequence", () => {
@@ -66,3 +68,54 @@ describe("RedisAdapter#zaddWithSequence", () => {
).rejects.toThrow("Redis unavailable");
});
});
describe("RedisAdapter unavailable timeout", () => {
let client: Redis;
beforeEach(() => {
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
vi.spyOn(Logger, "fatal").mockImplementation(() => undefined);
client = new Redis(env.REDIS_URL);
});
afterEach(() => {
client.disconnect();
vi.useRealTimers();
vi.restoreAllMocks();
});
it("exits when the connection does not recover", () => {
client.status = "reconnecting";
client.emit("reconnecting");
vi.advanceTimersByTime(env.REDIS_UNAVAILABLE_TIMEOUT);
expect(Logger.fatal).toHaveBeenCalledWith(
"Redis connection unavailable",
expect.any(Error),
expect.objectContaining({ status: "reconnecting" })
);
});
it("does not exit when the connection recovers", () => {
client.status = "reconnecting";
client.emit("reconnecting");
vi.advanceTimersByTime(env.REDIS_UNAVAILABLE_TIMEOUT / 2);
client.status = "ready";
client.emit("ready");
vi.advanceTimersByTime(env.REDIS_UNAVAILABLE_TIMEOUT);
expect(Logger.fatal).not.toHaveBeenCalled();
});
it("does not exit after the connection is closed", () => {
client.status = "reconnecting";
client.emit("reconnecting");
client.status = "end";
client.emit("end");
vi.advanceTimersByTime(env.REDIS_UNAVAILABLE_TIMEOUT);
expect(Logger.fatal).not.toHaveBeenCalled();
});
});
+39 -5
View File
@@ -77,13 +77,47 @@ export default class RedisAdapter extends Redis {
this.setMaxListeners(100);
this.on("error", (err) => {
if (err.name === "MaxRetriesPerRequestError") {
Logger.fatal("Redis maximum retries exceeded", err);
} else {
Logger.error("Redis error", err);
}
Logger.error("Redis error", err);
});
// Exit when the connection stays unavailable, as a restart can recover a
// process that is unable to reconnect by itself.
let unavailableTimeout: NodeJS.Timeout | undefined;
const startUnavailableTimeout = () => {
if (unavailableTimeout) {
return;
}
unavailableTimeout = setTimeout(() => {
unavailableTimeout = undefined;
if (this.status === "ready" || this.status === "end") {
return;
}
Logger.fatal(
"Redis connection unavailable",
new Error(
`Redis connection not ready after ${env.REDIS_UNAVAILABLE_TIMEOUT}ms`
),
{ connectionName, status: this.status }
);
}, env.REDIS_UNAVAILABLE_TIMEOUT);
// Don't keep the Node event loop alive solely for the timeout.
unavailableTimeout.unref();
};
const clearUnavailableTimeout = () => {
clearTimeout(unavailableTimeout);
unavailableTimeout = undefined;
};
this.on("connecting", startUnavailableTimeout);
this.on("reconnecting", startUnavailableTimeout);
this.on("ready", clearUnavailableTimeout);
this.on("end", clearUnavailableTimeout);
// Skip the healthcheck on connections reserved for blocking or pub/sub
// operations (signalled via maxRetriesPerRequest: null). A PING issued on
// those connections queues behind the in-flight blocking command and would