Tuesday, June 18, 2013

Multi-worker statistics and control with nginx "per-worker-listener" patch

Today I've written a small nginx patch providing a feature that solves many problems in RTMP-HTTP integration namely statistics and control. Statistics and control is known to work bad in multi-worker mode since HTTP request goes to a random worker. So the client receives random worker statistics or performs a control action with a worker that didn't receive previous actions from user.

The patch is named per-worker-listener. It can be downloaded here. When applied to nginx it opens ports for connecting to the worker you want, not just a random worker. Each worker has its own port number so you can choose the port to connect. The new syntax extends listen directive with a new option per_worker. The port specified in listen directive becomes base port number for worker listeners.

listen 80; # usual listen directive
listen 9000 per_worker; # per-worker listener

With the above configuration 1st worker will listen to 9000 port, 2nd to 9001 port, 3rd to 9002 port etc. To make that work please turn off accept_mutex. Listeners with no per_worker work as usual.

events {
worker_connections 1024;
accept_mutex off;
}


The patch is especially useful with stat and control handlers of nginx-rtmp-module.

server {
listen 9000 per_worker;
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /tmp;
}
location /control {
rtmp_control all;
}
}


Now see 3rd worker stats at the location http://localhost:9002/stat.

11 comments:

  1. I meet a problem,please help me !
    I use the command:curl http://localhost:8080/control/drop/publisher?app=hls&name=mystream;
    but it returns:Live stream not found;
    How can I solve it?Please help me !

    ReplyDelete
  2. Replies
    1. This comment has been removed by the author.

      Delete
  3. nginx patch fail on nginx-1.1.19 Ubuntu 12.04.2 LTS
    root@Cdn56:/usr/src/nginx/nginx-1.1.19# patch -p1 < per-worker-listener
    patching file src/core/ngx_connection.c
    patching file src/core/ngx_connection.h
    patching file src/http/ngx_http.c
    Hunk #1 succeeded at 1786 (offset -22 lines).
    patching file src/http/ngx_http_core_module.c
    Hunk #1 succeeded at 3781 (offset -221 lines).
    patching file src/http/ngx_http_core_module.h
    patching file src/os/unix/ngx_process.c
    patching file src/os/unix/ngx_process.h
    patching file src/os/unix/ngx_process_cycle.c
    Hunk #1 FAILED at 728.
    Hunk #2 succeeded at 953 (offset -6 lines).
    1 out of 2 hunks FAILED -- saving rejects to file src/os/unix/ngx_process_cycle.c.rej

    ReplyDelete
    Replies
    1. Your nginx is pretty old. Please try a newer version.

      Delete
  4. hey
    I have nginx 1.4.1 and in my conf i have 4 workers, when i try to use the rtmp_control module for a Drop
    sometimes i have a "Live stream not found" response

    is any method to drop all conections on all workers?

    thanks

    ReplyDelete
    Replies
    1. You should drop connection for each worker manually. Workers are independent so you cannot drop all connections on all workers.

      Delete
  5. Hi,
    I have error in compiling Ngingx-1.4.2
    I patch as:
    patch -p1 < dav-copy-hardlink
    patch -p1 < mmap-alloc
    patch -p1 < per-worker-listener

    Config:
    ./configure --add-module=/root/nginx-rtmp-module-master/

    make has an error:

    cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I/root/nginx-rtmp-module-master/ -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
    -o objs/src/core/ngx_palloc.o \
    src/core/ngx_palloc.c
    cc1: warnings being treated as errors
    src/core/ngx_palloc.c: In function ângx_destroy_poolâ:
    src/core/ngx_palloc.c:139: error: âdirectâ is used uninitialized in this function
    src/core/ngx_palloc.c:141: error: âlogâ may be used uninitialized in this function
    make[1]: *** [objs/src/core/ngx_palloc.o] Error 1
    make[1]: Leaving directory `/root/nginx-1.4.2'
    make: *** [build] Error 2

    please help me. Thanks you.

    ReplyDelete
    Replies
    1. mmap-alloc & dav-copy hardlink are old patches, I'm not sure they are compatible with current nginx versions.

      Delete
  6. This comment has been removed by the author.

    ReplyDelete
  7. Till today failed to run multiple cpu/core with rtmp-nginx service. Anybody there to help me out!!!

    ReplyDelete