Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/test/base/throttlingCodeOrDocsSearchClient.ts
13388 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
import type { CancellationToken } from 'vscode';
6
import { ICodeOrDocsSearchItem, ICodeOrDocsSearchMultiRepoScopingQuery, ICodeOrDocsSearchOptions, ICodeOrDocsSearchResult, ICodeOrDocsSearchSingleRepoScopingQuery, IDocsSearchClient } from '../../src/platform/remoteSearch/common/codeOrDocsSearchClient';
7
import { ThrottledWorker } from '../../src/util/vs/base/common/async';
8
import { SyncDescriptor } from '../../src/util/vs/platform/instantiation/common/descriptors';
9
import { IInstantiationService } from '../../src/util/vs/platform/instantiation/common/instantiation';
10
11
12
export class ThrottlingCodeOrDocsSearchClient implements IDocsSearchClient {
13
14
declare readonly _serviceBrand: undefined;
15
private readonly _throttler: ThrottledWorker<() => Promise<void>>;
16
private readonly searchClient: IDocsSearchClient;
17
18
constructor(
19
descriptor: SyncDescriptor<IDocsSearchClient>,
20
@IInstantiationService instantiationService: IInstantiationService,
21
) {
22
this.searchClient = instantiationService.createInstance(descriptor);
23
this._throttler = new ThrottledWorker({
24
maxBufferedWork: undefined, // We want to hold as many requests as possible
25
maxWorkChunkSize: 1,
26
waitThrottleDelayBetweenWorkUnits: true,
27
throttleDelay: 1000
28
}, async (tasks) => {
29
for (const task of tasks) {
30
await task();
31
}
32
});
33
}
34
35
search(query: string, scopingQuery: ICodeOrDocsSearchSingleRepoScopingQuery, options?: ICodeOrDocsSearchOptions, cancellationToken?: CancellationToken | undefined): Promise<ICodeOrDocsSearchItem[]>;
36
search(query: string, scopingQuery: ICodeOrDocsSearchMultiRepoScopingQuery, options?: ICodeOrDocsSearchOptions, cancellationToken?: CancellationToken | undefined): Promise<ICodeOrDocsSearchResult>;
37
search(query: string,
38
scopingQuery: ICodeOrDocsSearchSingleRepoScopingQuery | ICodeOrDocsSearchMultiRepoScopingQuery,
39
options: ICodeOrDocsSearchOptions = {},
40
cancellationToken?: CancellationToken
41
): Promise<ICodeOrDocsSearchItem[] | ICodeOrDocsSearchResult> {
42
return new Promise((resolve, reject) => {
43
this._throttler.work([async () => {
44
try {
45
if (Array.isArray(scopingQuery.repo)) {
46
const result = await this.searchClient.search(
47
query,
48
scopingQuery as ICodeOrDocsSearchMultiRepoScopingQuery,
49
options,
50
cancellationToken
51
);
52
resolve(result);
53
} else {
54
const result = await this.searchClient.search(
55
query,
56
scopingQuery as ICodeOrDocsSearchSingleRepoScopingQuery,
57
options,
58
cancellationToken
59
);
60
resolve(result);
61
}
62
} catch (error) {
63
reject(error);
64
}
65
}]);
66
});
67
}
68
}
69
70