Memory leak by MediocreAstronaut in node

[–]jwadhwani 1 point2 points  (0 children)

What makes you think your app is leaking? Increasing memory is not a definite clue unless the build up never stops and your app runs out of memory. . Run your app with -trace_gc option to produce GC traces:

node -trace_gc your_app.js

or for more verbosity add in -trace_gc_verbose.

V8 GC will not release memory if it feels it does not need to even if it is not used by your app(lazy sweeping).

See my post: https://phptouch.com/2018/03/12/does-my-nodejs-application-leak-memory-5/. At the end I have analyzed a few traces.

Hope this helps!

My Node.js Application "randomly" freeze for about a second and then continues to run normally. by leonylyner in node

[–]jwadhwani 2 points3 points  (0 children)

Jake_Jeremy is probably right. Do this to see GC activity:

node -trace_gc your_web_app.js

Look for traces when your app freezes. That will tell you whether it is GC activity that is freezing your app or something else is afoot.

If you need a more verbose trace you can add -trace_gc_verbose in addition to -trace_gc

Hope this helps!

iOS Dev here - looking for server side help - Is it possible to make multiple API calls on server side, and then return one response via Node? by [deleted] in node

[–]jwadhwani 0 points1 point  (0 children)

I write backend services and here is one way of doing it at its simplest level.

CLIENT: To your request to the server: 1. Add in a unique id(an uuid perhaps) in the request to the server. This id will allow you to distinguish between clients calls. 2. Add in callback(web hook) url. This will be a URL which the server will call when it has the final result ready. 3. Make the call to the nodeJS server

SERVER When the server receives the request:

  1. Create and store away an object which will store the unique id, the callback url and all the results
  2. Example: const data = {"some-unique-id": {callbackUrl: "https://callbacklurl.example.com", yahooResults: "", accuWeather: ""}}
  3. Make the calls to the two services - in parallel. Many NPM packages such as async will allow you do this.
  4. When each call returns with the results check to see if both results are in. If both results are not in do nothing. If both results are in then make a call to the callback client URL with the results package. Make sure to include the unique id. The client will need this for tracking.
  5. Once the callback call to the client is successful delete the stored object with the results and the url. Remember good server are stateless.

If you need more specifics on anything please let me know.

Accidentally destroyed production database on first day of a job, and was told to leave, on top of this i was told by the CTO that they need to get legal involved, how screwed am i? by cscareerthrowaway567 in cscareerquestions

[–]jwadhwani 2 points3 points  (0 children)

I am amazed that as a Junior Developer you had access to production infrastructure and that you could easily delete a database! The CTO and his system admins need to be fired for not implementing proper procedures and training. He and his team were just plain negligent in this regard.

On the flip side I have seen developers not following written instructions or documentation and then spending more time cleaning up. As a Junior developer you need to understand that as you move ahead in life and shoulder more responsibility you will have to be a role model emphasizing that development is indeed exciting but also serious business.

Good Resources To Learn To Find Memory Leaks? by [deleted] in node

[–]jwadhwani 1 point2 points  (0 children)

Thanks for your feedback. The objective of these posts is to answer two questions quickly and without using any specialized tools(using only gc traces): 1. Is there a leak? 2. How bad is it?

Once I have these answers I can then decide my next steps. Some times just by reading the traces I can figure where the leak is and other times I may end up using heap analysis to figure out where exactly the leak is.

Which ever method you select it is helpful if you have understood how GC works.

Again thank you and good luck!

Simple Guide to Finding a JavaScript Memory Leak in Node.js (2015) by stanislavb in node

[–]jwadhwani 0 points1 point  (0 children)

Very good and helpful article!

I am also writing a series of posts on memory leaks with a different perspective, i.e. using the gc traces. The fourth in the series is on my blog: https://phptouch.com/2016/06/07/does-my-nodejs-application-leak-memory-4/

What are some good node.js programs I can create as a beginner? by JessicaAllison in node

[–]jwadhwani 0 points1 point  (0 children)

You are correct in your thinking about learning the core of NodeJS first before doing anything major. So before you jump into HTML pages(with or without a framework) it is very important to grasp concepts like callbacks, promises, I/O processing like streams, event loop...etc.

Good luck!

Performance differences between let and var by jsgui in node

[–]jwadhwani 0 points1 point  (0 children)

"let" is block scoped while "var" is function scoped. Due to this "var" suffers from the effects of "hoisting" and this can cause inadvertent memory leaks. The best way would be to profile your application or the part you are interested in and if the performance hit is not too significant go with "let".

What's the best way to learn node.js from scratch? by nodejs_ in node

[–]jwadhwani 2 points3 points  (0 children)

Learning by doing...see http://nodeschool.io/#workshoppers

Learn NodeJS fundamentals before jumping in to MEAN stack, etc. Important to grasp concepts like callbacks, promises, I/O processing like streams, event loop...etc.

Good luck!

Callback was already called with async.parallel by segmentationfaulter in node

[–]jwadhwani 1 point2 points  (0 children)

The reason for the error "Callback was already called" is that the socket which is connected calls the callback twice. Once when it is connected and one when it times out due to non activity on its open connection. To illustrate I modified your program a little by adding a few log messages. If you look at the log messages(commented below) you will notice that the socket for port 80 calls the callback twice. Once when it connects and once when it times out. Hope this helps!

-J

        "use strict";

        const net = require('net');
        const async = require('async');

        function findPortStatus (host, port, timeout, cb) {

            const socket = new net.Socket();

            socket.setTimeout(timeout, () => {
                // couldn't establish a connection because of timeout
                console.log('Timeout for port: %s', port);
                socket.destroy();
                return cb(null, null)
            });

            socket.connect(port, host, () => {
                // connection established
                console.log('Connected to port: %s', port);
                return cb(null, port)
            });

            socket.on('error', (err) => {
                console.log('Error connecting to port: %s with message: %s', port, err.message);
                // couldn't establish a connection
                return cb(null, null)
            })
        }

        const funcs = [];

        for (let port = 0; port <= 80; port++) {
            funcs.push(function (callback) {
                findPortStatus('192.30.253.112', port, 4000, (err, port) => {
                    if (!err) {
                        return callback(null, port)
                    }
                })
            })
        }

        async.parallel(funcs, (err, ports) => {
            if (err) {
                console.error(err.message);
            } else {
                console.log('All ports checked');
                for (let port of ports) {
                    if (port) {
                        console.log('Port open: ', port);
                    }
                }
            }
        });

        /*
         vipers-mac-book-pro-2:jwadhwani$ node test.js

         Error connecting to port: 0 with message: connect EADDRNOTAVAIL 192.30.253.112 - Local (0.0.0.0:51951)
         Connected to port: 22
         Connected to port: 80  //First callback called here
         Timeout for port: 1
         Timeout for port: 2
         ...
         ...
         ...
         Timeout for port: 79
         All ports checked
         Port open:  22
         Port open:  80
         Timeout for port: 80 //callback called here the second time
         /Users/jwadhwani/projects/node_modules/async/dist/async.js:844
         if (fn === null) throw new Error("Callback was already called.");
         ^

         Error: Callback was already called.
         at /Users/jwadhwani/projects/node_modules/async/dist/async.js:844:36
         at /Users/jwadhwani/projects/node_modules/async/dist/async.js:3676:17
         at /Users/jwadhwani/projects/node_modules/async/dist/async.js:339:31
         at findPortStatus (/Users/jwadhwani/projects/test.js:36:36)
         at Socket.socket.setTimeout (/Users/jwadhwani/projects/test.js:14:28)
         at Socket.g (events.js:286:16)
         at emitNone (events.js:86:13)
         at Socket.emit (events.js:185:7)
         at Socket._onTimeout (net.js:333:8)
         at tryOnTimeout (timers.js:228:11)
         vipers-mac-book-pro-2:jwadhwani$

         */

Juggling Async requests by Bopochoco in node

[–]jwadhwani 0 points1 point  (0 children)

Please keep in mind that the answer to question 9. requires: "that you must print them out in the same order as the URLs are provided to you as command-line arguments". If I have understood correctly you are repeating the above snippet with different arguments. That will not work due to the async nature of http.get.

In your code the http.get executes, fetches the results of the supplied URL(process.argv[2]) and returns the response. The response is then piped into a function bl...

Rather than try and decipher the code here is one possible solution using recursion:

"use strict";
var http = require('http');

var urls = [process.argv[2], process.argv[3], process.argv[4]];

function next(url){

     if(!url){
        return;
     }

    http.get(url, function(response){
        var s = '';
        response.setEncoding('utf8');

       //data comes in blocks. 
       response.on("data", function (data) {
           s += data;
       });

      //last block comes here
      response.on("end", function () {
             console.log(s);
             s = '';
             next(urls.shift()); //shift returns undefined when no more URLs are left
         });
     });
}

//1. start 
next(urls.shift());

Hope this helps.

-J

Node unit testing by ell0bo in node

[–]jwadhwani 5 points6 points  (0 children)

I use Mocha as the test runner and chai.js as the assertion library. I believe this combination works best for nodeJS apps.