Skip to content

Commit

Permalink
fix(Storage): queue up opening storages to prevent issues in concurre…
Browse files Browse the repository at this point in the history
…nt calls (#1865)
  • Loading branch information
vladfrangu committed Apr 7, 2023
1 parent 3aed5ca commit 044c740
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"@crawlee/memory-storage": "^3.3.0",
"@crawlee/types": "^3.3.0",
"@crawlee/utils": "^3.3.0",
"@sapphire/async-queue": "^1.5.0",
"@types/tough-cookie": "^4.0.2",
"@vladfrangu/async_event_emitter": "^2.0.0",
"csv-stringify": "^6.2.0",
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/storages/storage_manager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Dictionary, StorageClient } from '@crawlee/types';
import { AsyncQueue } from '@sapphire/async-queue';
import { Configuration } from '../configuration';
import type { Constructor } from '../typedefs';

Expand All @@ -22,6 +23,7 @@ export class StorageManager<T extends IStorage = IStorage> {
private readonly name: 'Dataset' | 'KeyValueStore' | 'RequestQueue';
private readonly StorageConstructor: Constructor<T> & { name: string };
private readonly cache = new Map<string, T>();
private readonly storageOpenQueue = new AsyncQueue();

constructor(
StorageConstructor: Constructor<T>,
Expand Down Expand Up @@ -65,6 +67,8 @@ export class StorageManager<T extends IStorage = IStorage> {
}

async openStorage(idOrName?: string | null, client?: StorageClient): Promise<T> {
await this.storageOpenQueue.wait();

if (!idOrName) {
const defaultIdConfigKey = DEFAULT_ID_CONFIG_KEYS[this.name];
idOrName = this.config.get(defaultIdConfigKey) as string;
Expand All @@ -84,6 +88,8 @@ export class StorageManager<T extends IStorage = IStorage> {
this._addStorageToCache(storage);
}

this.storageOpenQueue.shift();

return storage;
}

Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,7 @@ __metadata:
"@crawlee/memory-storage": ^3.3.0
"@crawlee/types": ^3.3.0
"@crawlee/utils": ^3.3.0
"@sapphire/async-queue": ^1.5.0
"@types/tough-cookie": ^4.0.2
"@vladfrangu/async_event_emitter": ^2.0.0
csv-stringify: ^6.2.0
Expand Down

0 comments on commit 044c740

Please sign in to comment.