Skip to content

Commit 146821b

Browse files
authored
Cache service URLs (#4466)
* Cache service URLs * Increase cache ttl, invalidate Service URLs Cache
1 parent fda0bd2 commit 146821b

2 files changed

Lines changed: 57 additions & 1 deletion

File tree

packages/playground/src/config.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,21 @@ const GLOBAL_COMPONENTS: { [key: string]: Component } = {
2828
TfSelectCountry: defineAsyncComponent(() => import("./components/node_selector/select_location_internals/TfSelectCountry.vue")), // prettier-ignore
2929
};
3030

31+
const SERVICE_URLS_CACHE_KEY = "service_urls_cache";
32+
const SERVICE_URLS_CACHE_TTL = 60 * 60 * 1000; // 1 hour
33+
34+
/**
35+
* Invalidates the service URLs cache.
36+
* Should be called when service connection fails to ensure fresh URLs are fetched on next attempt.
37+
*/
38+
export function invalidateServiceUrlsCache() {
39+
try {
40+
sessionStorage.removeItem(SERVICE_URLS_CACHE_KEY);
41+
} catch (error) {
42+
console.warn("[invalidateServiceUrlsCache] Failed to invalidate service URLs cache", error);
43+
}
44+
}
45+
3146
export function defineGlobals(app: App<Element>): void {
3247
defineGlobalComponents(app);
3348
defineGlobalProps(app);
@@ -81,6 +96,20 @@ function defineGlobalProps(app: App<Element>) {
8196
* @returns A promise that resolves to `true` if all service URLs are successfully set, or `false` if any service URL is missing.
8297
*/
8398
export async function setGlobalEnv() {
99+
try {
100+
const cachedRaw = sessionStorage.getItem(SERVICE_URLS_CACHE_KEY);
101+
if (cachedRaw) {
102+
const cached = JSON.parse(cachedRaw) as { urls: Record<string, string>; timestamp: number };
103+
if (Date.now() - cached.timestamp < SERVICE_URLS_CACHE_TTL) {
104+
Object.assign(window.env, cached.urls);
105+
return true;
106+
}
107+
}
108+
} catch (error) {
109+
// Ignore cache errors and fall back to live checks
110+
console.warn("[setGlobalEnv] Failed to read service URLs cache", error);
111+
}
112+
84113
const {
85114
GRIDPROXY_STACKS,
86115
GRAPHQL_STACKS,
@@ -106,6 +135,7 @@ export async function setGlobalEnv() {
106135
const result = await urlManger.getAvailableServicesStack();
107136

108137
if (Object.values(result).includes(null)) {
138+
invalidateServiceUrlsCache();
109139
window.$$showMonitorError(result);
110140
return false;
111141
}
@@ -118,5 +148,26 @@ export async function setGlobalEnv() {
118148
window.env.ACTIVATION_SERVICE_URL = Activation!;
119149
window.env.RELAY_DOMAIN = RMB!;
120150
window.env.KYC_URL = KYC!;
151+
152+
try {
153+
sessionStorage.setItem(
154+
SERVICE_URLS_CACHE_KEY,
155+
JSON.stringify({
156+
urls: {
157+
GRIDPROXY_URL: window.env.GRIDPROXY_URL,
158+
STATS_URL: window.env.STATS_URL,
159+
GRAPHQL_URL: window.env.GRAPHQL_URL,
160+
SUBSTRATE_URL: window.env.SUBSTRATE_URL,
161+
ACTIVATION_SERVICE_URL: window.env.ACTIVATION_SERVICE_URL,
162+
RELAY_DOMAIN: window.env.RELAY_DOMAIN,
163+
KYC_URL: window.env.KYC_URL,
164+
},
165+
timestamp: Date.now(),
166+
}),
167+
);
168+
} catch (error) {
169+
console.warn("[setGlobalEnv] Failed to write service URLs cache", error);
170+
}
171+
121172
return true;
122173
}

packages/playground/src/utils/grid.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { markRaw } from "vue";
44

55
import type { SSHKeyData } from "@/types";
66

7+
import { invalidateServiceUrlsCache } from "../config";
78
import type { Profile } from "../stores/profile_manager";
89
const network = (process.env.NETWORK as NetworkEnv) || window.env.NETWORK;
910
export async function getGrid(
@@ -29,7 +30,11 @@ export async function getGrid(
2930
try {
3031
await grid.connect();
3132
} catch (e) {
32-
if (!(e instanceof InsufficientBalanceError)) throw e;
33+
if (!(e instanceof InsufficientBalanceError)) {
34+
// Invalidate cache if connect fails (except for insufficient balance)
35+
invalidateServiceUrlsCache();
36+
throw e;
37+
}
3338
}
3439
return grid;
3540
}

0 commit comments

Comments
 (0)