mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-25 04:33:05 +00:00
connect timeout, see #611
This commit is contained in:
@@ -99,7 +99,7 @@ function httpc.request(method, host, url, recvheader, header, content)
|
|||||||
if async_dns and not hostname:match(".*%d+$") then
|
if async_dns and not hostname:match(".*%d+$") then
|
||||||
hostname = dns.resolve(hostname)
|
hostname = dns.resolve(hostname)
|
||||||
end
|
end
|
||||||
local fd = socket.connect(hostname, port)
|
local fd = socket.connect(hostname, port, timeout)
|
||||||
local finish
|
local finish
|
||||||
if timeout then
|
if timeout then
|
||||||
skynet.timeout(timeout, function()
|
skynet.timeout(timeout, function()
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
local socket = require "socket"
|
local socket = require "socket"
|
||||||
|
local skynet = require "skynet"
|
||||||
|
|
||||||
local readbytes = socket.read
|
local readbytes = socket.read
|
||||||
local writebytes = socket.write
|
local writebytes = socket.write
|
||||||
@@ -66,8 +67,27 @@ function sockethelper.writefunc(fd)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function sockethelper.connect(host, port)
|
function sockethelper.connect(host, port, timeout)
|
||||||
local fd = socket.open(host, port)
|
local fd
|
||||||
|
if timeout then
|
||||||
|
local drop_fd
|
||||||
|
-- asynchronous connect
|
||||||
|
skynet.fork(function()
|
||||||
|
fd = socket.open(host, port)
|
||||||
|
if drop_fd then
|
||||||
|
-- sockethelper.connect already return, and raise socket_error
|
||||||
|
socket.close(fd)
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
skynet.sleep(timeout)
|
||||||
|
if not fd then
|
||||||
|
-- not connect yet
|
||||||
|
drop_fd = true
|
||||||
|
end
|
||||||
|
else
|
||||||
|
-- block connect
|
||||||
|
fd = socket.open(host, port)
|
||||||
|
end
|
||||||
if fd then
|
if fd then
|
||||||
return fd
|
return fd
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ local skynet = require "skynet"
|
|||||||
local httpc = require "http.httpc"
|
local httpc = require "http.httpc"
|
||||||
local dns = require "dns"
|
local dns = require "dns"
|
||||||
|
|
||||||
skynet.start(function()
|
local function main()
|
||||||
httpc.dns() -- set dns server
|
httpc.dns() -- set dns server
|
||||||
httpc.timeout = 100 -- set timeout 1 second
|
httpc.timeout = 100 -- set timeout 1 second
|
||||||
print("GET baidu.com")
|
print("GET baidu.com")
|
||||||
@@ -21,6 +21,9 @@ skynet.start(function()
|
|||||||
print(string.format("GET %s (baidu.com)", ip))
|
print(string.format("GET %s (baidu.com)", ip))
|
||||||
local status, body = httpc.get("baidu.com", "/", respheader, { host = "baidu.com" })
|
local status, body = httpc.get("baidu.com", "/", respheader, { host = "baidu.com" })
|
||||||
print(status)
|
print(status)
|
||||||
|
end
|
||||||
|
|
||||||
|
skynet.start(function()
|
||||||
|
print(pcall(main))
|
||||||
skynet.exit()
|
skynet.exit()
|
||||||
end)
|
end)
|
||||||
|
|||||||
Reference in New Issue
Block a user