From 22df64244a73312daa8c75b239230efe54a4e54b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=8E=E4=BB=94?= <41766775+huahua132@users.noreply.github.com> Date: Wed, 10 Apr 2024 13:40:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96http=E5=87=BA=E9=94=99?= =?UTF-8?q?=E6=8A=9B=E5=87=BA=E6=9B=B4=E8=AF=A6=E7=BB=86=E7=9A=84=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E4=BF=A1=E6=81=AF=20(#1900)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 优化http出错抛出更详细的错误信息 * 优化http err_info写法 --- lualib/http/httpc.lua | 4 ++++ lualib/http/httpd.lua | 5 +++++ lualib/http/internal.lua | 6 ++++++ lualib/http/sockethelper.lua | 35 +++++++++++++++++++++++++++-------- 4 files changed, 42 insertions(+), 8 deletions(-) diff --git a/lualib/http/httpc.lua b/lualib/http/httpc.lua index bc18dad0..54595648 100644 --- a/lualib/http/httpc.lua +++ b/lualib/http/httpc.lua @@ -2,8 +2,12 @@ local skynet = require "skynet" local socket = require "http.sockethelper" local internal = require "http.internal" local dns = require "skynet.dns" + local string = string local table = table +local pcall = pcall +local error = error +local pairs = pairs local httpc = {} diff --git a/lualib/http/httpd.lua b/lualib/http/httpd.lua index 1575f5a6..fb3fb2e8 100644 --- a/lualib/http/httpd.lua +++ b/lualib/http/httpd.lua @@ -2,6 +2,11 @@ local internal = require "http.internal" local string = string local type = type +local assert = assert +local tonumber = tonumber +local pcall = pcall +local ipairs = ipairs +local pairs = pairs local httpd = {} diff --git a/lualib/http/internal.lua b/lualib/http/internal.lua index 204767be..a34b7df6 100644 --- a/lualib/http/internal.lua +++ b/lualib/http/internal.lua @@ -1,5 +1,11 @@ local table = table local type = type +local string = string +local tonumber = tonumber +local pcall = pcall +local assert = assert +local error = error +local pairs = pairs local M = {} diff --git a/lualib/http/sockethelper.lua b/lualib/http/sockethelper.lua index 10b32346..9f0a6d03 100644 --- a/lualib/http/sockethelper.lua +++ b/lualib/http/sockethelper.lua @@ -1,11 +1,26 @@ local socket = require "skynet.socket" local skynet = require "skynet" +local coroutine = coroutine +local error = error +local tostring = tostring + local readbytes = socket.read local writebytes = socket.write local sockethelper = {} -local socket_error = setmetatable({} , { __tostring = function() return "[Socket Error]" end }) +local socket_error = setmetatable({} , { + __tostring = function(self) + local info = self.err_info + self.err_info = nil + return info or "[Socket Error]" + end, + + __call = function (self, info) + self.err_info = "[Socket Error] : " .. tostring(info) + return self + end +}) sockethelper.socket_error = socket_error @@ -27,7 +42,7 @@ local function preread(fd, str) if ret then return str .. ret else - error(socket_error) + error(socket_error("read failed fd = " .. fd)) end end end @@ -36,7 +51,7 @@ local function preread(fd, str) if ret then return ret else - error(socket_error) + error(socket_error("read failed fd = " .. fd)) end end end @@ -51,7 +66,7 @@ function sockethelper.readfunc(fd, pre) if ret then return ret else - error(socket_error) + error(socket_error("read failed fd = " .. fd)) end end end @@ -62,24 +77,27 @@ function sockethelper.writefunc(fd) return function(content) local ok = writebytes(fd, content) if not ok then - error(socket_error) + error(socket_error("write failed fd = " .. fd)) end end end function sockethelper.connect(host, port, timeout) - local fd + local fd, err + local is_time_out = false if timeout then + is_time_out = true local drop_fd local co = coroutine.running() -- asynchronous connect skynet.fork(function() - fd = socket.open(host, port) + fd, err = socket.open(host, port) if drop_fd then -- sockethelper.connect already return, and raise socket_error socket.close(fd) else -- socket.open before sleep, wakeup. + is_time_out = false skynet.wakeup(co) end end) @@ -89,13 +107,14 @@ function sockethelper.connect(host, port, timeout) drop_fd = true end else + is_time_out = false -- block connect fd = socket.open(host, port) end if fd then return fd end - error(socket_error) + error(socket_error("connect failed host = " .. host .. ' port = '.. port .. ' timeout = ' .. timeout .. ' err = ' .. tostring(err) .. ' is_time_out = '.. tostring(is_time_out))) end function sockethelper.close(fd)