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,68 +229,9 @@ 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
class BackgroundLooper {
326
233
private readonly opts : {
327
234
runDefault : RunFunc ;
328
- retry ?: RetryOptions ;
329
- periodic ?: PeriodicOptions ;
330
235
} ;
331
236
332
237
private started = false ;
@@ -348,25 +253,16 @@ class BackgroundLooper {
348
253
349
254
private lastID : number | undefined ;
350
255
351
- private nextPeriod = - 1 ;
352
-
353
256
constructor (
354
257
opts : {
355
258
runDefault ?: RunFunc ;
356
- retry ?: Partial < RetryOptions > ;
357
- periodic ?: Partial < PeriodicOptions > ;
358
259
} = { } ,
359
260
) {
360
261
this . opts = {
361
262
runDefault : opts . runDefault !== undefined
362
263
? opts . runDefault
363
264
: async ( ) => { throw Error ( 'no default operation provided' ) ; } ,
364
- retry : normalizeRetryOptions ( opts . retry ) ,
365
- periodic : normalizePeriodicOptions ( opts . periodic ) ,
366
265
} ;
367
- if ( this . opts . periodic !== undefined ) {
368
- this . nextPeriod = this . opts . periodic . initialTimestamp ;
369
- }
370
266
}
371
267
372
268
public start ( ) : void {
@@ -460,12 +356,6 @@ class BackgroundLooper {
460
356
this . done . promise . then ( ( ) => 0 ) ,
461
357
this . waitUntilReady . promise . then ( ( ) => 1 ) ,
462
358
] ;
463
- if ( this . opts . periodic !== undefined && this . nextPeriod > - 1 ) {
464
- const msLeft = Math . max ( 0 , this . nextPeriod - Date . now ( ) ) ;
465
- promises . push (
466
- sleep ( msLeft ) . then ( ( ) => 2 ) ,
467
- ) ;
468
- }
469
359
return Promise . race ( promises ) ;
470
360
} ;
471
361
@@ -475,12 +365,6 @@ class BackgroundLooper {
475
365
this . waitUntilReady = createDeferred < void > ( ) ;
476
366
// eslint-disable-next-line no-await-in-loop
477
367
await this . flush ( ) ;
478
- } else if ( winner === 2 ) {
479
- // We reset the period before queueing to avoid any races.
480
- this . nextPeriod = Date . now ( ) + this . opts . periodic ! . intervalms ;
481
- // Rather than running the request directly, we add
482
- // it to the queue. This avoids races.
483
- this . addRequest ( this . opts . runDefault ) ;
484
368
} else {
485
369
// This should not be reachable.
486
370
throw Error ( `unsupported winner ${ winner } ` ) ;
@@ -505,7 +389,7 @@ class BackgroundLooper {
505
389
const [ run , , notify ] = this . requests [ reqID ] ;
506
390
507
391
// eslint-disable-next-line no-await-in-loop
508
- await this . runRequest ( run ) ;
392
+ await run ( ) ;
509
393
510
394
// We leave the request until right before `notify()`
511
395
// for the sake of any calls to `getLastRequest()`.
@@ -515,33 +399,6 @@ class BackgroundLooper {
515
399
this . running = undefined ;
516
400
}
517
401
518
- private async runRequest ( run : RunFunc ) : Promise < void > {
519
- if ( this . opts . retry === undefined ) {
520
- // eslint-disable-next-line no-await-in-loop
521
- await run ( ) ;
522
- return ;
523
- }
524
- let retriesLeft = this . opts . retry . maxRetries ;
525
- const retryIntervalms = this . opts . retry . intervalms ;
526
- let retrying = false ;
527
- do {
528
- try {
529
- // eslint-disable-next-line no-await-in-loop
530
- await run ( ) ;
531
- } catch ( err ) {
532
- if ( retriesLeft < 1 ) {
533
- throw err ; // re-trhow
534
- }
535
- retriesLeft -= 1 ;
536
- logWarning ( `failed while handling request (${ err } )` ) ;
537
- logWarning ( `retrying (${ retriesLeft } attempts left)` ) ;
538
- // eslint-disable-next-line no-await-in-loop
539
- await sleep ( retryIntervalms ) ;
540
- retrying = true ;
541
- }
542
- } while ( ! retrying ) ;
543
- }
544
-
545
402
private getNextID ( ) : RequestID {
546
403
// For now there is no way to queue up a request with
547
404
// an ID that did not originate here. So we don't need
0 commit comments