From 634fdf0a3fbc9bcf4c329169d27413811d9487bf Mon Sep 17 00:00:00 2001 From: Paul Kulchenko Date: Tue, 2 Aug 2022 15:57:23 -0700 Subject: [PATCH] Add configuration for heartbeat interval --- tool/net/help.txt | 4 ++++ tool/net/redbean.c | 19 +++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/tool/net/help.txt b/tool/net/help.txt index 7d99d3b25..03c054d23 100644 --- a/tool/net/help.txt +++ b/tool/net/help.txt @@ -1368,6 +1368,10 @@ FUNCTIONS as Content-Range and Date, which are abstracted by the transport layer. + ProgramHeartbeatInterval(milliseconds:int) + Sets the heartbeat interval (in milliseconds). 5000ms is the default + and 100ms is the minimum. + ProgramTimeout(milliseconds:int|seconds:int) Default timeout is 60000ms. Minimal value of timeout is 10(ms). Negative values (<0) sets the keepalive in seconds. diff --git a/tool/net/redbean.c b/tool/net/redbean.c index b9bba8c9b..dc9b3c48f 100644 --- a/tool/net/redbean.c +++ b/tool/net/redbean.c @@ -150,7 +150,6 @@ STATIC_YOINK("ShowCrashReportsEarly"); #endif #define VERSION 0x02000f -#define HEARTBEAT 5000 /*ms*/ #define HASH_LOAD_FACTOR /* 1. / */ 4 #define MONITOR_MICROS 150000 #define READ(F, P, N) readv(F, &(struct iovec){P, N}, 1) @@ -418,6 +417,7 @@ static int sslpskindex; static int oldloglevel; static int messageshandled; static int sslticketlifetime; +static int heartbeatint = 5000; // ms static uint32_t clientaddrsize; static size_t zsize; @@ -4737,6 +4737,16 @@ static int LuaProgramUniprocess(lua_State *L) { return 1; } +static int LuaProgramHeartbeatInterval(lua_State *L) { + OnlyCallFromMainProcess(L, "ProgramHeartbeatInterval"); + if (!lua_isinteger(L, 1) && !lua_isnoneornil(L, 1)) { + return luaL_argerror(L, 1, "invalid heartbeat interval; integer expected"); + } + lua_pushinteger(L, heartbeatint); + if (lua_isinteger(L, 1)) heartbeatint = MAX(100, lua_tointeger(L, 1)); + return 1; +} + static int LuaProgramAddr(lua_State *L) { uint32_t ip; OnlyCallFromInitLua(L, "ProgramAddr"); @@ -5218,6 +5228,7 @@ static const luaL_Reg kLuaFuncs[] = { {"ProgramDirectory", LuaProgramDirectory}, // {"ProgramGid", LuaProgramGid}, // {"ProgramHeader", LuaProgramHeader}, // + {"ProgramHeartbeatInterval", LuaProgramHeartbeatInterval}, // {"ProgramLogBodies", LuaProgramLogBodies}, // {"ProgramLogMessages", LuaProgramLogMessages}, // {"ProgramLogPath", LuaProgramLogPath}, // @@ -7066,7 +7077,7 @@ static int EventLoop(int ms) { EnterMeltdownMode(); lua_repl_unlock(); meltdown = false; - } else if ((t = nowl()) - lastheartbeat > HEARTBEAT / 1000.) { + } else if ((t = nowl()) - lastheartbeat > heartbeatint / 1000.) { lastheartbeat = t; HandleHeartbeat(); } else if (HandlePoll(ms) == -1) { @@ -7366,12 +7377,12 @@ void RedBean(int argc, char *argv[]) { } } #ifdef STATIC - EventLoop(HEARTBEAT); + EventLoop(heartbeatint); #else GetHostsTxt(); // for effect GetResolvConf(); // for effect if (daemonize || uniprocess || !linenoiseIsTerminal()) { - EventLoop(HEARTBEAT); + EventLoop(heartbeatint); } else if (IsWindows()) { CHECK_NE(-1, _spawn(WindowsReplThread, 0, &replth)); EventLoop(100);