Skip to content

Commit ae7c510

Browse files
committed
fix #7806 -- backend side
1 parent 8f1deb9 commit ae7c510

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

src/packages/project/configuration.ts

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { realpath } from "fs/promises";
2525
import { promisify } from "util";
2626
import which from "which";
2727
const exec = promisify(child_process_exec);
28+
import { reuseInFlight } from "@cocalc/util/reuse-in-flight";
2829

2930
// we prefix the environment PATH by default bin paths pointing into it in order to pick up locally installed binaries.
3031
// they can't be set as defaults for projects since this could break it from starting up
@@ -320,31 +321,36 @@ async function capabilities(): Promise<MainCapabilities> {
320321
// "main": everything that's needed throughout the project
321322
// "x11": additional checks which are queried when an X11 editor opens up
322323
// TODO similarly, query available "shells" to use for the corresponding code editor button
323-
export async function get_configuration(
324-
aspect: ConfigurationAspect,
325-
no_cache = false,
326-
): Promise<Configuration> {
327-
const cached = conf[aspect];
328-
if (cached != null && !no_cache) return cached;
329-
const t0 = new Date().getTime();
330-
const new_conf: any = (async function () {
331-
switch (aspect) {
332-
case "main":
333-
return {
334-
timestamp: new Date(),
335-
capabilities: await capabilities(),
336-
};
337-
case "x11":
338-
return {
339-
timestamp: new Date(),
340-
capabilities: await x11_apps(),
341-
};
342-
}
343-
})();
344-
new_conf.timing_s = (new Date().getTime() - t0) / 1000;
345-
conf[aspect] = await new_conf;
346-
return new_conf;
347-
}
324+
325+
// This is expensive, so put in a reuseInFlight to make it cheap in case a frontend
326+
// annoyingly calls this dozens of times at once -- https://github.com/sagemathinc/cocalc/issues/7806
327+
export const get_configuration = reuseInFlight(
328+
async (
329+
aspect: ConfigurationAspect,
330+
no_cache = false,
331+
): Promise<Configuration> => {
332+
const cached = conf[aspect];
333+
if (cached != null && !no_cache) return cached;
334+
const t0 = new Date().getTime();
335+
const new_conf: any = (async function () {
336+
switch (aspect) {
337+
case "main":
338+
return {
339+
timestamp: new Date(),
340+
capabilities: await capabilities(),
341+
};
342+
case "x11":
343+
return {
344+
timestamp: new Date(),
345+
capabilities: await x11_apps(),
346+
};
347+
}
348+
})();
349+
new_conf.timing_s = (new Date().getTime() - t0) / 1000;
350+
conf[aspect] = await new_conf;
351+
return new_conf;
352+
},
353+
);
348354

349355
// testing: uncomment, and run $ ts-node configuration.ts
350356
// (async () => {

0 commit comments

Comments
 (0)