Heya!
On Friday, August 15, 2014 11:08:34 PM UTC+2, agentzh wrote:
Hello!
On Fri, Aug 15, 2014 at 1:32 PM, info wrote:
>
> This is Windows you know :) things work a bit differently.
But basic things and concepts still remain the same :)
As I found out today, read on cause this is gonna be a big reply :)
that it cannot get reused by future requests. So the chance that you
get a broken connection from the pool is much smaller for relatively
idle servers. Also, the getreusedtimes() result can also be used on
Have a look at the example below, maybe you can give some pointers about this.
> ngxLuaDB is a redis+++ replacement for Windows,
> http://forum.nginx.org/read.php?2,252488
>
Where can I found the source code?
There ain't any source, nginx for Windows has Lua and lua-nginx-module build in, ngxLuaDB are DLL's that are compiled against Luajit so that Windows functionality becomes available inside nginx, its like .so modules can be added, it works the same with a .dll
You should test the following mysql query with a *single* nginx worker
process configured:
select sleep(1);
You were right (however I hate having to say that :)) I read your pdf about the redis presentation.
Try testing the case that pcall actually catches some error :) And
compare that with simply returning and checking an error string ;)
Well it didn't slow things down, its just an extra query which should always work.
Finally, as I've stated in my previous mail, the "nonblocking magic"
cannot happen if you're not using cosockets nor subrequests when
running Lua code doing socket I/O atop the official ngx_lua module :)
See my tested example below :)
Before I paste my work of today I have 2 issues,
1: I would like to make a persistent connection via init_by_lua but when trying I get this error:
2014/08/16 20:45:35 [error] 1516#3236: init_by_lua error: .\openresty\mysql.lua:462: no request found
stack traceback:
[C]: in function 'tcp'
.\openresty\mysql.lua:462: in function 'new'
init_by_lua:5: in main chunk
2: local row = cjson.encode(res) returns for example [{"cn":"Canada"}] from the database,
how do I get the plain value Canada from this table ??
And now the paste which is tested non blocking !
worker_processes 2;
#error_log logs/error.log debug;
error_log logs/error.log;
pid logs/nginx.pid;
events {
worker_connections 8192;
}
http {
log_format main '[$time_local] $remote_addr:$remote_port - $remote_user "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" $upstream_cache_status';
access_log logs/access.log main;
# Initialize a shared Lua cache for SQL queries
lua_shared_dict SqlQueries 10m;
server {
listen 80;
server_name localhost;
root html;
# include debugbylua.lua;
location / {
index index.html index.htm;
try_files $uri $uri/ =404;
}
# Get GeoIP in a MySQL database: http://vincent.delau.net/php/geoip.html
# The old GeoliteIP csv: http://dev.maxmind.com/geoip/legacy/geolite/
# curl -i "http://127.0.0.1/geoiptest?ip=192.10.224.5"
# curl -i "http://127.0.0.1/geoiptest?ip=192.210.224.5"
# blocking test, set worker_processes to 1 before the 'ab' test and disable cache
# ab -n 200 -c 100 -k -i "http://127.0.0.1:80/geoiptest?ip=192.210.224.5"
location /geoiptest {
content_by_lua '
local args = ngx.req.get_uri_args()
local reqip
function ip2long(curIP)
local _,_,a,b,c,d = string.find(curIP, "(%d+).(%d+).(%d+).(%d+)")
if (tonumber(a) and tonumber(b) and tonumber(c) and tonumber(d) ) then
return a*16777216 + b*65536 + c*256 + d
else
return 0
end
end
ngx.header.content_type = "text/plain"
if not args["ip"] then args["ip"] = "123.45.67.89" end
reqip = ip2long(args["ip"])
local Query = ngx.shared.SqlQueries
local QResult = Query:get(reqip) -- disable this line disables the cache
ngx.say("IP request "..args["ip"].." to GeoIP "..reqip.."\\n")
if not QResult then
local mysql = require "openresty.mysql"
local cjson = require "openresty.ljson"
local db, err = mysql:new()
if not db then
ngx.say("failed to instantiate mysql: ", err)
return
end
db:set_timeout(3000) -- 3 sec
local ok, err, errno, sqlstate = db:connect{
host = "127.0.0.1", port = 3306, database = "GeoIP",
user = "user", password = "password",
max_packet_size = 1024 * 1024 }
if not ok then
ngx.say("failed to connect: ", err, ": ", errno, " ", sqlstate)
return
end
-- restt, err, errno, sqlstate = db:query("SELECT sleep(1)", 10) -- Tested, non-blocking!
local res, err, errno, sqlstate = db:query("SELECT cn, cn FROM ip NATURAL JOIN cc WHERE "..reqip.." BETWEEN start AND end LIMIT 1", 10)
local row = cjson.encode(res)
if not row then row = "Unknown" end
ngx.say (row.." from database\\n")
Query:set(reqip, row) -- stick results in cache
else
ngx.say(QResult.." from cache\\n")
end
';
}
} # server block end
} # http end
Comments are welcome :)