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

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


The following commit(s) were added to refs/heads/master by this push:
     new 1090a38551 Add support for millisecond sleep in Lua plugin (#11905)
1090a38551 is described below

commit 1090a38551b18eb5fb16fdebad48e8a7ec37e151
Author: Kit Chan <[email protected]>
AuthorDate: Mon Dec 9 19:56:32 2024 +0100

    Add support for millisecond sleep in Lua plugin (#11905)
    
    * Update ts_lua_misc.cc
    
    * Update lua.en.rst
---
 doc/admin-guide/plugins/lua.en.rst |  8 +++++---
 plugins/lua/ts_lua_misc.cc         | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+), 3 deletions(-)

diff --git a/doc/admin-guide/plugins/lua.en.rst 
b/doc/admin-guide/plugins/lua.en.rst
index afd0a885d9..a1ecfa4caf 100644
--- a/doc/admin-guide/plugins/lua.en.rst
+++ b/doc/admin-guide/plugins/lua.en.rst
@@ -3884,13 +3884,15 @@ We will get the response like this:
 
 :ref:`TOP <admin-plugins-ts-lua>`
 
-ts.sleep
---------
+ts.sleep or ts.sleep_ms
+-----------------------
 **syntax:** *ts.sleep(sec)*
 
+**syntax:** *ts.sleep_ms(msec)*
+
 **context:** *hook point functions added after do_remap*
 
-**description:** Sleeps for the specified seconds without blocking.
+**description:** Sleeps for the specified seconds (or milliseconds) without 
blocking.
 
 Behind the scene, this method makes use of the ATS event model.
 
diff --git a/plugins/lua/ts_lua_misc.cc b/plugins/lua/ts_lua_misc.cc
index 9526b12f7b..ffbb9d3a80 100644
--- a/plugins/lua/ts_lua_misc.cc
+++ b/plugins/lua/ts_lua_misc.cc
@@ -32,6 +32,7 @@ static int ts_lua_note(lua_State *L);
 static int ts_lua_warning(lua_State *L);
 static int ts_lua_alert(lua_State *L);
 static int ts_lua_sleep(lua_State *L);
+static int ts_lua_sleep_ms(lua_State *L);
 static int ts_lua_host_lookup(lua_State *L);
 static int ts_lua_schedule(lua_State *L);
 static int ts_lua_get_install_dir(lua_State *L);
@@ -103,6 +104,10 @@ ts_lua_inject_misc_api(lua_State *L)
   lua_pushcfunction(L, ts_lua_sleep);
   lua_setfield(L, -2, "sleep");
 
+  /* ts.sleep_ms(...) */
+  lua_pushcfunction(L, ts_lua_sleep_ms);
+  lua_setfield(L, -2, "sleep_ms");
+
   /* ts.schedule(...) */
   lua_pushcfunction(L, ts_lua_schedule);
   lua_setfield(L, -2, "schedule");
@@ -394,6 +399,36 @@ done:
   return 0;
 }
 
+static int
+ts_lua_sleep_ms(lua_State *L)
+{
+  int                msec;
+  TSAction           action;
+  TSCont             contp;
+  ts_lua_async_item *ai;
+  ts_lua_cont_info  *ci;
+
+  ci = ts_lua_get_cont_info(L);
+  if (ci == nullptr) {
+    TSError("[ts_lua][%s] no cont info found", __FUNCTION__);
+    TSReleaseAssert(!"Unexpected fetch of cont info");
+    return 0;
+  }
+
+  msec = luaL_checknumber(L, 1);
+  if (msec < 1) {
+    msec = 1;
+  }
+
+  contp  = TSContCreate(ts_lua_sleep_handler, ci->mutex);
+  action = TSContScheduleOnPool(contp, msec, TS_THREAD_POOL_NET);
+
+  ai = ts_lua_async_create_item(contp, ts_lua_sleep_cleanup, (void *)action, 
ci);
+  TSContDataSet(contp, ai);
+
+  return lua_yield(L, 0);
+}
+
 static int
 ts_lua_sleep(lua_State *L)
 {

Reply via email to