lua-resty-couchbase - Lua couchbase client driver for the ngx_lua based on the cosocket API.
It has been running stably on IQIYI Play Service for more than 5 years, and the online support QPS reaches 20W. Under the OpenResty architecture, CouchBase can be accessed directly from nginx.
使用cosocket纯lua实现的couchbase的client,已经在爱奇艺重要的播放服务稳定运行5年多,线上支持峰值QPS达到20W,在OpenResty架构下面,可以直接从nginx上面访问CouchBase.
This library is considered production ready.
This Lua library is a CouchBase client driver for the ngx_lua nginx module:
https://github.com/openresty/lua-nginx-module/#readme
This Lua library takes advantage of ngx_lua's cosocket API, which ensures 100% nonblocking behavior.
Note that at least ngx_lua 0.5.14 or OpenResty 1.2.1.14 is required.
#opm
opm install iqiyi/lua-resty-couchbase
# luarocks
luarocks install --local lua-resty-couchbase
lua_package_path "/path/to/lua-resty-couchbase/lib/?.lua;;";
lua_shared_dict ldict 10m;
server {
location /test {
content_by_lua_block {
local cjson = require "cjson"
local couchbase = require "resty.couchbase"
local function get_from_service()
-- nothing
return "{}"
end
local conf = {
hosts = { "10.10.10.1:8091", "10.10.10.2:8091"},
bucket_name = "test",
bucketpwd = "test-password",
}
local client, err = couchbase:create_client(conf.hosts, conf.bucket_name, conf.bucketpwd)
if client == nil then
ngx.log(ngx.ERR, err)
end
-- test set_timeout
client:set_timeout(500)
local key = "test-key"
local key1 = "test-key1"
-- test set
client:set(key, "{}")
client:set(key1, "{}")
-- test get_bluk
local values, bluk_err = client:get_bluk(key, key1)
if not bluk_err then
ngx.say(cjson.encode(values))
end
-- test n1ql
local result, query_err = client:query('SELECT country FROM `travel-sample` WHERE name = "Excel Airways";')
if not query_err then
ngx.say(result)
end
-- test get get_from_replica
local value, get_err = client:get(key)
if value then
ngx.say(value)
else
if get_err then
if string.find(get_err, "Not found") then
ngx.log(ngx.INFO, "key not found: ", key, " error: ", get_err)
ngx.say(get_from_service())
else
local value_bak, err_bak = client:get_from_replica(key)
if value_bak then
ngx.log(ngx.WARN, "get key from replica success: ", key)
ngx.say(value_bak)
else
ngx.log(ngx.ERR, "get replica error: ", key, "error: ", err_bak)
ngx.say(get_from_service())
end
end
end
end
-- test close
client:close()
}
}
}
It is usually convenient to use the lua-cjson library to encode the return values of the couchbase command methods to JSON. For example,
local cjson = require "cjson"
...
local res, err = client:get("h1234")
if res then
print("res: ", cjson.encode(res))
end
By default the underlying ngx_lua module does error logging when socket errors happen. If you are already doing proper error handling in your own Lua code, then you are recommended to disable this automatic error logging by turning off ngx_lua's lua_socket_log_errors directive, that is,
lua_socket_log_errors off;
n
concurrent requests and your NGINX has m
workers, then the connection pool size should be configured as n/m
. For example, if your NGINX usually handles 1000 concurrent requests and you have 10 NGINX workers, then the connection pool size should be 100.Please report bugs or submit patches by
goecho gaofeng@qiyi.com
This module is licensed under the BSD license.
Copyright (C) 2015-2020, by goecho gaofeng@qiyi.com, iQIYI Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。