MCPcopy Create free account
hub / github.com/MultithreadedJSBook/code-samples / getWorker

Method getWorker

ch6-thread-pool/rpc-worker.js:45–65  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

43 }
44// THIS LINE SHOULD NOT APPEAR IN PRINT
45 getWorker() {
46 let id;
47 if (this.strategy === 'random') {
48 id = Math.floor(Math.random() * this.size);
49 } else if (this.strategy === 'roundrobin') {
50 this.rr_index++;
51 if (this.rr_index >= this.size) this.rr_index = 0;
52 id = this.rr_index;
53 } else if (this.strategy === 'leastbusy') {
54 let min = Infinity;
55 for (let i = 0; i < this.size; i++) {
56 let worker = this.workers[i];
57 if (worker.in_flight_commands.size < min) {
58 min = worker.in_flight_commands.size;
59 id = i;
60 }
61 }
62 }
63 console.log('Selected Worker:', id);
64 return this.workers[id];
65 }
66};

Callers 1

execMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected