mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 12:20:41 +00:00
use async dns resolve in httpc, and dns resolve cache by ttl. see about issue #253
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
local socket = require "http.sockethelper"
|
||||
local url = require "http.url"
|
||||
local internal = require "http.internal"
|
||||
local dns = require "dns"
|
||||
local string = string
|
||||
local table = table
|
||||
|
||||
@@ -78,6 +79,13 @@ local function request(fd, method, host, url, recvheader, header, content)
|
||||
return code, body
|
||||
end
|
||||
|
||||
local async_dns
|
||||
|
||||
function httpc.dns(server,port)
|
||||
async_dns = true
|
||||
dns.server(server,port)
|
||||
end
|
||||
|
||||
function httpc.request(method, host, url, recvheader, header, content)
|
||||
local hostname, port = host:match"([^:]+):?(%d*)$"
|
||||
if port == "" then
|
||||
@@ -85,6 +93,9 @@ function httpc.request(method, host, url, recvheader, header, content)
|
||||
else
|
||||
port = tonumber(port)
|
||||
end
|
||||
if async_dns and not hostname:match(".*%d+$") then
|
||||
hostname = dns.resolve(hostname)
|
||||
end
|
||||
local fd = socket.connect(hostname, port)
|
||||
local ok , statuscode, body = pcall(request, fd,method, host, url, recvheader, header, content)
|
||||
socket.close(fd)
|
||||
|
||||
Reference in New Issue
Block a user