Skip to content

Commit 59932cc

Browse files
committed
feat: support for assigning new tasks in batches , to avoid network overhead
If workers asks for batched tasks , will answer to that with that batch for new work requests
1 parent 9544b2c commit 59932cc

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,20 @@ class Master {
103103
this.peer.register('requestWork', (pk, args, cb) => {
104104
switch (true) {
105105
case this.jobs.length > 1: {
106+
if (args.getBatch && this.jobs.length > args.batchSize) {
107+
args.batchTasks = this.jobs.splice(0, args.batchSize); // splice mutates original array which slices it
108+
this.log.debug(
109+
`task queue reduced:${this.jobs.length} - ${args.batchSize}`
110+
);
111+
break;
112+
}
106113
args.task = this.jobs.shift();
107114
this.log.debug(`task queue reduced:${this.jobs.length}`);
108115
break;
109116
}
110117
case this.jobs.length === 1: {
111-
// shift make array undefined on last element
112-
[args.task] = this.jobs; // , correct this with this check
118+
// shift leaves array undefined on last element
119+
[args.task] = this.jobs; // this case is to avoid that
113120
this.jobs = [];
114121
this.log.debug(`task queue finished:${this.jobs.length}`);
115122
break;

0 commit comments

Comments
 (0)