openresty / lua-resty-mysql

Nonblocking Lua MySQL driver library for ngx_lua or OpenResty
708 stars 236 forks source link

when tested against concurrent requests more than 1000, tcp timeout errors occurred #93

Open tgxhenry opened 5 years ago

tgxhenry commented 5 years ago

When tested against concurrent requests more than 1000, tcp timeout errors occurred, detailed error message,lua tcp socket connect timed out, when connecting to 127.0.0.1:3306. Personally, I think the multiple connections to mysql and keep-alive setting lead to the problem. Any solution to fix the problem, like mysql connection pool ,or global mysql instance?

Below is part of the code used in the test. ################################################### res, err, errcode, sqlstate = db:query("select * from country limit 1") if not res then ngx.say("Bad result", err) return ngx.exit(500) end

local json = require "cjson" ngx.say(json.encode(res))

local ok, err = db:set_keepalive(60000, 100) if not ok then ngx.say("Failed to set keep alive") return end ###################################################

zhiyong0804 commented 5 years ago

when query failed, need to close db connection, so you need to do as below: res, err, errcode, sqlstate = db:query("select * from country limit 1") if not res then ngx.say("Bad result", err) db:close() return ngx.exit(500) end

in addtion, you can change mysql max connection to a bigger value, and you can enable resty.mysql backlog feature. about this feature pls ref to https://github.com/openresty/lua-nginx-module/blob/master/doc/HttpLuaModule.wiki