Skip to content

Commit 4cc412e

Browse files
Drop the retry/periodic code.
1 parent 6be28bc commit 4cc412e

File tree

1 file changed

+2
-145
lines changed

1 file changed

+2
-145
lines changed

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

Lines changed: 2 additions & 145 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,68 +229,9 @@ 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
class BackgroundLooper {
326233
private readonly opts: {
327234
runDefault: RunFunc;
328-
retry?: RetryOptions;
329-
periodic?: PeriodicOptions;
330235
};
331236

332237
private started = false;
@@ -348,25 +253,16 @@ class BackgroundLooper {
348253

349254
private lastID: number | undefined;
350255

351-
private nextPeriod = -1;
352-
353256
constructor(
354257
opts: {
355258
runDefault?: RunFunc;
356-
retry?: Partial<RetryOptions>;
357-
periodic?: Partial<PeriodicOptions>;
358259
} = {},
359260
) {
360261
this.opts = {
361262
runDefault: opts.runDefault !== undefined
362263
? opts.runDefault
363264
: async () => { throw Error('no default operation provided'); },
364-
retry: normalizeRetryOptions(opts.retry),
365-
periodic: normalizePeriodicOptions(opts.periodic),
366265
};
367-
if (this.opts.periodic !== undefined) {
368-
this.nextPeriod = this.opts.periodic.initialTimestamp;
369-
}
370266
}
371267

372268
public start(): void {
@@ -460,12 +356,6 @@ class BackgroundLooper {
460356
this.done.promise.then(() => 0),
461357
this.waitUntilReady.promise.then(() => 1),
462358
];
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-
}
469359
return Promise.race(promises);
470360
};
471361

@@ -475,12 +365,6 @@ class BackgroundLooper {
475365
this.waitUntilReady = createDeferred<void>();
476366
// eslint-disable-next-line no-await-in-loop
477367
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);
484368
} else {
485369
// This should not be reachable.
486370
throw Error(`unsupported winner ${winner}`);
@@ -505,7 +389,7 @@ class BackgroundLooper {
505389
const [run, , notify] = this.requests[reqID];
506390

507391
// eslint-disable-next-line no-await-in-loop
508-
await this.runRequest(run);
392+
await run();
509393

510394
// We leave the request until right before `notify()`
511395
// for the sake of any calls to `getLastRequest()`.
@@ -515,33 +399,6 @@ class BackgroundLooper {
515399
this.running = undefined;
516400
}
517401

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-
545402
private getNextID(): RequestID {
546403
// For now there is no way to queue up a request with
547404
// an ID that did not originate here. So we don't need

0 commit comments

Comments
 (0)