Hello!
lua-resty-redis 库基于 cosocket API,而 cosocket 会 yield 当前的 coroutine
的,而你这里是在你自己的 lua_CFunction 内部调用 resty.redis 的会 yield 的操作,而这个上下文是不允许
yield 的。所以自然是不行的。你应该会触发下面这个 LuaJIT VM 的异常:
attempt to yield across C-call boundary
这是新手常犯的错误。
Regards,
-agentzh
On Mon, Aug 14, 2017 at 10:41 PM, bin...@126.com <bin...@126.com> wrote:
> 亦春大神:
> 你好!
> 我在使用ngx_lua-0.10.9rc5 这个版本的http-lua模块。nginx版本:1.10.2
>
> 我代码是这样写的
>
> --lua code:
>
> local redis = require "resty.redis"
>
> local function mycallback()
> local rdb = redis:new()
> rdb:set_timeout(2000)
> local ok, err = rdb:connect("127.0.0.1", 6379)
> if not ok or err then
> ngx.log(ngx.ERR, "failed to connect: ", err)
> return ad
> end
>
> local ok, err = rdb:get(name) -- 这个不会返回!
> -- ... ...
> return 'ok'
> end
>
> local ret = myfunc(data, mycallback)
>
>
>
> /*
> c code:
> */
> static int ngx_http_lua_create_module(lua_State * L)
> {
> lua_createtable(L, 0, 8);
> lua_pushcfunction(L, ngx_http_lua_myfunc);
> lua_setfield(L, -2, "myfunc");
> // ... ...
> return 1;
> }
>
>
> int ngx_http_lua_myfunc(lua_State * L)
> {
> // ... ...
> lua_pushvalue(L, 1);
>
> if (0 != lua_pcall(L, 0, 1, 0))
> {
> if (lua_isstring(L, -1))
> printf("lua_pcall error: %s\n", lua_tostring(L, -1));
> else
> printf("lua_pcall error, ret type: %s\n", lua_typename(L, -1));
>
> lua_pop(L, 1);
> return 0;
> }
>
> if (!lua_isstring(L, -1)) {
> printf("lua_isstring() ret type: %s\n", lua_typename(L, -1));
> lua_pop(L, 1);
> return 0;
> }
>
> out = (char *)luaL_checklstring(L, -1, &outlen);
> if (!outlen || outlen < 1) {
> lua_pop(L, 1);
> return 0;
> }
> // ... ...
> }
>
>
> 最后发现在文件 resty/redis.lua中的 函数_read_reply 中 sock:receive() 没有返回
> C语言部分我还没有跟,刚刚发现这个问题。
>
> local function _read_reply(self, sock)
> local line, err = sock:receive()
> ngx.log(ngx.ERR, "line:", line)
> if not line then
> if err == "timeout" and not rawget(self, "_subscribed") then
> sock:close()
> end
> return nil, err
> end
> ... ...
>
>
> tcpdump抓包,发现没有任何问题。
> redis数据库正常返回的报文的,并且正常结束。
>
>
>
> 有什么好的建议么? 谢谢!
>
> ________________________________
> 中国上海
> 顾振洲
>
>
> --
> --
> 邮件来自列表“openresty”,专用于技术讨论!
> 订阅: 请发空白邮件到 openresty+subscribe@googlegroups.com
> 发言: 请发邮件到 openresty@googlegroups.com
> 退订: 请发邮件至 openresty+unsubscribe@googlegroups.com
> 归档: http://groups.google.com/group/openresty
> 官网: http://openresty.org/
> 仓库: https://github.com/agentzh/ngx_openresty
> 教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html
--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty+subscribe@googlegroups.com
发言: 请发邮件到 openresty@googlegroups.com
退订: 请发邮件至 openresty+unsubscribe@googlegroups.com
归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html