Skip to content
This repository was archived by the owner on May 14, 2024. It is now read-only.

Commit dede2e6

Browse files
authored
addition of example for threading via net server
Addition of working example demonstrating threading via clustering and a net server forwarding socket connections to ldapjs.
1 parent c97f1be commit dede2e6

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const cluster = require('cluster');
2+
const ldap = require('ldapjs');
3+
const net = require('net');
4+
const os = require('os');
5+
6+
const threads = [];
7+
threads.getNext = function () {
8+
return (Math.floor(Math.random() * this.length));
9+
};
10+
11+
const serverOptions = {
12+
port: 1389
13+
};
14+
15+
if (cluster.isMaster) {
16+
const server = net.createServer(serverOptions, (socket) => {
17+
socket.pause();
18+
console.log('ldapjs client requesting connection');
19+
let routeTo = threads.getNext();
20+
threads[routeTo].send({ type: 'connection' }, socket);
21+
});
22+
23+
for (let i = 0; i < os.cpus().length; i++) {
24+
let thread = cluster.fork({
25+
'id': i
26+
});
27+
thread.id = i;
28+
thread.on('message', function (msg) {
29+
30+
});
31+
threads.push(thread);
32+
}
33+
34+
server.listen(serverOptions.port, function () {
35+
console.log('ldapjs listening at ldap://127.0.0.1:' + serverOptions.port);
36+
});
37+
} else {
38+
const server = ldap.createServer(serverOptions);
39+
40+
let threadId = process.env.id;
41+
42+
process.on('message', (msg, socket) => {
43+
switch (msg.type) {
44+
case 'connection':
45+
server.newConnection(socket);
46+
socket.resume();
47+
console.log('ldapjs client connection accepted on ' + threadId.toString());
48+
}
49+
});
50+
51+
server.search('dc=example', function (req, res, next) {
52+
console.log('ldapjs search initiated on ' + threadId.toString());
53+
var obj = {
54+
dn: req.dn.toString(),
55+
attributes: {
56+
objectclass: ['organization', 'top'],
57+
o: 'example'
58+
}
59+
};
60+
61+
if (req.filter.matches(obj.attributes))
62+
res.send(obj);
63+
64+
res.end();
65+
});
66+
}

0 commit comments

Comments
 (0)