6
6
7
7
import { Event } from 'vscode' ;
8
8
import '../../../../common/extensions' ;
9
- import { createDeferred , sleep } from '../../../../common/utils/async' ;
9
+ import { createDeferred } from '../../../../common/utils/async' ;
10
10
import { logWarning } from '../../../../logging' ;
11
11
import { IEnvsCache } from '../../envsCache' ;
12
12
import { PythonEnvInfo } from '../../info' ;
@@ -20,37 +20,12 @@ import { getEnvs, getQueryFilter } from '../../locatorUtils';
20
20
import { PythonEnvsChangedEvent , PythonEnvsWatcher } from '../../watcher' ;
21
21
import { pickBestEnv } from './reducingLocator' ;
22
22
23
- type CachingLocatorOptions = {
24
- refreshMinutes : number ,
25
- refreshRetryMinutes : number ,
26
- } ;
27
-
28
- // Set defaults and otherwise adjust values.
29
- function normalizeCachingLocatorOptions (
30
- opts : Partial < CachingLocatorOptions > ,
31
- defaults : CachingLocatorOptions = {
32
- refreshMinutes : 24 * 60 , // 1 day
33
- refreshRetryMinutes : 10 ,
34
- } ,
35
- ) : CachingLocatorOptions {
36
- const normalized = { ...opts } ;
37
- if ( normalized . refreshMinutes === undefined ) {
38
- normalized . refreshMinutes = defaults . refreshMinutes ;
39
- }
40
- if ( normalized . refreshRetryMinutes === undefined ) {
41
- normalized . refreshRetryMinutes = defaults . refreshRetryMinutes ;
42
- }
43
- return normalized as CachingLocatorOptions ;
44
- }
45
-
46
23
/**
47
24
* A locator that stores the known environments in the given cache.
48
25
*/
49
26
export class CachingLocator implements ILocator {
50
27
public readonly onChanged : Event < PythonEnvsChangedEvent > ;
51
28
52
- private readonly opts : CachingLocatorOptions ;
53
-
54
29
private readonly watcher = new PythonEnvsWatcher ( ) ;
55
30
56
31
private readonly initializing = createDeferred < void > ( ) ;
@@ -62,22 +37,11 @@ export class CachingLocator implements ILocator {
62
37
constructor (
63
38
private readonly cache : IEnvsCache ,
64
39
private readonly locator : ILocator ,
65
- opts : {
66
- refreshMinutes ?: number ,
67
- refreshRetryMinutes ?: number ,
68
- } = { } ,
69
40
) {
70
41
this . onChanged = this . watcher . onChanged ;
71
- this . opts = normalizeCachingLocatorOptions ( opts ) ;
72
42
// eslint-disable-next-line @typescript-eslint/no-use-before-define
73
43
this . looper = new BackgroundLooper ( {
74
44
runDefault : ( ) => this . refresh ( ) ,
75
- retry : {
76
- intervalms : this . opts . refreshRetryMinutes * 60 * 1000 ,
77
- } ,
78
- periodic : {
79
- intervalms : this . opts . refreshMinutes * 60 * 1000 ,
80
- } ,
81
45
} ) ;
82
46
}
83
47
@@ -265,63 +229,6 @@ type RequestID = number;
265
229
type RunFunc = ( ) => Promise < void > ;
266
230
type NotifyFunc = ( ) => void ;
267
231
268
- type RetryOptions = {
269
- maxRetries : number ;
270
- intervalms : number ;
271
- } ;
272
-
273
- // Set defaults and otherwise adjust values.
274
- function normalizeRetryOptions (
275
- opts : Partial < RetryOptions > | undefined ,
276
- defaults : RetryOptions = {
277
- maxRetries : 3 ,
278
- intervalms : 100 ,
279
- } ,
280
- ) : RetryOptions | undefined {
281
- if ( opts === undefined ) {
282
- return undefined ;
283
- }
284
- const normalized = { ...opts } ;
285
- if ( normalized . maxRetries === undefined ) {
286
- normalized . maxRetries = defaults . maxRetries ;
287
- } else if ( normalized . maxRetries < 0 ) {
288
- // This is effectively infinity.
289
- normalized . maxRetries = Number . MAX_SAFE_INTEGER ;
290
- }
291
- if ( normalized . intervalms === undefined ) {
292
- normalized . intervalms = defaults . intervalms ;
293
- }
294
- return normalized as RetryOptions ;
295
- }
296
-
297
- type PeriodicOptions = {
298
- intervalms : number ;
299
- initialTimestamp : number ;
300
- } ;
301
-
302
- function normalizePeriodicOptions (
303
- opts : Partial < PeriodicOptions > | undefined ,
304
- defaults : PeriodicOptions = {
305
- intervalms : - 1 ,
306
- initialTimestamp : - 1 ,
307
- } ,
308
- ) : PeriodicOptions | undefined {
309
- if ( opts === undefined ) {
310
- return undefined ;
311
- }
312
- const normalized = { ...opts } ;
313
- if ( normalized . intervalms === undefined ) {
314
- // "never run"
315
- normalized . intervalms = defaults . intervalms ;
316
- }
317
- if ( normalized . initialTimestamp === undefined && normalized . intervalms > - 1 ) {
318
- normalized . initialTimestamp = Date . now ( ) + normalized . intervalms ;
319
- } else {
320
- normalized . initialTimestamp = defaults . initialTimestamp ;
321
- }
322
- return normalized as PeriodicOptions ;
323
- }
324
-
325
232
/**
326
233
* This helps avoid running duplicate expensive operations.
327
234
*
@@ -331,8 +238,6 @@ function normalizePeriodicOptions(
331
238
class BackgroundLooper {
332
239
private readonly opts : {
333
240
runDefault : RunFunc ;
334
- retry ?: RetryOptions ;
335
- periodic ?: PeriodicOptions ;
336
241
} ;
337
242
338
243
private started = false ;
@@ -354,25 +259,16 @@ class BackgroundLooper {
354
259
355
260
private lastID : number | undefined ;
356
261
357
- private nextPeriod = - 1 ;
358
-
359
262
constructor (
360
263
opts : {
361
264
runDefault ?: RunFunc ;
362
- retry ?: Partial < RetryOptions > ;
363
- periodic ?: Partial < PeriodicOptions > ;
364
265
} = { } ,
365
266
) {
366
267
this . opts = {
367
268
runDefault : opts . runDefault !== undefined
368
269
? opts . runDefault
369
270
: async ( ) => { throw Error ( 'no default operation provided' ) ; } ,
370
- retry : normalizeRetryOptions ( opts . retry ) ,
371
- periodic : normalizePeriodicOptions ( opts . periodic ) ,
372
271
} ;
373
- if ( this . opts . periodic !== undefined ) {
374
- this . nextPeriod = this . opts . periodic . initialTimestamp ;
375
- }
376
272
}
377
273
378
274
/**
@@ -506,12 +402,6 @@ class BackgroundLooper {
506
402
this . done . promise . then ( ( ) => 0 ) ,
507
403
this . waitUntilReady . promise . then ( ( ) => 1 ) ,
508
404
] ;
509
- if ( this . opts . periodic !== undefined && this . nextPeriod > - 1 ) {
510
- const msLeft = Math . max ( 0 , this . nextPeriod - Date . now ( ) ) ;
511
- promises . push (
512
- sleep ( msLeft ) . then ( ( ) => 2 ) ,
513
- ) ;
514
- }
515
405
return Promise . race ( promises ) ;
516
406
} ;
517
407
@@ -521,12 +411,6 @@ class BackgroundLooper {
521
411
this . waitUntilReady = createDeferred < void > ( ) ;
522
412
// eslint-disable-next-line no-await-in-loop
523
413
await this . flush ( ) ;
524
- } else if ( winner === 2 ) {
525
- // We reset the period before queueing to avoid any races.
526
- this . nextPeriod = Date . now ( ) + this . opts . periodic ! . intervalms ;
527
- // Rather than running the request directly, we add
528
- // it to the queue. This avoids races.
529
- this . addRequest ( this . opts . runDefault ) ;
530
414
} else {
531
415
// This should not be reachable.
532
416
throw Error ( `unsupported winner ${ winner } ` ) ;
@@ -558,7 +442,7 @@ class BackgroundLooper {
558
442
const [ run , , notify ] = this . requests [ reqID ] ;
559
443
560
444
// eslint-disable-next-line no-await-in-loop
561
- await this . runRequest ( run ) ;
445
+ await run ( ) ;
562
446
563
447
// We leave the request until right before `notify()`
564
448
// for the sake of any calls to `getLastRequest()`.
@@ -568,36 +452,6 @@ class BackgroundLooper {
568
452
this . running = undefined ;
569
453
}
570
454
571
- /**
572
- * Run a single request.
573
- */
574
- private async runRequest ( run : RunFunc ) : Promise < void > {
575
- if ( this . opts . retry === undefined ) {
576
- // eslint-disable-next-line no-await-in-loop
577
- await run ( ) ;
578
- return ;
579
- }
580
- let retriesLeft = this . opts . retry . maxRetries ;
581
- const retryIntervalms = this . opts . retry . intervalms ;
582
- let retrying = false ;
583
- do {
584
- try {
585
- // eslint-disable-next-line no-await-in-loop
586
- await run ( ) ;
587
- } catch ( err ) {
588
- if ( retriesLeft < 1 ) {
589
- throw err ; // re-trhow
590
- }
591
- retriesLeft -= 1 ;
592
- logWarning ( `failed while handling request (${ err } )` ) ;
593
- logWarning ( `retrying (${ retriesLeft } attempts left)` ) ;
594
- // eslint-disable-next-line no-await-in-loop
595
- await sleep ( retryIntervalms ) ;
596
- retrying = true ;
597
- }
598
- } while ( ! retrying ) ;
599
- }
600
-
601
455
/**
602
456
* Provide the request ID to use next.
603
457
*/
0 commit comments