Skip to content

Commit 08f5347

Browse files
committed
feature: used lua-resty-socket when the phase is not yieldable
1 parent 3116fc6 commit 08f5347

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

lib/resty/requests/adapter.lua

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
-- Copyright (C) Alex Zhang
22

33
local util = require "resty.requests.util"
4+
local resty_socket = require "resty.socket"
45
local response = require "resty.requests.response"
56
local check_http2, http2 = pcall(require, "resty.http2")
67

@@ -11,9 +12,10 @@ local lower = string.lower
1112
local format = string.format
1213
local insert = table.insert
1314
local concat = table.concat
14-
local socket = ngx.socket.tcp
15+
local tcp_socket = ngx.socket.tcp
1516
local ngx_match = ngx.re.match
1617
local ngx_now = ngx.now
18+
local get_phase = ngx.get_phase
1719
local dict = util.dict
1820
local new_tab = util.new_tab
1921
local is_tab = util.is_tab
@@ -43,6 +45,21 @@ if check_http2 then
4345
end
4446

4547

48+
local function socket()
49+
local phase = get_phase()
50+
51+
-- ignore the other non-yiedable phases, since these phases are
52+
-- requests-specific and we shouldn't use the blocking APIs, it will hurt
53+
-- the event loop, so just let the Cosocket throws "API disabled ..."
54+
-- error.
55+
if phase == "init" or phase == "init_worker" then
56+
return resty_socket()
57+
end
58+
59+
return tcp_socket()
60+
end
61+
62+
4663
local function parse_status_line(status_line)
4764
local m, err = ngx_match(status_line, "HTTP/(.+?)\\s.*?(\\d+).*", "jo")
4865
if not m then

lua-resty-requests-0.7-1.rockspec

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package = "lua-resty-requests"
2-
version = "0.7-1"
2+
version = "0.7.1-1"
3+
34
source = {
45
url = "git://github.com/tokers/lua-resty-requests",
56
tag = "v0.7",
67
}
8+
79
description = {
810
summary = "Yet Another HTTP library for OpenResty",
911
detailed = [[
@@ -13,9 +15,12 @@ description = {
1315
homepage = "https://github.com/tokers/lua-resty-requests",
1416
maintainer = "Alex Zhang <[email protected]>",
1517
}
18+
1619
dependencies = {
17-
"lua >= 5.1"
20+
"lua >= 5.1",
21+
"lua-resty-socket == 0.0.4",
1822
}
23+
1924
build = {
2025
type = "builtin",
2126
modules = {

0 commit comments

Comments
 (0)