There is a classic server on Node.JS that generates requests for LDAP through LDAPjs, the problem is that if you make many requests in a row, say 30-40, then Node.JS hangs, that is, in the console it is clear that it starts processing requests with a delay in a few minutes, it will execute 3-5 requests and hangs, first you sinned on LDAPjs, but then I noticed that the static is not given either, that is, the whole server is down, there were no such problems before LDAPjs, there is also a search for JSON and it works stably on a sufficiently large number of requests, it is clearly a matter of Node.JS itself, the only thing that comes and mind is the fact that LDAPjs exceeds some threshold for using resources in Node.JS ps itself: after a series of experiments, it turned out that if the function is run in a for loop, it will work out 100 requests, but as soon as I make a request web muzzle everything falls, test code cleaned here

/*jslint node: true, plusplus: true, vars: true*/ 'use strict'; var fs = require("fs"); var http = require("http"); var path = require("path"); var url = require('url'); var util = require('util'); var ldap = require('ldapjs'); var pbdb_index = fs.readFileSync('./index.html'); var jquery = fs.readFileSync('./jquery.js'); var server_port = '80'; http.createServer(function (req, res) { if (req.method !== undefined && (req.method).toString() === "POST") { var body = ''; req.on('data', function (data) { body += data; }); req.on('end', function () { var body_arr = decodeURIComponent(body); body_arr = body_arr.replace(/[+]/gi, " ").split('='); res.writeHead(200, {'Content-Type': 'text/html; charset=utf8'}); if (req.url === "/ldap" && body_arr[1].length > 2) { search_in_LDAP(body_arr[0], body_arr[1], function (err, ret_array) { ret_array = ret_array.toString(); res.end(ret_array); } ); } }); } if (req.method !== undefined && (req.method).toString() === "GET") { if (url.parse(req.url).pathname === '/jquery.js') { res.writeHead(200, {'Content-Type': 'text/javasript'}); res.end(jquery); } else { res.writeHead(200, {'Content-Type': 'text/html; charset=utf8'}); res.end(pbdb_index); } } }).listen(server_port); function search_in_LDAP(attribute_id1, search_str, callback) { var client = ldap.createClient({ url: 'ldap://10.0.0.1:389/DC=domainname,DC=ru' }); var ldap_user = 'login'; var ldap_pass = 'password'; var ret_array = []; var attribute_id = "sn"; var opts = { filter: "(&(" + attribute_id + "=" + search_str + "*))", scope: "sub" }; client.bind(ldap_user, ldap_pass, function (err, res) { if (err) { console.log("ERROR: bind error" + err); return 1; } client.search('OU=OU_Name,DC=domainname,DC=ru', opts, function (err, search) { search.on('searchEntry', function (entry) { var user = entry.object; var tem_json_obj = {}; tem_json_obj.sn = entry.object.sn || ""; tem_json_obj.cn = entry.object.cn || ""; tem_json_obj.mail = entry.object.mail || ""; tem_json_obj.department = entry.object.department || ""; tem_json_obj.description = entry.object.description || ""; tem_json_obj.physicalDeliveryOfficeName = entry.object.physicalDeliveryOfficeName || ""; tem_json_obj.title = entry.object.title || ""; tem_json_obj.telephoneNumber = entry.object.telephoneNumber || ""; tem_json_obj.distinguishedName = entry.object.distinguishedName || ""; ret_array.push(tem_json_obj); }); search.on('end', function (err) { client.unbind(function (err, data) { callback(err, ret_array); }); }); }); }); } function strip_string_gi(in_str, regexp_str) { var regexp_key = "gi"; var out_str = []; if (typeof in_str === 'string') { var reg_exp = new RegExp(regexp_str, regexp_key); out_str = in_str.match(reg_exp); if (out_str !== null && out_str.length > 0) { out_str = out_str.join(''); return out_str; } } console.log("strip_string function ERROR:\n" + in_str + "\n" + regexp_str); return null; } 
  • Rather, requests in LDAP are trying to do some part synchronously and everything is waiting for them. The javascript code is executed in one thread. - Qwertiy
  • Well, then why not immediately but after 50 requests, and why it hangs so long, it didn’t hang out in 30 minutes, moreover with the debug option it just do it in 20 minutes that the process was completed and that's all - pnp2000
  • In general, now it sticks almost immediately, after several entries of the search, each time Node more and more upsets me :( - pnp2000
  • I suspected Windows, but on a pure virtual machine everything is the same, you see the problem in the depth of the node is hidden, because the search in LDAPjs is asynchronous and if it hits the whole node, it turns out the node itself bug - pnp2000
  • If only the code showed what modules you are using. - Qwertiy

1 answer 1

1) Maybe you do not always have this condition

 if (req.url === "/ldap" && body_arr[1].length > 2) 

and as a result, you do not close the res.end connection

there is also no connection closure on

 if (req.method !== undefined && (req.method).toString() === "POST") { 

The client can request a type of OPTIONS, DELETE, etc. and your connection will hang up before the timeout

2) update the node

  • Thanks, I resruded res.end, although it is strange that the node reacted to it like this, apparently deeply in the guts it incorrectly fulfills such an event - pnp2000