@@ -25,6 +25,7 @@ import { realpath } from "fs/promises";
25
25
import { promisify } from "util" ;
26
26
import which from "which" ;
27
27
const exec = promisify ( child_process_exec ) ;
28
+ import { reuseInFlight } from "@cocalc/util/reuse-in-flight" ;
28
29
29
30
// we prefix the environment PATH by default bin paths pointing into it in order to pick up locally installed binaries.
30
31
// 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> {
320
321
// "main": everything that's needed throughout the project
321
322
// "x11": additional checks which are queried when an X11 editor opens up
322
323
// 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
+ ) ;
348
354
349
355
// testing: uncomment, and run $ ts-node configuration.ts
350
356
// (async () => {
0 commit comments