|
1 | | -import { $replica } from "~/db.server"; |
2 | | -import { env } from "~/env.server"; |
3 | | -import { singleton } from "~/utils/singleton"; |
4 | | -import { FEATURE_FLAG } from "~/v3/featureFlags"; |
5 | | -import { makeFlag } from "~/v3/featureFlags.server"; |
6 | | -import { logger } from "../logger.server"; |
7 | 1 | import { type RealtimeEnvironment } from "../realtimeClient.server"; |
8 | | -import { realtimeClient } from "../realtimeClientGlobal.server"; |
9 | | -import { BoundedTtlCache } from "./boundedTtlCache"; |
10 | 2 | import { type RealtimeStreamClient } from "./nativeRealtimeClient.server"; |
11 | 3 | import { getNativeRealtimeClient } from "./nativeRealtimeClientInstance.server"; |
12 | | -import { getShadowRealtimeClient } from "./shadowRealtimeClientInstance.server"; |
13 | | - |
14 | | -type RealtimeBackend = "electric" | "native" | "shadow"; |
15 | | - |
16 | | -// Two gates, both defaulting to the Electric path: the env master switch, then the |
17 | | -// per-org `realtimeBackend` feature flag (cached so long-polls don't hit the DB per request). |
18 | | -const nativeBackendEnabled = env.REALTIME_BACKEND_NATIVE_ENABLED === "1"; |
19 | | - |
20 | | -const flag = singleton("realtimeBackendFlag", () => makeFlag($replica)); |
21 | | -const backendCache = singleton( |
22 | | - "realtimeBackendCache", |
23 | | - () => |
24 | | - new BoundedTtlCache<RealtimeBackend>( |
25 | | - env.REALTIME_BACKEND_FLAG_CACHE_TTL_MS, |
26 | | - env.REALTIME_BACKEND_FLAG_CACHE_MAX_ENTRIES |
27 | | - ) |
28 | | -); |
29 | 4 |
|
| 5 | +// The realtime run feed is served exclusively by the native backend; there is no longer |
| 6 | +// an Electric proxy path or per-org backend selection. The signature is kept async (and |
| 7 | +// still accepts the authenticated environment) so the run routes don't have to change. |
30 | 8 | export async function resolveRealtimeStreamClient( |
31 | 9 | environment: RealtimeEnvironment & { organization?: { featureFlags?: unknown } } |
32 | 10 | ): Promise<RealtimeStreamClient> { |
33 | | - if (!nativeBackendEnabled) { |
34 | | - return realtimeClient; |
35 | | - } |
36 | | - |
37 | | - // The authenticated environment already carries the org's feature flags; pass them |
38 | | - // through so a cache miss doesn't need an extra organization read. |
39 | | - const orgFeatureFlags = environment.organization |
40 | | - ? (environment.organization.featureFlags ?? {}) |
41 | | - : undefined; |
42 | | - |
43 | | - switch (await getRealtimeBackend(environment.organizationId, orgFeatureFlags)) { |
44 | | - case "native": |
45 | | - return getNativeRealtimeClient(); |
46 | | - case "shadow": |
47 | | - // The client is still served Electric; the native path is diffed in the background. |
48 | | - return getShadowRealtimeClient(); |
49 | | - case "electric": |
50 | | - default: |
51 | | - return realtimeClient; |
52 | | - } |
53 | | -} |
54 | | - |
55 | | -async function getRealtimeBackend( |
56 | | - organizationId: string, |
57 | | - orgFeatureFlags: unknown | undefined |
58 | | -): Promise<RealtimeBackend> { |
59 | | - const cached = backendCache.get(organizationId); |
60 | | - if (cached !== undefined) { |
61 | | - return cached; |
62 | | - } |
63 | | - |
64 | | - let backend: RealtimeBackend = "electric"; |
65 | | - |
66 | | - try { |
67 | | - const overrides = |
68 | | - orgFeatureFlags !== undefined |
69 | | - ? orgFeatureFlags |
70 | | - : ( |
71 | | - await $replica.organization.findFirst({ |
72 | | - where: { id: organizationId }, |
73 | | - select: { featureFlags: true }, |
74 | | - }) |
75 | | - )?.featureFlags; |
76 | | - |
77 | | - backend = await flag({ |
78 | | - key: FEATURE_FLAG.realtimeBackend, |
79 | | - defaultValue: "electric", |
80 | | - overrides: (overrides as Record<string, unknown>) ?? {}, |
81 | | - }); |
82 | | - } catch (error) { |
83 | | - // Never let a flag lookup failure break the realtime feed. |
84 | | - logger.error("[resolveRealtimeStreamClient] failed to resolve realtimeBackend flag", { |
85 | | - organizationId, |
86 | | - error, |
87 | | - }); |
88 | | - backend = "electric"; |
89 | | - } |
90 | | - |
91 | | - backendCache.set(organizationId, backend); |
92 | | - return backend; |
| 11 | + return getNativeRealtimeClient(); |
93 | 12 | } |
0 commit comments