未验证 提交 41e29f41 编写于 作者: R robotspace 提交者: GitHub

Infactor Lua connection pool implementation. (#20720)

* Do not retrieve err msg when connection is established successfully to avoid exception.

* Restore check script for lua installation.

* Infactor connection pool implementation.
上级 b54ff078
local _M = {} local _M = {}
local driver = require "luaconnector51" local driver = require "luaconnector51"
local water_mark = 0 td_pool_watermark = 0
local occupied = 0 td_pool_occupied = 0
local connection_pool = {} td_connection_pool = {}
function _M.new(o,config) function _M.new(o, config)
o = o or {} o = o or {}
o.connection_pool = connection_pool o.connection_pool = td_connection_pool
o.water_mark = water_mark o.watermark = td_pool_watermark
o.occupied = occupied o.occupied = td_pool_occupied
if #connection_pool == 0 then if #td_connection_pool == 0 then
for i = 1, config.connection_pool_size do for i = 1, config.connection_pool_size do
local res = driver.connect(config) local res = driver.connect(config)
if res.code ~= 0 then if res.code ~= 0 then
...@@ -18,8 +17,8 @@ function _M.new(o,config) ...@@ -18,8 +17,8 @@ function _M.new(o,config)
return nil return nil
else else
local object = {obj = res.conn, state = 0} local object = {obj = res.conn, state = 0}
table.insert(o.connection_pool,i, object) table.insert(td_connection_pool, i, object)
ngx.log(ngx.INFO, "add connection, now pool size:"..#(o.connection_pool)) ngx.log(ngx.INFO, "add connection, now pool size:"..#(td_connection_pool))
end end
end end
...@@ -32,13 +31,13 @@ function _M:get_connection() ...@@ -32,13 +31,13 @@ function _M:get_connection()
local connection_obj local connection_obj
for i = 1, #connection_pool do for i = 1, #td_connection_pool do
connection_obj = connection_pool[i] connection_obj = td_connection_pool[i]
if connection_obj.state == 0 then if connection_obj.state == 0 then
connection_obj.state = 1 connection_obj.state = 1
occupied = occupied +1 td_pool_occupied = td_pool_occupied + 1
if occupied > water_mark then if td_pool_occupied > td_pool_watermark then
water_mark = occupied td_pool_watermark = td_pool_occupied
end end
return connection_obj["obj"] return connection_obj["obj"]
end end
...@@ -49,21 +48,27 @@ function _M:get_connection() ...@@ -49,21 +48,27 @@ function _M:get_connection()
return nil return nil
end end
function _M:get_water_mark() function _M:get_watermark()
return td_pool_watermark
end
function _M:get_current_load()
return water_mark return td_pool_occupied
end end
function _M:release_connection(conn) function _M:release_connection(conn)
local connection_obj local connection_obj
for i = 1, #connection_pool do for i = 1, #td_connection_pool do
connection_obj = connection_pool[i] connection_obj = td_connection_pool[i]
if connection_obj["obj"] == conn then if connection_obj["obj"] == conn then
connection_obj["state"] = 0 connection_obj["state"] = 0
occupied = occupied -1 td_pool_occupied = td_pool_occupied -1
return return
end end
end end
......
...@@ -4,8 +4,21 @@ local Pool = require "tdpool" ...@@ -4,8 +4,21 @@ local Pool = require "tdpool"
local config = require "config" local config = require "config"
ngx.say("start time:"..os.time()) ngx.say("start time:"..os.time())
local pool = Pool.new(Pool,config) local pool = Pool.new(Pool, config)
local conn = pool:get_connection() local another_pool = Pool.new(Pool, config)
local conn, conn1, conn2
conn = pool:get_connection()
conn1 = pool:get_connection()
conn2 = pool:get_connection()
local temp_conn = another_pool:get_connection()
ngx.say("pool size:"..config.connection_pool_size)
ngx.say("pool watermark:"..pool:get_watermark())
ngx.say("pool current load:"..pool:get_current_load())
pool:release_connection(conn1)
pool:release_connection(conn2)
another_pool:release_connection(temp_conn)
ngx.say("pool watermark:"..pool:get_watermark())
ngx.say("pool current load:"..pool:get_current_load())
local res = driver.query(conn,"drop database if exists nginx") local res = driver.query(conn,"drop database if exists nginx")
if res.code ~=0 then if res.code ~=0 then
...@@ -31,7 +44,6 @@ end ...@@ -31,7 +44,6 @@ end
res = driver.query(conn,"create table m1 (ts timestamp, speed int,owner binary(20))") res = driver.query(conn,"create table m1 (ts timestamp, speed int,owner binary(20))")
if res.code ~=0 then if res.code ~=0 then
ngx.say("create table---failed: "..res.error) ngx.say("create table---failed: "..res.error)
else else
ngx.say("create table--- pass.") ngx.say("create table--- pass.")
end end
...@@ -83,8 +95,5 @@ while not flag do ...@@ -83,8 +95,5 @@ while not flag do
-- ngx.say("i am here once...") -- ngx.say("i am here once...")
ngx.sleep(0.001) -- time unit is second ngx.sleep(0.001) -- time unit is second
end end
ngx.say("pool water_mark:"..pool:get_water_mark())
pool:release_connection(conn) pool:release_connection(conn)
ngx.say("end time:"..os.time()) ngx.say("end time:"..os.time())
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册