From f7b4804251cfc228e5c0913fe0a0ec00a76185b9 Mon Sep 17 00:00:00 2001 From: Paul Kulchenko Date: Mon, 2 Aug 2021 14:53:24 -0700 Subject: [PATCH] Add Sleep API to redbean (#220) --- tool/net/help.txt | 4 ++++ tool/net/redbean.c | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/tool/net/help.txt b/tool/net/help.txt index cd3dd91d0..c1a7441aa 100644 --- a/tool/net/help.txt +++ b/tool/net/help.txt @@ -864,6 +864,10 @@ FUNCTIONS Slurp(filename:str) → str Reads file data from local file system. + Sleep(seconds:number) + Sleeps the specified number of seconds (can be fractional). The + smallest interval is a millisecond. + Route([host:str,[path:str]]) Instructs redbean to follow the normal HTTP serving path. This function is useful when writing an OnHttpRequest handler, since diff --git a/tool/net/redbean.c b/tool/net/redbean.c index ee307d5d6..51e80e5c2 100644 --- a/tool/net/redbean.c +++ b/tool/net/redbean.c @@ -4885,6 +4885,11 @@ static int LuaLog(lua_State *L) { return 0; } +static int LuaSleep(lua_State *L) { + usleep(1000000 * luaL_checknumber(L, 1)); + return 0; +} + static int LuaIsHiddenPath(lua_State *L) { size_t n; const char *s; @@ -5267,6 +5272,7 @@ static const luaL_Reg kLuaFuncs[] = { {"Sha256", LuaSha256}, // {"Sha384", LuaSha384}, // {"Sha512", LuaSha512}, // + {"Sleep", LuaSleep}, // {"Slurp", LuaSlurp}, // {"StoreAsset", LuaStoreAsset}, // {"Underlong", LuaUnderlong}, //