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

Commit f5a8b6a

Browse files
authored
Merge pull request #727 from CoryGH/master
connection router to accomodate multithreading
2 parents 505f1f8 + 6d1f23a commit f5a8b6a

File tree

5 files changed

+255
-3
lines changed

5 files changed

+255
-3
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+
```js
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+
```
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
const routeTo = threads.getNext()
20+
threads[routeTo].send({ type: 'connection' }, socket)
21+
})
22+
23+
for (let i = 0; i < os.cpus().length; i++) {
24+
const thread = cluster.fork({
25+
id: i
26+
})
27+
thread.id = i
28+
thread.on('message', function () {
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+
const 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) {
52+
console.log('ldapjs search initiated on ' + threadId.toString())
53+
const 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)) { res.send(obj) }
62+
63+
res.end()
64+
})
65+
}

examples/cluster-threading.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
const cluster = require('cluster')
2+
const ldap = require('ldapjs')
3+
const os = require('os')
4+
5+
const threads = []
6+
threads.getNext = function () {
7+
return (Math.floor(Math.random() * this.length))
8+
}
9+
10+
const serverOptions = {
11+
connectionRouter: (socket) => {
12+
socket.pause()
13+
console.log('ldapjs client requesting connection')
14+
const routeTo = threads.getNext()
15+
threads[routeTo].send({ type: 'connection' }, socket)
16+
}
17+
}
18+
19+
const server = ldap.createServer(serverOptions)
20+
21+
if (cluster.isMaster) {
22+
for (let i = 0; i < os.cpus().length; i++) {
23+
const thread = cluster.fork({
24+
id: i
25+
})
26+
thread.id = i
27+
thread.on('message', function () {
28+
29+
})
30+
threads.push(thread)
31+
}
32+
33+
server.listen(1389, function () {
34+
console.log('ldapjs listening at ' + server.url)
35+
})
36+
} else {
37+
const threadId = process.env.id
38+
serverOptions.connectionRouter = () => {
39+
console.log('should not be hit')
40+
}
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) {
52+
console.log('ldapjs search initiated on ' + threadId.toString())
53+
const 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)) { res.send(obj) }
62+
63+
res.end()
64+
})
65+
}

lib/server.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,9 @@ function Server (options) {
313313
return c
314314
}
315315

316-
function newConnection (conn) {
316+
self.newConnection = function (conn) {
317+
// TODO: make `newConnection` available on the `Server` prototype
318+
// https://github.com/ldapjs/node-ldapjs/pull/727/files#r636572294
317319
setupConnection(conn)
318320
log.trace('new connection from %s', conn.ldap.id)
319321

@@ -438,9 +440,9 @@ function Server (options) {
438440
this.routes = {}
439441
if ((options.cert || options.certificate) && options.key) {
440442
options.cert = options.cert || options.certificate
441-
this.server = tls.createServer(options, newConnection)
443+
this.server = tls.createServer(options, options.connectionRouter ? options.connectionRouter : self.newConnection)
442444
} else {
443-
this.server = net.createServer(newConnection)
445+
this.server = net.createServer(options.connectionRouter ? options.connectionRouter : self.newConnection)
444446
}
445447
this.server.log = options.log
446448
this.server.ldap = {

test/server.test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict'
22

3+
const net = require('net')
34
const tap = require('tap')
45
const vasync = require('vasync')
56
const { getSock } = require('./utils')
@@ -331,3 +332,49 @@ tap.test('close passes error to callback', function (t) {
331332
t.end()
332333
})
333334
})
335+
336+
tap.test('multithreading support via external server', function (t) {
337+
const serverOptions = { }
338+
const server = ldap.createServer(serverOptions)
339+
const fauxServer = net.createServer(serverOptions, (connection) => {
340+
server.newConnection(connection)
341+
})
342+
fauxServer.log = serverOptions.log
343+
fauxServer.ldap = {
344+
config: serverOptions
345+
}
346+
t.ok(server)
347+
fauxServer.listen(5555, 'localhost', function () {
348+
t.ok(true, 'server listening on ' + server.url)
349+
350+
t.ok(fauxServer)
351+
const client = ldap.createClient({ url: 'ldap://127.0.0.1:5555' })
352+
client.on('connect', function () {
353+
t.ok(client)
354+
client.unbind()
355+
fauxServer.close(() => t.end())
356+
})
357+
})
358+
})
359+
360+
tap.test('multithreading support via hook', function (t) {
361+
const serverOptions = {
362+
connectionRouter: (connection) => {
363+
server.newConnection(connection)
364+
}
365+
}
366+
const server = ldap.createServer(serverOptions)
367+
const fauxServer = ldap.createServer(serverOptions)
368+
t.ok(server)
369+
fauxServer.listen(0, 'localhost', function () {
370+
t.ok(true, 'server listening on ' + server.url)
371+
372+
t.ok(fauxServer)
373+
const client = ldap.createClient({ url: fauxServer.url })
374+
client.on('connect', function () {
375+
t.ok(client)
376+
client.unbind()
377+
fauxServer.close(() => t.end())
378+
})
379+
})
380+
})

0 commit comments

Comments
 (0)