Welcome! Log In Create A New Profile

Advanced

How to reduce CPU usage when using lua http resty from NGINX?

Posted by hkahlouche 
How to reduce CPU usage when using lua http resty from NGINX?
April 26, 2016 02:17PM
I am using NGINX 1.9.13 with lua-resty-http.
The CPU load is too hight when making HTTP requests via the lua http resty module (https://github.com/liseen/lua-resty-http) :

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
8446 root 20 0 209m 142m 1748 R 99.1 3.7 0:31.27 nginx

The more the body of the request is big, the higher the CPU. In this case the body was 20MB. If we add multiple requests,
it will kill the CPU.

QUESTION: Is there a way to reduce the CPU load when using LUA http resty?

I added below the NGINX Lua code as well as the NGINX configuration:
NGINX is used a front-end proxy towards an internal web server.

The below lua code (content.lua) is called multiple times from NGINX server for each curl http request :

local ok, code, headers, status, body = nil, 0, nil, 0, nil
local Http = require("resty.http") -- https://github.com/liseen/lua-resty-http
local Httpc = Http.new()
local req_headers = ngx.req.get_headers()
req_headers["range"] = "bytes=0-20000000" -- example of range
req_headers["connection"] = "keep-alive"
req_headers["if-range"] = nil

local req_params = {
url = "http://127.0.01:8099" .. ngx.var.request_uri,
method = 'GET',
headers = req_headers,
keepalive = 30000 -- 30 seconds
}

req_params["code_callback"] =
function(statuscode)
code = statuscode
end

req_params["header_callback"] =
function(headers)
if headers and headers["x-custom"] and ngx.re.match(headers ["x-custom"],"CUSTOM_STR") then
--increment a counter
else
--increment another counter
end
end

req_params["body_callback"] =
function(data, chunked_header, ...)
if chunked_header or code ~= 206 then return end
ngx.print(data)
ngx.flush(true)
end

ok, code, headers, status, body = Httpc:request(req_params)
local start, stop = 0, 20000000 -- example of value
if (code == 206) and body then
ngx.print(string.sub(body, start, stop))
end

Below is my NGINX configuration:

worker_processes auto;
events {
worker_connections 2048;
multi_accept on;
use epoll;
}

http {
include ./mime.types;
default_type application/octet-stream;

proxy_cache_bypass 1;
proxy_no_cache 1;
proxy_set_header Host $http_host;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_buffering off;
proxy_buffer_size 128k;
proxy_buffers 100 128k;
sendfile on;
tcp_nopush on;
keepalive_timeout 10;
reset_timedout_connection on;

upstream backend {
server 127.0.0.1:8090;
keepalive 20;
}

server {
listen 192.168.0.10:8080;

location ~ \.(mp4|jpg)$ {
lua_check_client_abort on;
lua_code_cache on;
lua_http10_buffering off;

#request the lua code
content_by_lua_file '/opt/location/content.lua';
}
}

server {
listen 127.0.0.1:8099;
access_log off;

location / {
proxy_pass http://backend;
}
}

}
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 177
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready