You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by sp...@apache.org on 2022/06/20 06:24:50 UTC

[apisix] branch master updated: fix: the argument to usleep should be integer (#7271)

This is an automated email from the ASF dual-hosted git repository.

spacewander pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new c72c08737 fix: the argument to usleep should be integer (#7271)
c72c08737 is described below

commit c72c08737f31ca815018d672da9261fd719209d6
Author: 罗泽轩 <sp...@gmail.com>
AuthorDate: Mon Jun 20 14:24:45 2022 +0800

    fix: the argument to usleep should be integer (#7271)
    
    Signed-off-by: spacewander <sp...@gmail.com>
---
 apisix/core/config_xds.lua |  6 ++----
 apisix/core/os.lua         | 19 ++++++++++++++++++-
 t/core/os.t                | 19 +++++++++++++++++++
 3 files changed, 39 insertions(+), 5 deletions(-)

diff --git a/apisix/core/config_xds.lua b/apisix/core/config_xds.lua
index e5e452f7e..793592b6f 100644
--- a/apisix/core/config_xds.lua
+++ b/apisix/core/config_xds.lua
@@ -23,6 +23,7 @@ local config_local      = require("apisix.core.config_local")
 local string            = require("apisix.core.string")
 local log               = require("apisix.core.log")
 local json              = require("apisix.core.json")
+local os                = require("apisix.core.os")
 local ngx_sleep         = require("apisix.core.utils").sleep
 local check_schema      = require("apisix.core.schema").check
 local new_tab           = require("table.new")
@@ -67,10 +68,7 @@ end
 
 
 ffi.cdef[[
-typedef unsigned int  useconds_t;
-
 extern void initial(void* config_zone, void* version_zone);
-int usleep(useconds_t usec);
 ]]
 
 local created_obj  = {}
@@ -323,7 +321,7 @@ function _M.new(key, opts)
 
         -- blocking until xds completes initial configuration
         while true do
-            C.usleep(0.1)
+            os.usleep(1000)
             fetch_version()
             if latest_version then
                 break
diff --git a/apisix/core/os.lua b/apisix/core/os.lua
index ae721e883..4a922d01e 100644
--- a/apisix/core/os.lua
+++ b/apisix/core/os.lua
@@ -23,6 +23,9 @@ local ffi = require("ffi")
 local ffi_str = ffi.string
 local ffi_errno = ffi.errno
 local C = ffi.C
+local ceil = math.ceil
+local floor = math.floor
+local error = error
 local tostring = tostring
 local type = type
 
@@ -71,6 +74,20 @@ function _M.setenv(name, value)
 end
 
 
+---
+--  sleep blockingly in microseconds
+--
+-- @function core.os.usleep
+-- @tparam number us The number of microseconds.
+local function usleep(us)
+    if ceil(us) ~= floor(us) then
+        error("bad microseconds: " .. us)
+    end
+    C.usleep(us)
+end
+_M.usleep = usleep
+
+
 local function waitpid_nohang(pid)
     local res = C.waitpid(pid, nil, WNOHANG)
     if res == -1 then
@@ -86,7 +103,7 @@ function _M.waitpid(pid, timeout)
     local total = timeout * 1000 * 1000
     while step * count < total do
         count = count + 1
-        C.usleep(step)
+        usleep(step)
         local ok, err = waitpid_nohang(pid)
         if err then
             return nil, err
diff --git a/t/core/os.t b/t/core/os.t
index dff6c8b3c..4c99b311a 100644
--- a/t/core/os.t
+++ b/t/core/os.t
@@ -70,3 +70,22 @@ A
 false
 false
 false
+
+
+
+=== TEST 3: usleep, bad arguments
+--- config
+    location /t {
+        content_by_lua_block {
+            local core = require("apisix.core")
+
+            for _, c in ipairs({
+                {us = 0.1},
+            }) do
+                local ok = pcall(core.os.usleep, c.us)
+                ngx.say(ok)
+            end
+        }
+    }
+--- response_body
+false