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