-
Notifications
You must be signed in to change notification settings - Fork 3
/
get_db4.lua
101 lines (88 loc) · 3.73 KB
/
get_db4.lua
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by Administrator.
--- DateTime: 2020/7/2 15:10
---
local mongol = require("resty.mongol")
local conf = require("myconfig") -- 配置项,正式环境
local DB_POOL = {
_VERSION = "0.1.0"
}
function DB_POOL.get_db_mongo_cli(host, port, timeout)
local host = host or conf.myconf_mongo_ip
local port = port or conf.myconf_mongo_port
local timeout = timeout or conf.myconf_mongo_timeout
if ngx.ctx.db_mongo_cli then -- 同一次HTTP请求,该值是一样的;不同的HTTP请求该值不一样。
return ngx.ctx.db_mongo_cli
end
local conn = mongol:new()
if not conn then
return nil, '### get_db_mongo_cli() MongoDB initialize failed. ###'
end
conn:set_timeout(timeout)
-- config the ngx.socket.tcp/cosocket built-in connection pool
-- https://github.com/openresty/lua-nginx-module#ngxsockettcp
local pool = conf.myconf_db_username ..":"..conf.myconf_db_database..":"..conf.myconf_mongo_ip..":"..conf.myconf_mongo_port
local pool_size = conf.myconf_mongo_poolsize
local backlog = 0
local pool_opts = {
pool = pool,
pool_size = pool_size,
-- backlog = backlog
}
local ok, err = conn:connect(host, port, pool_opts)
if not ok then
return nil, err
end
local times, err = conn:get_reused_times()
if 0 == times or nil == times then
db = conn:new_db_handle("admin")
if nil == db then
ngx.log(ngx.ERR, "get_db_mongo_cli() MongoDB new_db_handle failed 01.\n")
-- DB_POOL.close_db_mongo_cli()
conn:close() -- 关闭此socket连接
return nil, '### get_db_mongo_col() MongoDB new_db_handle failed 01. ###'
end
--用户授权
local ok, err = db:auth_scram_sha1(conf.myconf_db_username, conf.myconf_db_passwd)
if ok ~= 1 then
ngx.log(ngx.ERR, "get_db_mongo_cli() MongoDB user auth failed, err=", err, ".\n")
-- DB_POOL.close_db_mongo_cli()
conn:close() -- 关闭此socket连接
return nil, '### get_db_mongo_col() MongoDB auth_scram_sha1 failed: '..err.." ###"
else
ngx.log(ngx.ERR, "get_db_mongo_cli() user auth success, ok=", ok, "\n")
end
else
ngx.log(ngx.ERR, '$$$ get_db_mongo_col() reused: ', times)
end
ngx.ctx.db_mongo_cli = conn
return ngx.ctx.db_mongo_cli, nil
end
function DB_POOL.get_db_mongo_col(colname)
local conn, err = DB_POOL.get_db_mongo_cli()
if not conn or err then
return nil, '### get_db_mongo_col() MongoDB connect failed: '..err..' ###';
end
-- 切换数据库
local db = conn:new_db_handle(conf.myconf_db_database)
if nil == db then
ngx.log(ngx.ERR, "get_db_mongo_col() swith to ", conf.myconf_das_database, " failed.\n")
-- DB_POOL.close_db_mongo_cli() -- 会导致泄露
conn:close() -- 关闭此socket连接
return nil, '### get_db_mongo_col() MongoDB new_db_handle failed 01. ###'
end
local colname = colname
return db:get_col(colname), nil
end
function DB_POOL.close_db_mongo_cli(keepalive_time)
local keepaliveTime = keepalive_time or conf.myconf_mongo_keepalive
local pool_size = conf.myconf_mongo_poolsize
if ngx.ctx.db_mongo_cli then
ngx.log(ngx.ERR, "close_db_mongo_cli() set_keepalive keepalive_time=", keepaliveTime, ".\n")
-- no need to manually call the close method on it afterwards.
ngx.ctx.db_mongo_cli:set_keepalive(keepaliveTime, pool_size) -- 放入连接池, keepaliveTime=0(长连接)
ngx.ctx.db_mongo_cli = nil
end
end
return DB_POOL