| 266 | } |
| 267 | |
| 268 | function queryServer(worker, message) { |
| 269 | // Stop processing if worker already disconnecting |
| 270 | if (worker.exitedAfterDisconnect) |
| 271 | return; |
| 272 | |
| 273 | const key = `${message.address}:${message.port}:${message.addressType}:` + |
| 274 | `${message.fd}` + (message.port === 0 ? `:${message.index}` : ''); |
| 275 | const cachedHandle = handles.get(key); |
| 276 | let handle; |
| 277 | if (cachedHandle && !cachedHandle.has(worker)) { |
| 278 | handle = cachedHandle; |
| 279 | } |
| 280 | |
| 281 | if (handle === undefined) { |
| 282 | let address = message.address; |
| 283 | |
| 284 | // Find shortest path for unix sockets because of the ~100 byte limit |
| 285 | if (message.port < 0 && typeof address === 'string' && |
| 286 | process.platform !== 'win32') { |
| 287 | |
| 288 | address = path.relative(process.cwd(), address); |
| 289 | |
| 290 | if (message.address.length < address.length) |
| 291 | address = message.address; |
| 292 | } |
| 293 | |
| 294 | // UDP is exempt from round-robin connection balancing for what should |
| 295 | // be obvious reasons: it's connectionless. There is nothing to send to |
| 296 | // the workers except raw datagrams and that's pointless. |
| 297 | if (schedulingPolicy !== SCHED_RR || |
| 298 | message.addressType === 'udp4' || |
| 299 | message.addressType === 'udp6') { |
| 300 | handle = new SharedHandle(key, address, message); |
| 301 | } else { |
| 302 | handle = new RoundRobinHandle(key, address, message); |
| 303 | } |
| 304 | |
| 305 | if (!cachedHandle) { |
| 306 | handles.set(key, handle); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | handle.data ||= message.data; |
| 311 | |
| 312 | // Set custom server data |
| 313 | handle.add(worker, (errno, reply, serverHandle) => { |
| 314 | if (!errno) { |
| 315 | handles.set(key, handle); // Update in case it was replaced. |
| 316 | } |
| 317 | const { data } = handles.get(key); |
| 318 | if (!cachedHandle && errno) { |
| 319 | handles.delete(key); |
| 320 | } |
| 321 | |
| 322 | send(worker, { |
| 323 | errno, |
| 324 | key, |
| 325 | ack: message.seq, |