Skip to content

Instantly share code, notes, and snippets.

@alexeyten
Last active October 27, 2015 17:59

Revisions

  1. alexeyten revised this gist Oct 27, 2015. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,8 @@ I've tested this code with Node.js 4.2.1

    This server just response with `OK` and restart workers after 200-300 completed requests. With default cluster scheduler (which is `SCHED_RR`) server eventually loses some requests and `ab` stops with timeout error. When I uncomment line 5 and use `SCHED_NONE` scheduler, then server works fine.


    My Node.js version:
    ```
    $ apt-cache policy nodejs
    nodejs:
  2. alexeyten revised this gist Oct 27, 2015. 1 changed file with 15 additions and 2 deletions.
    17 changes: 15 additions & 2 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,16 @@
    По-умолчанию в NodeJS 0.12 и выше используется планировщик `SCHED_RR` и при большой нагрузке часть запросов _теряется_.
    I've tested this code with Node.js 4.2.1

    Если раскомментировать пятую строчку (`SCHED_NONE`), то сервер легко обрабатывает любое количество запросов.
    This server just response with `OK` and restart workers after 200-300 completed requests. With default cluster scheduler (which is `SCHED_RR`) server eventually loses some requests and `ab` stops with timeout error. When I uncomment line 5 and use `SCHED_NONE` scheduler, then server works fine.

    ```
    $ apt-cache policy nodejs
    nodejs:
    Installed: 4.2.1-2nodesource1~wily1
    Candidate: 4.2.1-2nodesource1~wily1
    Version table:
    *** 4.2.1-2nodesource1~wily1 0
    500 https://deb.nodesource.com/node_4.x/ wily/main amd64 Packages
    100 /var/lib/dpkg/status
    0.10.25~dfsg2-2ubuntu1 0
    500 http://ru.archive.ubuntu.com/ubuntu/ wily/universe amd64 Packages
    ```
  3. alexeyten revised this gist Oct 27, 2015. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    По-умолчанию в NodeJS 0.12 и выше используется планировщик `SCHED_RR` и при большой нагрузке часть запросов _теряется_.

    Если раскомментировать пятую строчку (`SCHED_NONE`), то сервер легко обрабатывает любое количество запросов.
  4. alexeyten created this gist Oct 27, 2015.
    40 changes: 40 additions & 0 deletions server.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    var cluster = require('cluster');
    var http = require('http');

    //cluster.schedulingPolicy = cluster.SCHED_RR; // default for 0.12
    //cluster.schedulingPolicy = cluster.SCHED_NONE; // default for 0.10

    cluster.on('fork', function(w) {
    console.log('fork', w.id);
    var isWorkerTired = false;
    w.on('message', function(msg) {
    if (!isWorkerTired && msg === 'tired') {
    isWorkerTired = true;
    console.log('tired', w.id);
    var newWorker = cluster.fork();
    newWorker.on('listening', function() {
    w.disconnect();
    });
    }
    });
    });

    cluster.on('exit', function(w) {
    console.log('exit', w.id);
    });

    if (cluster.isWorker) {
    var N = (200 + Math.random() * 100)|0;
    http.createServer(function(req, res) {
    N--;
    if (N < 0) {
    process.send('tired');
    }
    res.end('OK\n')
    }).listen(1337);
    } else {
    cluster.fork();
    cluster.fork();
    cluster.fork();
    cluster.fork();
    }
    17 changes: 17 additions & 0 deletions test-rr.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    $ ab -n 10000 -c 5 http://localhost:1337/
    This is ApacheBench, Version 2.3 <$Revision: 1638069 $>
    Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
    Licensed to The Apache Software Foundation, http://www.apache.org/

    Benchmarking localhost (be patient)
    Completed 1000 requests
    Completed 2000 requests
    Completed 3000 requests
    Completed 4000 requests
    Completed 5000 requests
    Completed 6000 requests
    Completed 7000 requests
    Completed 8000 requests
    Completed 9000 requests
    apr_pollset_poll: The timeout specified has expired (70007)
    Total of 9996 requests completed