Nginx Socket.io nodejs утечка памяти - PullRequest
1 голос
/ 01 апреля 2020

Мы используем nodejs (express) и nginx, и когда мы включаем socket.io, использование памяти увеличивается слишком быстро и не уменьшается в течение 1 часа + 1 ГБ, однажды полная утечка памяти 12 ГБ и мой сервер замерзает. Я попытался выключить socket.io, и он начинает работать нормально, память выглядит нормально. Я не могу найти, в чем проблема, вы, ребята, пожалуйста, посмотрите?

enter image description here

Nginx conf:

worker_processes 4;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

worker_rlimit_nofile 30000;

events {
    worker_connections 10000;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
        client_max_body_size 500M;
    #proxy_read_timeout 320;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;

    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

conf сайта:

upstream http_backend {
    server 127.0.0.1:8080;
    keepalive 32;
}

    map $request $loggable {
        ~/api/socket/* 0;
        default 1;
    }
server {
        listen 80;
        listen [::]:80;
        server_name  amjilt.com www.amjilt.com;
        return 301 https://$server_name$request_uri;
}
server {
        listen 7070;
        listen [::]:7070;
        server_name  amjilt.com www.amjilt.com;
        return 301 https://$server_name$request_uri;
}
server {
        listen       443 ssl http2 default_server;
        listen       [::]:443 ssl http2 default_server;
        server_name  amjilt.com www.amjilt.com;

        ssl on;
        ssl_certificate /etc/nginx/cert/amjilt/cert_chain.crt;
        ssl_certificate_key /etc/nginx/cert/amjilt/amjilt_2.key;

        ssl_session_cache  builtin:1000  shared:SSL:10m;
        ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
        ssl_prefer_server_ciphers on;

        client_max_body_size 500M;
        client_body_buffer_size 500M;
        proxy_buffer_size   16M;
        proxy_buffers   24 8M;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;

        location /css{
                root /home/amjilt/projects/amjilt_react/static;
        }
        location /js{
                root /home/amjilt/projects/amjilt_react/static;
        }
        location /images{
                root /home/amjilt/projects/amjilt_react/static;
        }
        location /fonts{
                root /home/amjilt/projects/amjilt_react/static;
        }
        location /dist{
                root /home/amjilt/projects/amjilt_react/static;
        }
        location /tmp{
                root /home/amjilt/projects/amjilt_api/static;
        }
        location /images/upload{
                root /home/amjilt/projects/amjilt_api/static;
        }
        location /adm/{
                root /home/amjilt/projects/amjilt_api/static;
        }
        location /images/avatar{
                root /home/amjilt/projects/amjilt_api/static;
        }
        location /api/video/show{
                expires off;
                proxy_buffering off;
                chunked_transfer_encoding on;
                proxy_pass http://http_backend;
                proxy_http_version 1.1;
                proxy_set_header Connection "";
        }
        location /api/video/mobile{
                expires off;
                proxy_buffering off;
                chunked_transfer_encoding on;
                proxy_pass http://http_backend;
                proxy_http_version 1.1;
                proxy_set_header Connection "";
        }
        location /api/pdf/show{
                expires off;
                proxy_buffering off;
                chunked_transfer_encoding on;
                proxy_pass http://http_backend;
                proxy_http_version 1.1;
                proxy_set_header Connection "";
        }
        location / {
                proxy_pass http://localhost:8080;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_set_header Host $host;
        }

    access_log /var/log/nginx/access.log combined if=$loggable;
        # gzip
        gzip_types text/css text/less text/plain text/xml application/xml application/json application/javascript;
        gzip on;
}

сервер:

const io = new SocketIo(s, {path: '/api/socket' , secure : true , rejectUnauthorized: false});
io.adapter(redis({key:'amjilt', host: 'localhost', port: 6379 }));
io.set('transports', [
    'websocket',
    //'polling'
]);
io.set('browser client gzip', true);
io.set('browser client minification', true);
io.set('browser client etag', true);
app.set('socketio', io);
const socketEvents = require('./src/SocketEvents')(io);
server(config, app);
io.use(function(socket, next){
    var token = null;
    if(socket.request.headers.cookie){
        var cookies = cookie.parse(socket.request.headers.cookie);
        token = cookies.token;
    }
    if(!token){
        var query = socket.handshake.query;
        if(query){
            token = query.token;

        }
    }
    if (token) {
        jsonwebtoken.verify(token, 'KDrL5JEaHklA3e9TjJSNaZXQGapZTRZh', function (err, decoded) {
            if (err) {
                next(new Error('Authentication error token expired'));
            } else {
                User.findOne({_id: decoded.id}, {
                    email: 1,
                    email_extra:1,
                    avatar: 1,
                    roles: 1,
                    name: 1,
                    full_name: 1,
                    last_name: 1,
                    description: 1,
                    cash: 1,
                    cover: 1,
                    friends: 1,
                    slug: 1,
                }, function (err, user) {
                    socket.request.user = user;
                    next();
                });

            }
        });
    } else {
        next(new Error('Authentication error'));
    }
});
ServerEvents.addNotificationListener(onNotificationReceived);
function onNotificationReceived(userId, notification){
    io.sockets.in(userId).emit('notification received', notification);
}
...