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

Commit b860932

Browse files
authored
updated documentation
Updated examples documentation.
1 parent dede2e6 commit b860932

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

docs/examples.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,3 +550,76 @@ To test out this example, try:
550550
$ ldapsearch -H ldap://localhost:389 -x -D cn=demo,dc=example,dc=com \
551551
-w demo -b "dc=example,dc=com" objectclass=*
552552
```
553+
554+
# Multi-threaded Server
555+
556+
This example demonstrates multi-threading via the `cluster` module utilizing a `net` server for initial socket receipt. An alternate example demonstrating use of the `connectionRouter` `serverOptions` hook is available in the `examples` directory.
557+
558+
```
559+
const cluster = require('cluster');
560+
const ldap = require('ldapjs');
561+
const net = require('net');
562+
const os = require('os');
563+
564+
const threads = [];
565+
threads.getNext = function () {
566+
return (Math.floor(Math.random() * this.length));
567+
};
568+
569+
const serverOptions = {
570+
port: 1389
571+
};
572+
573+
if (cluster.isMaster) {
574+
const server = net.createServer(serverOptions, (socket) => {
575+
socket.pause();
576+
console.log('ldapjs client requesting connection');
577+
let routeTo = threads.getNext();
578+
threads[routeTo].send({ type: 'connection' }, socket);
579+
});
580+
581+
for (let i = 0; i < os.cpus().length; i++) {
582+
let thread = cluster.fork({
583+
'id': i
584+
});
585+
thread.id = i;
586+
thread.on('message', function (msg) {
587+
588+
});
589+
threads.push(thread);
590+
}
591+
592+
server.listen(serverOptions.port, function () {
593+
console.log('ldapjs listening at ldap://127.0.0.1:' + serverOptions.port);
594+
});
595+
} else {
596+
const server = ldap.createServer(serverOptions);
597+
598+
let threadId = process.env.id;
599+
600+
process.on('message', (msg, socket) => {
601+
switch (msg.type) {
602+
case 'connection':
603+
server.newConnection(socket);
604+
socket.resume();
605+
console.log('ldapjs client connection accepted on ' + threadId.toString());
606+
}
607+
});
608+
609+
server.search('dc=example', function (req, res, next) {
610+
console.log('ldapjs search initiated on ' + threadId.toString());
611+
var obj = {
612+
dn: req.dn.toString(),
613+
attributes: {
614+
objectclass: ['organization', 'top'],
615+
o: 'example'
616+
}
617+
};
618+
619+
if (req.filter.matches(obj.attributes))
620+
res.send(obj);
621+
622+
res.end();
623+
});
624+
}
625+
```

0 commit comments

Comments
 (0)