Skip to content

Commit 0f133eb

Browse files
cjihrigtargos
authored andcommitted
cluster: use Map to track handles in cluster child
PR-URL: #23125 Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: Denys Otrishko <[email protected]>
1 parent 2dd157f commit 0f133eb

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

lib/internal/cluster/child.js

+11-12
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const { owner_symbol } = require('internal/async_hooks').symbols;
77
const Worker = require('internal/cluster/worker');
88
const { internal, sendHelper } = require('internal/cluster/utils');
99
const cluster = new EventEmitter();
10-
const handles = {};
10+
const handles = new Map();
1111
const indexes = new Map();
1212
const noop = () => {};
1313

@@ -111,12 +111,12 @@ function shared(message, handle, indexesKey, cb) {
111111

112112
handle.close = function() {
113113
send({ act: 'close', key });
114-
delete handles[key];
114+
handles.delete(key);
115115
indexes.delete(indexesKey);
116116
return close.apply(this, arguments);
117117
}.bind(handle);
118-
assert(handles[key] === undefined);
119-
handles[key] = handle;
118+
assert(handles.has(key) === false);
119+
handles.set(key, handle);
120120
cb(message.errno, handle);
121121
}
122122

@@ -144,7 +144,7 @@ function rr(message, indexesKey, cb) {
144144
return;
145145

146146
send({ act: 'close', key });
147-
delete handles[key];
147+
handles.delete(key);
148148
indexes.delete(indexesKey);
149149
key = undefined;
150150
}
@@ -166,15 +166,15 @@ function rr(message, indexesKey, cb) {
166166
handle.getsockname = getsockname; // TCP handles only.
167167
}
168168

169-
assert(handles[key] === undefined);
170-
handles[key] = handle;
169+
assert(handles.has(key) === false);
170+
handles.set(key, handle);
171171
cb(0, handle);
172172
}
173173

174174
// Round-robin connection.
175175
function onconnection(message, handle) {
176176
const key = message.key;
177-
const server = handles[key];
177+
const server = handles.get(key);
178178
const accepted = server !== undefined;
179179

180180
send({ ack: message.seq, accepted });
@@ -207,17 +207,16 @@ function _disconnect(masterInitiated) {
207207
}
208208
}
209209

210-
for (var key in handles) {
211-
const handle = handles[key];
212-
delete handles[key];
210+
handles.forEach((handle) => {
213211
waitingCount++;
214212

215213
if (handle[owner_symbol])
216214
handle[owner_symbol].close(checkWaitingCount);
217215
else
218216
handle.close(checkWaitingCount);
219-
}
217+
});
220218

219+
handles.clear();
221220
checkWaitingCount();
222221
}
223222

0 commit comments

Comments
 (0)