11-- Copyright (C) Alex Zhang
22
33local util = require " resty.requests.util"
4+ local resty_socket = require " resty.socket"
45local response = require " resty.requests.response"
56local check_http2 , http2 = pcall (require , " resty.http2" )
67
@@ -11,9 +12,10 @@ local lower = string.lower
1112local format = string.format
1213local insert = table.insert
1314local concat = table.concat
14- local socket = ngx .socket .tcp
15+ local tcp_socket = ngx .socket .tcp
1516local ngx_match = ngx .re .match
1617local ngx_now = ngx .now
18+ local get_phase = ngx .get_phase
1719local dict = util .dict
1820local new_tab = util .new_tab
1921local is_tab = util .is_tab
@@ -43,6 +45,21 @@ if check_http2 then
4345end
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+
4663local function parse_status_line (status_line )
4764 local m , err = ngx_match (status_line , " HTTP/(.+?)\\ s.*?(\\ d+).*" , " jo" )
4865 if not m then
0 commit comments