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