Skip to content

Commit abf86c3

Browse files
Drop the retry/periodic code.
1 parent c57dd0d commit abf86c3

File tree

1 file changed

+2
-148
lines changed

1 file changed

+2
-148
lines changed

src/client/pythonEnvironments/base/locators/composite/cachingLocator.ts

Lines changed: 2 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import { Event } from 'vscode';
88
import '../../../../common/extensions';
9-
import { createDeferred, sleep } from '../../../../common/utils/async';
9+
import { createDeferred } from '../../../../common/utils/async';
1010
import { logWarning } from '../../../../logging';
1111
import { IEnvsCache } from '../../envsCache';
1212
import { PythonEnvInfo } from '../../info';
@@ -20,37 +20,12 @@ import { getEnvs, getQueryFilter } from '../../locatorUtils';
2020
import { PythonEnvsChangedEvent, PythonEnvsWatcher } from '../../watcher';
2121
import { pickBestEnv } from './reducingLocator';
2222

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-
4623
/**
4724
* A locator that stores the known environments in the given cache.
4825
*/
4926
export class CachingLocator implements ILocator {
5027
public readonly onChanged: Event<PythonEnvsChangedEvent>;
5128

52-
private readonly opts: CachingLocatorOptions;
53-
5429
private readonly watcher = new PythonEnvsWatcher();
5530

5631
private readonly initializing = createDeferred<void>();
@@ -62,22 +37,11 @@ export class CachingLocator implements ILocator {
6237
constructor(
6338
private readonly cache: IEnvsCache,
6439
private readonly locator: ILocator,
65-
opts: {
66-
refreshMinutes?: number,
67-
refreshRetryMinutes?: number,
68-
} = {},
6940
) {
7041
this.onChanged = this.watcher.onChanged;
71-
this.opts = normalizeCachingLocatorOptions(opts);
7242
// eslint-disable-next-line @typescript-eslint/no-use-before-define
7343
this.looper = new BackgroundLooper({
7444
runDefault: () => this.refresh(),
75-
retry: {
76-
intervalms: this.opts.refreshRetryMinutes * 60 * 1000,
77-
},
78-
periodic: {
79-
intervalms: this.opts.refreshMinutes * 60 * 1000,
80-
},
8145
});
8246
}
8347

@@ -265,63 +229,6 @@ type RequestID = number;
265229
type RunFunc = () => Promise<void>;
266230
type NotifyFunc = () => void;
267231

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-
325232
/**
326233
* This helps avoid running duplicate expensive operations.
327234
*
@@ -331,8 +238,6 @@ function normalizePeriodicOptions(
331238
class BackgroundLooper {
332239
private readonly opts: {
333240
runDefault: RunFunc;
334-
retry?: RetryOptions;
335-
periodic?: PeriodicOptions;
336241
};
337242

338243
private started = false;
@@ -354,25 +259,16 @@ class BackgroundLooper {
354259

355260
private lastID: number | undefined;
356261

357-
private nextPeriod = -1;
358-
359262
constructor(
360263
opts: {
361264
runDefault?: RunFunc;
362-
retry?: Partial<RetryOptions>;
363-
periodic?: Partial<PeriodicOptions>;
364265
} = {},
365266
) {
366267
this.opts = {
367268
runDefault: opts.runDefault !== undefined
368269
? opts.runDefault
369270
: async () => { throw Error('no default operation provided'); },
370-
retry: normalizeRetryOptions(opts.retry),
371-
periodic: normalizePeriodicOptions(opts.periodic),
372271
};
373-
if (this.opts.periodic !== undefined) {
374-
this.nextPeriod = this.opts.periodic.initialTimestamp;
375-
}
376272
}
377273

378274
/**
@@ -506,12 +402,6 @@ class BackgroundLooper {
506402
this.done.promise.then(() => 0),
507403
this.waitUntilReady.promise.then(() => 1),
508404
];
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-
}
515405
return Promise.race(promises);
516406
};
517407

@@ -521,12 +411,6 @@ class BackgroundLooper {
521411
this.waitUntilReady = createDeferred<void>();
522412
// eslint-disable-next-line no-await-in-loop
523413
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);
530414
} else {
531415
// This should not be reachable.
532416
throw Error(`unsupported winner ${winner}`);
@@ -558,7 +442,7 @@ class BackgroundLooper {
558442
const [run, , notify] = this.requests[reqID];
559443

560444
// eslint-disable-next-line no-await-in-loop
561-
await this.runRequest(run);
445+
await run();
562446

563447
// We leave the request until right before `notify()`
564448
// for the sake of any calls to `getLastRequest()`.
@@ -568,36 +452,6 @@ class BackgroundLooper {
568452
this.running = undefined;
569453
}
570454

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-
601455
/**
602456
* Provide the request ID to use next.
603457
*/

0 commit comments

Comments
 (0)