init.lua 1.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
local _M = {}
local driver = require "luaconnector51"
local water_mark = 0
local occupied = 0
local connection_pool = {}

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

      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))
	 end
      end
      
   end
   
   return setmetatable(o, { __index = _M })
end

function _M:get_connection()

   local connection_obj

   for i = 1, #connection_pool do
      connection_obj = connection_pool[i]
      if connection_obj.state == 0 then
	 connection_obj.state = 1
	 occupied = occupied +1
	 if occupied > water_mark then
	    water_mark = occupied
	 end
	 return connection_obj["obj"]	 
      end
   end
   
   ngx.log(ngx.ERR,"ALERT! NO FREE CONNECTION.")

   return nil
end

function _M:get_water_mark()

   return water_mark
end

function _M:release_connection(conn)
 
   local connection_obj

   for i = 1, #connection_pool do
      connection_obj = connection_pool[i]
 
      if connection_obj["obj"] == conn then
	 connection_obj["state"] = 0
	 occupied = occupied -1
	 return
      end
   end
end

return _M