Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: optimize adding large amount of requests via crawler.addRequests() #2456

Merged
merged 3 commits into from
May 13, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions packages/core/src/storages/request_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,10 @@ export abstract class RequestProvider implements IStorage {
ow(requestsLike, ow.array);
ow(options, ow.object.exactShape({
forefront: ow.optional.boolean,
cache: ow.optional.boolean,
}));

const { forefront = false } = options;
const { forefront = false, cache = true } = options;

const uniqueKeyToCacheKey = new Map<string, string>();
const getCachedRequestId = (uniqueKey: string) => {
Expand Down Expand Up @@ -253,7 +254,10 @@ export abstract class RequestProvider implements IStorage {
const cacheKey = getCachedRequestId(newRequest.uniqueKey);

const { requestId, wasAlreadyPresent } = newRequest;
this._cacheRequest(cacheKey, newRequest);

if (cache) {
this._cacheRequest(cacheKey, newRequest);
}

if (!wasAlreadyPresent && !this.inProgress.has(requestId) && !this.recentlyHandledRequestsCache.get(requestId)) {
this.assumedTotalCount++;
Expand Down Expand Up @@ -294,26 +298,27 @@ export abstract class RequestProvider implements IStorage {
batchSize = 1000,
waitBetweenBatchesMillis = 1000,
} = options;
const builtRequests: Request[] = [];
const builtRequests: Source[] = [];

for (const opts of requests) {
if (opts && typeof opts === 'object' && 'requestsFromUrl' in opts) {
await this.addRequest(opts, { forefront: options.forefront });
} else {
builtRequests.push(new Request(typeof opts === 'string' ? { url: opts } : opts as RequestOptions));
builtRequests.push(typeof opts === 'string' ? { url: opts } : opts as RequestOptions);
B4nan marked this conversation as resolved.
Show resolved Hide resolved
}
}

const attemptToAddToQueueAndAddAnyUnprocessed = async (providedRequests: Request[]) => {
const attemptToAddToQueueAndAddAnyUnprocessed = async (providedRequests: Source[], cache = true) => {
const resultsToReturn: ProcessedRequest[] = [];
const apiResult = await this.addRequests(providedRequests, { forefront: options.forefront });
const apiResult = await this.addRequests(providedRequests, { forefront: options.forefront, cache });
resultsToReturn.push(...apiResult.processedRequests);

if (apiResult.unprocessedRequests.length) {
await sleep(waitBetweenBatchesMillis);

resultsToReturn.push(...await attemptToAddToQueueAndAddAnyUnprocessed(
providedRequests.filter((r) => !apiResult.processedRequests.some((pr) => pr.uniqueKey === r.uniqueKey)),
false,
));
}

Expand All @@ -339,7 +344,7 @@ export abstract class RequestProvider implements IStorage {
const finalAddedRequests: ProcessedRequest[] = [];

for (const requestChunk of chunks) {
finalAddedRequests.push(...await attemptToAddToQueueAndAddAnyUnprocessed(requestChunk));
finalAddedRequests.push(...await attemptToAddToQueueAndAddAnyUnprocessed(requestChunk, false));

await sleep(waitBetweenBatchesMillis);
}
Expand Down Expand Up @@ -707,6 +712,12 @@ export interface RequestQueueOperationOptions {
* @default false
*/
forefront?: boolean;
/**
* Should the requests be added to the local LRU cache?
* @default false
* @internal
*/
cache?: boolean;
}

/**
Expand Down
Loading