Skip to content

Commit

Permalink
feature(core): implement core.sleep (#2397)
Browse files Browse the repository at this point in the history
  • Loading branch information
dabue committed Oct 19, 2020
1 parent 1fe4e50 commit 943be09
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 9 deletions.
5 changes: 4 additions & 1 deletion apisix/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
-- limitations under the License.
--
local log = require("apisix.core.log")
local utils = require("apisix.core.utils")
local local_conf, err = require("apisix.core.config_local").local_conf()
if not local_conf then
error("failed to parse yaml config: " .. err)
Expand All @@ -26,10 +27,12 @@ log.info("use config_center: ", config_center)
local config = require("apisix.core.config_" .. config_center)
config.type = config_center


return {
version = require("apisix.core.version"),
log = log,
config = config,
sleep = utils.sleep,
json = require("apisix.core.json"),
table = require("apisix.core.table"),
request = require("apisix.core.request"),
Expand All @@ -40,7 +43,7 @@ return {
ctx = require("apisix.core.ctx"),
timer = require("apisix.core.timer"),
id = require("apisix.core.id"),
utils = require("apisix.core.utils"),
utils = utils,
etcd = require("apisix.core.etcd"),
http = require("apisix.core.http"),
tablepool= require("tablepool"),
Expand Down
2 changes: 1 addition & 1 deletion apisix/core/config_etcd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ local insert_tab = table.insert
local type = type
local ipairs = ipairs
local setmetatable = setmetatable
local ngx_sleep = ngx.sleep
local ngx_sleep = require("apisix.core.utils").sleep
local ngx_timer_at = ngx.timer.at
local ngx_time = ngx.time
local sub_str = string.sub
Expand Down
2 changes: 1 addition & 1 deletion apisix/core/config_yaml.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ local insert_tab = table.insert
local type = type
local ipairs = ipairs
local setmetatable = setmetatable
local ngx_sleep = ngx.sleep
local ngx_sleep = require("apisix.core.utils").sleep
local ngx_timer_at = ngx.timer.at
local ngx_time = ngx.time
local sub_str = string.sub
Expand Down
2 changes: 1 addition & 1 deletion apisix/core/timer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
-- limitations under the License.
--
local log = require("apisix.core.log")
local sleep = ngx.sleep
local sleep = require("apisix.core.utils").sleep
local timer_every = ngx.timer.every
local timer_at = ngx.timer.at
local update_time = ngx.update_time
Expand Down
20 changes: 19 additions & 1 deletion apisix/core/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ local type = type
local C = ffi.C
local ffi_string = ffi.string
local get_string_buf = base.get_string_buf

local exiting = ngx.worker.exiting
local ngx_sleep = ngx.sleep
local max_sleep_interval = 1

ffi.cdef[[
int ngx_escape_uri(char *dst, const char *src,
Expand Down Expand Up @@ -200,4 +202,20 @@ function _M.validate_header_value(value)
end


local function sleep(sec)
if sec <= max_sleep_interval then
return ngx_sleep(sec)
end
ngx_sleep(max_sleep_interval)
if exiting() then
return
end
sec = sec - max_sleep_interval
return sleep(sec)
end


_M.sleep = sleep


return _M
2 changes: 1 addition & 1 deletion apisix/plugins/fault-injection.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
-- limitations under the License.
--
local core = require("apisix.core")
local sleep = ngx.sleep
local sleep = core.sleep

local plugin_name = "fault-injection"

Expand Down
2 changes: 1 addition & 1 deletion apisix/plugins/limit-conn.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
--
local limit_conn_new = require("resty.limit.conn").new
local core = require("apisix.core")
local sleep = ngx.sleep
local sleep = core.sleep
local plugin_name = "limit-conn"


Expand Down
2 changes: 1 addition & 1 deletion apisix/plugins/limit-req.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
local limit_req_new = require("resty.limit.req").new
local core = require("apisix.core")
local plugin_name = "limit-req"
local sleep = ngx.sleep
local sleep = core.sleep


local schema = {
Expand Down
3 changes: 2 additions & 1 deletion apisix/plugins/wolf-rbac.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
local core = require("apisix.core")
local consumer = require("apisix.consumer")
local json = require("apisix.core.json")
local sleep = core.sleep
local ngx_re = require("ngx.re")
local http = require("resty.http")
local ipairs = ipairs
Expand Down Expand Up @@ -206,7 +207,7 @@ local function check_url_permission(server, appid, action, resName, client_ip, w
else
core.log.info("request [curl -v ", url, "] failed! status:", res.status)
if i < retry_max then
ngx.sleep(0.1)
sleep(0.1)
end
end
end
Expand Down

0 comments on commit 943be09

Please sign in to comment.