tcp连接的setkeepalive方法返回error

27 views
Skip to first unread message

hf d

unread,
Jun 20, 2020, 11:58:20 PM6/20/20
to openresty

我直接用https://github.com/chronolaw/openresty_dev这个仓库的代码测试keepalive,运行curl 127.1:81/cosocket -d "12|34|"命令,得到的输出是:

12,
34,
failed to read data: closed
sock usedtimes = 0
receive from backend. len: 16 data: hellohellohello

usedtimes的值始终是0,setkeepalive方法执行失败,我把err打印出来,显示的是invalid connection,我在ngx_stream_lua_socket_tcp.c这个文件里看到这个错误信息的判断如下:

if (c->read->eof
        || c->read->error
        || c->read->timedout
        || c->write->error
        || c->write->timedout)
    {
        lua_pushnil(L);
        lua_pushliteral(L, "invalid connection");
        return 2;
    }

为了知道是哪个条件导致的失败,我这样修改源码:

if (c->read->eof)
    {
        lua_pushnil(L);
        lua_pushliteral(L, "read eof invalid connection");
        return 2;
    }

    if (c->read->error)
    {
        lua_pushnil(L);
        lua_pushliteral(L, "read error invalid connection");
        return 2;
    }

    if (c->read->timedout)
    {
        lua_pushnil(L);
        lua_pushliteral(L, "read timeout invalid connection");
        return 2;
    }

    if (c->write->error)
    {
        lua_pushnil(L);
        lua_pushliteral(L, "write error invalid connection");
        return 2;
    }

    if (c->write->timedout)
    {
        lua_pushnil(L);
        lua_pushliteral(L, "write timeout invalid connection");
        return 2;
    }

最后编译重现问题,日志显示错误信息为:read eof invalid connection
请问这个问题如何避免,是缺少什么配置吗。

Reply all
Reply to author
Forward
0 new messages