Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
506 changes: 374 additions & 132 deletions src/github/app.ts

Large diffs are not rendered by default.

44 changes: 44 additions & 0 deletions src/selfhost/redis-response-cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Redis-backed GitHub GET-response cache (#perf). Optional: when REDIS_URL + GITHUB_CACHE_TTL_SECONDS>0 are set,
// the self-host caches safe GitHub API GET responses for a short TTL. A single review pass makes ~24 GitHub
// fetches (PR data, files, user/org lookups) — many repeated — all network-bound and rate-limited. A short-TTL
// cache dedups those within and across rapid re-reviews, cutting latency and rate-limit pressure, and it
// persists across restarts. Keyed by URL; the TTL bounds staleness. Only the status + body + content-type are
// stored — NOT rate-limit headers (a cache hit consumed no quota) or content-encoding (the body is decoded).
import type { Redis } from "ioredis";
import type { CachedGitHubResponse, GitHubResponseCache } from "../github/app";

const keyFor = (url: string): string => `gh:resp:${url}`;

export function createRedisResponseCache(
redis: Redis,
ttlSeconds: number,
): GitHubResponseCache {
return {
async get(url: string) {
const raw = await redis.get(keyFor(url));
if (!raw) return null;
try {
const value = JSON.parse(raw) as Partial<CachedGitHubResponse>;
return typeof value.status === "number" &&
typeof value.body === "string" &&
typeof value.contentType === "string"
? {
status: value.status,
body: value.body,
contentType: value.contentType,
}
: null;
} catch {
return null;
}
},
async set(url: string, value: CachedGitHubResponse) {
await redis.set(
keyFor(url),
JSON.stringify(value),
"EX",
Math.max(1, ttlSeconds),
);
},
};
}
48 changes: 48 additions & 0 deletions src/selfhost/redis-token-cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Redis-backed installation-token store (#perf). Optional: when REDIS_URL is set, the self-host backs
// github/app.ts's installation-token cache with Redis so warm tokens SURVIVE restarts/deploys. The default
// in-isolate Map dies on every restart, so a brokered self-host re-mints a token (an Orb round-trip) on the
// next call after each cold start — wasteful when the container restarts often. Keyed by installation id, with
// the TTL set to the token's own remaining lifetime so the entry self-expires exactly when the token does.
// Also makes the cache shared across instances if the stack is ever scaled horizontally.
import type { Redis } from "ioredis";
import type { InstallationTokenStore } from "../github/app";

const keyFor = (installationId: number): string =>
`gh:insttoken:${installationId}`;

export function createRedisTokenCache(redis: Redis): InstallationTokenStore {
return {
async get(installationId: number) {
const raw = await redis.get(keyFor(installationId));
if (!raw) return null;
try {
const value = JSON.parse(raw) as {
token?: unknown;
expiresAtMs?: unknown;
};
return typeof value.token === "string" &&
typeof value.expiresAtMs === "number"
? { token: value.token, expiresAtMs: value.expiresAtMs }
: null;
} catch {
return null;
}
},
async set(
installationId: number,
value: { token: string; expiresAtMs: number },
) {
// Floor at 1s; a token already inside the safety margin still gets cached briefly rather than not at all.
const ttlSeconds = Math.max(
1,
Math.floor((value.expiresAtMs - Date.now()) / 1000),
);
await redis.set(
keyFor(installationId),
JSON.stringify(value),
"EX",
ttlSeconds,
);
},
};
}
23 changes: 22 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,12 +323,33 @@ async function main(): Promise<void> {
const { createRedisCache } = await import("./selfhost/redis-cache");
rateLimiter = createRedisRateLimiter(redisClient);
webhookCache = createRedisCache(redisClient);
// Persist the installation-token cache in Redis so warm GitHub App tokens survive restarts/deploys and are
// shared across replicas (the in-isolate Map otherwise re-mints — an Orb round-trip — per replica/cold start).
const { createRedisTokenCache } =
await import("./selfhost/redis-token-cache");
const { setInstallationTokenStore, setGitHubResponseCache } =
await import("./github/app");
setInstallationTokenStore(createRedisTokenCache(redisClient));
// Short-TTL cache for safe GitHub GET responses (dedups the ~24 reads per review). Default 20s; 0 disables.
const ghCacheTtl = Math.max(
0,
Number(process.env.GITHUB_CACHE_TTL_SECONDS ?? "20"),
);
if (ghCacheTtl > 0) {
const { createRedisResponseCache } =
await import("./selfhost/redis-response-cache");
setGitHubResponseCache(createRedisResponseCache(redisClient, ghCacheTtl));
}
readinessProbes.push({
name: "redis",
check: () => withTimeout(redisClient.ping().then(() => true)),
});
console.log(
JSON.stringify({ event: "selfhost_rate_limiter", backend: "redis" }),
JSON.stringify({
event: "selfhost_rate_limiter",
backend: "redis",
githubResponseCacheTtl: ghCacheTtl,
}),
);
}

Expand Down
Loading
Loading