Skip to content

Commit

Permalink
Keep an overall thread limit
Browse files Browse the repository at this point in the history
  • Loading branch information
emmercm committed Oct 26, 2023
1 parent e007e6a commit f423bbb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ export default class Constants {
static readonly DAT_DEFAULT_THREADS = 3;

/**
* Max number of archive entries to process (possibly extract & MD5/SHA1 checksum) at once.
* A reasonable max number of files to write at once.
*/
static readonly ARCHIVE_ENTRY_SCANNER_THREADS_PER_ARCHIVE = 5;
static readonly FILE_READER_DEFAULT_THREADS = 10;

/**
* A reasonable max number of files to write at once.
* Max number of archive entries to process (possibly extract & MD5/SHA1 checksum) at once.
*/
static readonly FILE_READER_DEFAULT_THREADS = 10;
static readonly ARCHIVE_ENTRY_SCANNER_THREADS_PER_ARCHIVE = 5;

/**
* A reasonable max number of ROM release candidates to write at once. This will be the limiting
Expand Down
22 changes: 17 additions & 5 deletions src/driveSemaphore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ export default class DriveSemaphore {

private readonly keySemaphoresMutex = new Mutex();

private readonly defaultThreads: number;
private readonly threads: number;

constructor(defaultThreads = 1) {
this.defaultThreads = defaultThreads;
private readonly threadsSemaphore: Semaphore;

constructor(threads = 1) {
this.threads = threads;
this.threadsSemaphore = new Semaphore(threads);
}

/**
Expand Down Expand Up @@ -74,15 +77,24 @@ export default class DriveSemaphore {

const keySemaphore = await this.keySemaphoresMutex.runExclusive(async () => {
if (!this.keySemaphores.has(filePathDisk)) {
let threads = this.defaultThreads;
let { threads } = this;
if (await FsPoly.isSamba(filePathDisk)) {
// Forcefully limit the number of files to be processed concurrently from a single
// Samba network share
threads = 1;
}
this.keySemaphores.set(filePathDisk, new Semaphore(threads));
}
return this.keySemaphores.get(filePathDisk) as Semaphore;
});

return keySemaphore.runExclusive(async () => runnable(file));
// First, limit the number of threads per drive, which will better balance the processing of
// files on different drives vs. processing files sequentially
return keySemaphore.runExclusive(
// Second, limit the overall number of threads
async () => this.threadsSemaphore.runExclusive(
async () => runnable(file),
),
);
}
}

0 comments on commit f423bbb

Please sign in to comment.