Skip to content

Commit

Permalink
Infactor Lua connection pool implementation. (#20720)
Browse files Browse the repository at this point in the history
* Do not retrieve err msg when connection is established successfully to avoid exception.

* Restore check script for lua installation.

* Infactor connection pool implementation.
  • Loading branch information
robotspace authored and 54liuyao committed Apr 21, 2023
1 parent 6f8c362 commit 7b04146
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 27 deletions.
47 changes: 26 additions & 21 deletions examples/lua/OpenResty/rest/tdpool/init.lua
@@ -1,25 +1,24 @@
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
ngx.log(ngx.ERR, "connect--- failed:"..res.error)
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

Expand All @@ -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
Expand All @@ -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
Expand Down
21 changes: 15 additions & 6 deletions examples/lua/OpenResty/rest/test.lua
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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())

0 comments on commit 7b04146

Please sign in to comment.