使用lua的ngx.socket.tcp发送原始http请求blocked一秒(keepalive)

822 views
Skip to first unread message

haoyun

unread,
Feb 1, 2016, 9:33:53 PM2/1/16
to open...@googlegroups.com
请大家帮忙看下!
我的环境如下:
centos 6.5 x64
openresty-1.4.3.6
我想在nginx的lua中使用ngx.socket.tcp发出原始http请求,配置如下:
    location /time {
        content_by_lua_file conf/http.lua;
    }
http.lua内容如下:
local s = ngx.socket.tcp()
local host = "142.162.233.150"
local port = 80
local ok, err = s:connect(host,port)
if not ok then
ngx.say("failed to connect: " .. err)
return
end
local data = "GET /time HTTP/1.1\r\n"
.. "Host: domain.cn\r\n"
.. "Connection: close\r\n"
.. "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\n"
.. "User-Agent: Nginx-lua\r\n"
.. "Accept-Language: zh-CN,zh;q=0.8,en;q=0.6\r\n\r\n"
local bytes, err = s:send(data)
ngx.say(bytes)
if not bytes then
ngx.say("failed to send query")
return
end
local read_headers = s:receiveuntil("\r\n\r\n")
local headers, err = read_headers()
if not read_headers then
ngx.say("failed to read response headers:", err)
return
end
ngx.say(headers)
local body,err = s:receive("*a")
if not body then
ngx.say("failed to read response body", err)
return
end

ngx.say(body)
local ok, err = s:setkeepalive(60000,8)
if not ok then
ngx.say("failed to put the connection into pool "
.. "with pool capacity 8 "
.. "and maximal idle time 60 sec"
)
return
end
这段代码可以发送并接收成功,速度也快
[root@bogon ~]# time curl 10.0.10.17/time
206
HTTP/1.1 200 OK
Server: ngx_openresty
Date: Tue, 02 Feb 2016 02:21:16 GMT
Content-Type: application/octet-stream
Content-Length: 14
Connection: close
1454379676.240
failed to put the connection into pool with pool capacity 8 and maximal idle time 60 sec

real 0m0.025s
user 0m0.006s
sys 0m0.002s
但是将Connection设置成keep-alive时,就会等待1分钟才显示结果,这是怎么回事呢?
[root@bogon ~]# time curl 10.0.10.17/time
211
HTTP/1.1 200 OK
Server: ngx_openresty
Date: Tue, 02 Feb 2016 02:12:22 GMT
Content-Type: application/octet-stream
Content-Length: 14
Connection: keep-alive
1454379142.900

real 1m0.025s
user 0m0.005s
sys 0m0.006s
请求帮助~
先谢谢各位了


 

YuanSheng Wang

unread,
Feb 1, 2016, 10:52:48 PM2/1/16
to open...@googlegroups.com
local body,err = s:receive("*a")

  • '*a':从 socket 中读取内容直到连接被关闭。这里是不执行 end-of-line 翻译。
你这里是 http 请求,建议使用 https://github.com/pintsized/lua-resty-http 完成请求,要更加简洁通用。




 

--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+...@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



--

YuanSheng Wang
---------------------------------------
OpenResty lover ^_^

haoyun

unread,
Feb 2, 2016, 12:21:45 AM2/2/16
to open...@googlegroups.com
我的妈呀,春哥帮助,什么问题都能迎刃而解~
太感动了~
感谢春哥!
订阅: 请发空白邮件到 openresty+subs...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+unsub...@googlegroups.com

Jian Wen

unread,
Feb 2, 2016, 12:24:26 AM2/2/16
to open...@googlegroups.com
你再看仔细点。
Best,

Jian

haoyun

unread,
Feb 2, 2016, 1:03:56 AM2/2/16
to open...@googlegroups.com
还有一个问题,就是我想将连接放入连接池,用我的方法也可以,只是需要判断一下body长度,
在带有content-length的情况下好办,但是在chunk的情况下怎么处理呢?





在 2016-02-02 11:52:44,"YuanSheng Wang" <yuan...@openresty.org> 写道:
订阅: 请发空白邮件到 openresty+subs...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+unsub...@googlegroups.com

haoyun

unread,
Feb 2, 2016, 1:14:12 AM2/2/16
to open...@googlegroups.com
我去。。。
我这是啥眼神儿啊?名字都给看错了,灰常抱歉!
我这。。,一般都是春哥回复我的多,就直接。。。
非常非常感谢“YuanSheng Wang”





在 2016-02-02 11:52:44,"YuanSheng Wang" <yuan...@openresty.org> 写道:
订阅: 请发空白邮件到 openresty+subs...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+unsub...@googlegroups.com

YuanSheng Wang

unread,
Feb 2, 2016, 2:03:09 AM2/2/16
to open...@googlegroups.com
在带有content-length的情况下好办,但是在chunk的情况下怎么处理呢?
你这里是 http 请求,建议使用 https://github.com/pintsized/lua-resty-http 完成请求,要更加简洁通用。

上面的库,有现成的读取办法,看看官方示例,就知道了。

haoyun

unread,
Feb 2, 2016, 2:32:00 AM2/2/16
to open...@googlegroups.com
好的,非常感谢哈~
Reply all
Reply to author
Forward
0 new messages