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
4 changes: 2 additions & 2 deletions src/pages/monitor.astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
import { isNull } from "drizzle-orm";
import { and, eq, isNull } from "drizzle-orm";
import { MonitorWorkspace } from "@/components/monitor/MonitorWorkspace";
import AppLayout from "@/layouts/AppLayout.astro";
import { db } from "@/server/db/client";
Expand Down Expand Up @@ -53,7 +53,7 @@ if (initialTab === "queue" && jobIdParam) {
const allProjects = await db
.select({ id: projects.id, name: projects.name, icon: projects.icon })
.from(projects)
.where(isNull(projects.deletedAt));
.where(and(eq(projects.ownerUserId, user.id), isNull(projects.deletedAt)));
---

<AppLayout title="Monitor">
Expand Down
50 changes: 50 additions & 0 deletions src/server/tailscale/urls.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { beforeEach, describe, expect, it, vi } from "vitest";

const { getTailscaleConfig } = vi.hoisted(() => ({
getTailscaleConfig: vi.fn(),
}));

vi.mock("./config", () => ({ getTailscaleConfig }));

import { getTailscaleProjectUrl, invalidateTailscaleUrlCache } from "./urls";

describe("getTailscaleProjectUrl", () => {
beforeEach(() => {
invalidateTailscaleUrlCache();
getTailscaleConfig.mockReset();
getTailscaleConfig.mockResolvedValue({
enabled: true,
authKey: null,
hostname: "doce",
tailnetName: "example.ts.net",
});
});

it("shares one config lookup across concurrent project URL builds", async () => {
const urls = await Promise.all(
Array.from({ length: 6 }, (_, index) =>
getTailscaleProjectUrl(
`project-${index}`,
"preview",
`project-${index}`,
),
),
);

expect(getTailscaleConfig).toHaveBeenCalledTimes(1);
expect(urls).toEqual([
"https://project-0-proje-preview.example.ts.net",
"https://project-1-proje-preview.example.ts.net",
"https://project-2-proje-preview.example.ts.net",
"https://project-3-proje-preview.example.ts.net",
"https://project-4-proje-preview.example.ts.net",
"https://project-5-proje-preview.example.ts.net",
]);
});

it("preserves production URL construction", async () => {
await expect(
getTailscaleProjectUrl("my-project", "production", "abcde12345"),
).resolves.toBe("https://my-project-abcde.example.ts.net");
});
});
36 changes: 15 additions & 21 deletions src/server/tailscale/urls.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
import { getOrSet, invalidate } from "@/server/cache/memory";
import { getTailscaleConfig } from "./config";

let cachedConfig: {
enabled: boolean;
tailnetName: string | null;
} | null = null;

let cacheExpiry = 0;

const URL_CONFIG_CACHE_KEY = "tailscale:url-config";
const CACHE_TTL_MS = 30_000;

async function getCachedConfig() {
const now = Date.now();
if (cachedConfig && now < cacheExpiry) {
return cachedConfig;
}
interface UrlConfig {
enabled: boolean;
tailnetName: string | null;
}

const config = await getTailscaleConfig();
cachedConfig = {
enabled: config.enabled,
tailnetName: config.tailnetName,
};
cacheExpiry = now + CACHE_TTL_MS;
return cachedConfig;
function getCachedConfig(): Promise<UrlConfig> {
return getOrSet(URL_CONFIG_CACHE_KEY, { ttlMs: CACHE_TTL_MS }, async () => {
const config = await getTailscaleConfig();
return {
enabled: config.enabled,
tailnetName: config.tailnetName,
};
});
}

/**
Expand Down Expand Up @@ -64,6 +59,5 @@ export async function getTailscaleAppUrl(): Promise<string | null> {

/** Invalidate the cached config (call after connect/disconnect). */
export function invalidateTailscaleUrlCache(): void {
cachedConfig = null;
cacheExpiry = 0;
invalidate(URL_CONFIG_CACHE_KEY);
}
Loading