-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathchild.js
More file actions
144 lines (120 loc) · 3.14 KB
/
Copy pathchild.js
File metadata and controls
144 lines (120 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const errorUtils = require('./errorUtils');
class Child {
constructor() {
if (!process.send) {
throw new Error('Only create Child instances in a worker!');
}
this.module = undefined;
this.childId = undefined;
this.callQueue = [];
this.responseQueue = new Map();
this.responseId = 0;
this.maxConcurrentCalls = 10;
}
messageListener(data) {
if (data === 'die') {
return this.end();
}
let type = data.type;
if (type === 'response') {
return this.handleResponse(data);
} else if (type === 'request') {
return this.handleRequest(data);
}
}
async send(data) {
process.send(data, err => {
if (err && err instanceof Error) {
if (err.code === 'ERR_IPC_CHANNEL_CLOSED') {
// IPC connection closed
// no need to keep the worker running if it can't send or receive data
return this.end();
}
}
});
}
childInit(module, childId) {
this.module = require(module);
this.childId = childId;
}
async handleRequest(data) {
let idx = data.idx;
let child = data.child;
let method = data.method;
let args = data.args;
let result = {idx, child, type: 'response'};
try {
result.contentType = 'data';
if (method === 'childInit') {
result.content = this.childInit(...args, child);
} else {
result.content = await this.module[method](...args);
}
} catch (e) {
result.contentType = 'error';
result.content = errorUtils.errorToJson(e);
}
this.send(result);
}
async handleResponse(data) {
let idx = data.idx;
let contentType = data.contentType;
let content = data.content;
let call = this.responseQueue.get(idx);
if (contentType === 'error') {
call.reject(errorUtils.jsonToError(content));
} else {
call.resolve(content);
}
this.responseQueue.delete(idx);
// Process the next call
this.processQueue();
}
// Keep in mind to make sure responses to these calls are JSON.Stringify safe
async addCall(request, awaitResponse = true) {
let call = request;
call.type = 'request';
call.child = this.childId;
call.awaitResponse = awaitResponse;
let promise;
if (awaitResponse) {
promise = new Promise((resolve, reject) => {
call.resolve = resolve;
call.reject = reject;
});
}
this.callQueue.push(call);
this.processQueue();
return promise;
}
async sendRequest(call) {
let idx;
if (call.awaitResponse) {
idx = this.responseId++;
this.responseQueue.set(idx, call);
}
this.send({
idx: idx,
child: call.child,
type: call.type,
location: call.location,
method: call.method,
args: call.args,
awaitResponse: call.awaitResponse
});
}
async processQueue() {
if (!this.callQueue.length) {
return;
}
if (this.responseQueue.size < this.maxConcurrentCalls) {
this.sendRequest(this.callQueue.shift());
}
}
end() {
process.exit();
}
}
let child = new Child();
process.on('message', child.messageListener.bind(child));
module.exports = child;