-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into feature/2295
- Loading branch information
Showing
20 changed files
with
186 additions
and
75 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -109,6 +109,7 @@ | |
"events": "^3.2.0", | ||
"express": "^4.17.1", | ||
"fetch-retry": "^6.0.0", | ||
"fastq": "^1.17.1", | ||
"get-port": "^5.1.1", | ||
"go-ipfs": "npm:[email protected]", | ||
"http-server": "^0.12.3", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 50 additions & 21 deletions
71
packages/backend/src/nest/libp2p/process-in-chunks.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,85 @@ | ||
import { EventEmitter } from 'events' | ||
import fastq from 'fastq' | ||
import type { queue, done } from 'fastq' | ||
|
||
import Logger from '../common/logger' | ||
|
||
const DEFAULT_CHUNK_SIZE = 10 | ||
export const DEFAULT_NUM_TRIES = 2 | ||
|
||
type ProcessTask<T> = { | ||
data: T | ||
tries: number | ||
} | ||
|
||
export class ProcessInChunksService<T> extends EventEmitter { | ||
private isActive: boolean | ||
private data: T[] | ||
private data: Set<T> = new Set() | ||
private chunkSize: number | ||
private taskQueue: queue<ProcessTask<T>> | ||
private processItem: (arg: T) => Promise<any> | ||
private readonly logger = Logger(ProcessInChunksService.name) | ||
constructor() { | ||
super() | ||
} | ||
|
||
public init(data: T[] = [], processItem: (arg: T) => Promise<any>, chunkSize: number = DEFAULT_CHUNK_SIZE) { | ||
this.data = data | ||
public init(data: T[], processItem: (arg: T) => Promise<any>, chunkSize: number = DEFAULT_CHUNK_SIZE) { | ||
this.logger(`Initializing process-in-chunks.service with peers ${JSON.stringify(data, null, 2)}`) | ||
this.processItem = processItem | ||
this.chunkSize = chunkSize | ||
this.taskQueue = fastq(this, this.processOneItem, this.chunkSize) | ||
this.updateData(data) | ||
this.addToTaskQueue() | ||
} | ||
|
||
updateData(items: T[]) { | ||
public updateData(items: T[]) { | ||
this.logger(`Updating data with ${items.length} items`) | ||
this.data = [...new Set(this.data.concat(items))] | ||
this.taskQueue.pause() | ||
items.forEach(item => this.data.add(item)) | ||
this.addToTaskQueue() | ||
} | ||
|
||
public async processOneItem() { | ||
const toProcess = this.data.shift() | ||
if (toProcess) { | ||
try { | ||
await this.processItem(toProcess) | ||
} catch (e) { | ||
this.logger(`Processing ${toProcess} failed, message:`, e.message) | ||
} finally { | ||
process.nextTick(async () => { | ||
await this.processOneItem() | ||
}) | ||
private addToTaskQueue() { | ||
this.logger(`Adding ${this.data.size} items to the task queue`) | ||
for (const item of this.data) { | ||
if (item) { | ||
this.logger(`Adding data ${item} to the task queue`) | ||
this.data.delete(item) | ||
try { | ||
this.taskQueue.push({ data: item, tries: 0 } as ProcessTask<T>) | ||
} catch (e) { | ||
this.logger.error(`Error occurred while adding new task for item ${item} to the queue`, e) | ||
this.data.add(item) | ||
} | ||
} | ||
} | ||
} | ||
|
||
public async process() { | ||
this.logger(`Processing ${this.data.length} items`) | ||
for (let i = 0; i < this.chunkSize; i++) { | ||
// Do not wait for this promise as items should be processed simultineously | ||
void this.processOneItem() | ||
public async processOneItem(task: ProcessTask<T>) { | ||
try { | ||
this.logger(`Processing task with data ${task.data}`) | ||
await this.processItem(task.data) | ||
} catch (e) { | ||
this.logger.error(`Processing task with data ${task.data} failed`, e) | ||
if (task.tries + 1 < DEFAULT_NUM_TRIES) { | ||
this.logger(`Will try to re-attempt task with data ${task.data}`) | ||
this.taskQueue.push({ ...task, tries: task.tries + 1 }) | ||
} | ||
} finally { | ||
this.logger(`Done attempting to process task with data ${task.data}`) | ||
} | ||
} | ||
|
||
public async process() { | ||
this.logger(`Processing ${this.taskQueue.length} items`) | ||
this.taskQueue.resume() | ||
} | ||
|
||
public stop() { | ||
if (this.isActive) { | ||
this.logger('Stopping initial dial') | ||
this.isActive = false | ||
this.taskQueue.pause() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.