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
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { getClickhouseAdminClient } from "@/lib/clickhouse";
import { isPreviewModeEnabled } from "@/lib/preview-mode";
import { seedDummyProject } from "@/lib/seed-dummy-data";
import { getPrismaClientForTenancy } from "@/prisma-client";
import { createSmartRouteHandler } from "@/route-handlers/smart-route-handler";
import { adaptSchema, clientOrHigherAuthTypeSchema, yupNumber, yupObject, yupString } from "@stackframe/stack-shared/dist/schema-fields";
import { StatusError } from "@stackframe/stack-shared/dist/utils/errors";
import { ignoreUnhandledRejection } from "@stackframe/stack-shared/dist/utils/promises";

export const POST = createSmartRouteHandler({
metadata: {
Expand Down Expand Up @@ -35,6 +37,17 @@ export const POST = createSmartRouteHandler({
throw new StatusError(StatusError.Forbidden, "This endpoint is only available in preview mode");
}

// Pre-warm the ClickHouse Cloud connection, then hand the same client to
// seedDummyProject so every analytics insert reuses it. The first insert
// otherwise pays a one-time ~0.7s cold cost (idle-service wake-up + TLS).
// Firing a trivial query now — unawaited — overlaps that wake-up and the
// TLS handshake with the Postgres-heavy seeding below; threading the warmed
// client through means the handshake is established exactly once.
Comment thread
BilalG1 marked this conversation as resolved.
const clickhouseClient = getClickhouseAdminClient();
const clickhouseWarmup = clickhouseClient
.command({ query: "SELECT 1" });
ignoreUnhandledRejection(clickhouseWarmup);

const userId = auth.user.id;
const prisma = await getPrismaClientForTenancy(auth.tenancy);

Expand All @@ -58,8 +71,13 @@ export const POST = createSmartRouteHandler({
oauthProviderIds: ['github', 'google', 'microsoft', 'spotify'],
excludeAlphaApps: true,
skipGithubConfigSource: true,
clickhouseClient,
});

// Settle the warm-up promise (long since resolved by now) so it does not
// float past the handler return.
await clickhouseWarmup;

return {
statusCode: 200,
bodyType: "json",
Expand Down
5 changes: 5 additions & 0 deletions apps/backend/src/lib/clickhouse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { createClient, type ClickHouseClient, type ClickHouseSettings } from "@c
import { getEnvVariable } from "@stackframe/stack-shared/dist/utils/env";
import { StackAssertionError } from "@stackframe/stack-shared/dist/utils/errors";

// Re-exported so other modules can hold a typed ClickHouse client (e.g. to
// thread a single warmed client through helpers) without taking a direct
// dependency on the @clickhouse/client package.
export type { ClickHouseClient } from "@clickhouse/client";

function getAdminAuth() {
return {
username: getEnvVariable("STACK_CLICKHOUSE_ADMIN_USER", "stackframe"),
Expand Down
Loading
Loading