为了方便调用db操作,将db查询封装成一个模块了,模块内容如下。封装后用ab进行测试,加了-k的参数,测试结果吞吐量比不封装模块下降了十几倍。请问这样封装有什么问题,求大神解答local mysql_pool = {}--建立连接.function mysql_pool:get_connect() local mysql = require "resty.mysql" local client, errmsg = mysql.new() if not client then return false, "mysql.socket_failed: " .. (errmsg or "nil") end --设置超时时间 client:set_timeout(1000) local props = { host = "192.168.1.111", port = 3306, database = "visitor", user = "username", password = "password", max_packet_size = 1024 * 1024 } local res, err, errno, sqlstate = client:connect(props) if not res then return false, "mysql.query_failed: "..(errmsg or "nil")..", errno:"..(errno or "nil")..", sql_state:"..(sqlstate or "nil") end local query = "SET NAMES UTF8" local result, errmsg, errno, sqlstate = client:query(query) if not result then return false, "mysql.query_failed: "..(errmsg or "nil")..", errno:"..(errno or "nil")..", sql_state:"..(sqlstate or "nil") end return true, clientend--把连接返回到连接池--用set_keepalive代替close() 将开启连接池特性 (1分钟timout,连接池100个)function mysql_pool:close( client ) if client then local ok, err = client:set_keepalive(60000, 100) if not ok then ngx.say("failed to set keepalive:", err) end endendfunction mysql_pool:query(sql, flag) local ret, client = self:get_connect(flag) if not ret then return false, client, nil end local result, errmsg, errno, sqlstate = client:query(sql) self:close(client) if not result then errmsg = concat_db_errmsg("mysql.query_failed:", errno, errmsg, sqlstate) return false, errmsg, sqlstate end return true, result, sqlstateendreturn mysql_pool --