mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-29 07:48:32 +00:00
Add redbean OnLogLatency hook (#495)
This commit is contained in:
parent
6598940d8a
commit
574eba8352
2 changed files with 32 additions and 5 deletions
|
@ -534,6 +534,12 @@ HOOKS
|
||||||
each time redbean accepts a new client connection. If it returns
|
each time redbean accepts a new client connection. If it returns
|
||||||
`true`, redbean will close the connection without calling fork.
|
`true`, redbean will close the connection without calling fork.
|
||||||
|
|
||||||
|
OnLogLatency(reqtimeus:int,contimeus:int)
|
||||||
|
If this function is defined it'll be called from the main process
|
||||||
|
each time redbean completes handling of a request, but before the
|
||||||
|
response is sent. The handler received the time (in µs) since the
|
||||||
|
request handling and connection handling started.
|
||||||
|
|
||||||
OnProcessCreate(pid:int,ip:int,port:int,serverip:int,serverport:int)
|
OnProcessCreate(pid:int,ip:int,port:int,serverip:int,serverport:int)
|
||||||
If this function is defined it'll be called from the main process
|
If this function is defined it'll be called from the main process
|
||||||
each time redbean forks a connection handler worker process. The
|
each time redbean forks a connection handler worker process. The
|
||||||
|
|
|
@ -385,6 +385,7 @@ static bool gotcachecontrol;
|
||||||
static bool interpretermode;
|
static bool interpretermode;
|
||||||
static bool sslclientverify;
|
static bool sslclientverify;
|
||||||
static bool connectionclose;
|
static bool connectionclose;
|
||||||
|
static bool hasonloglatency;
|
||||||
static bool hasonworkerstop;
|
static bool hasonworkerstop;
|
||||||
static bool isexitingworker;
|
static bool isexitingworker;
|
||||||
static bool hasonworkerstart;
|
static bool hasonworkerstart;
|
||||||
|
@ -1108,6 +1109,21 @@ static bool LuaOnClientConnection(void) {
|
||||||
return dropit;
|
return dropit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void LuaOnLogLatency(long reqtime, long contime) {
|
||||||
|
#ifndef STATIC
|
||||||
|
lua_State *L = GL;
|
||||||
|
int n = lua_gettop(L);
|
||||||
|
lua_getglobal(L, "OnLogLatency");
|
||||||
|
lua_pushinteger(L, reqtime);
|
||||||
|
lua_pushinteger(L, contime);
|
||||||
|
if (LuaCallWithTrace(L, 2, 0, NULL) != LUA_OK) {
|
||||||
|
LogLuaError("OnLogLatency", lua_tostring(L, -1));
|
||||||
|
lua_pop(L, 1); // pop error
|
||||||
|
}
|
||||||
|
AssertLuaStackIsAt(L, n);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
static void LuaOnProcessCreate(int pid) {
|
static void LuaOnProcessCreate(int pid) {
|
||||||
#ifndef STATIC
|
#ifndef STATIC
|
||||||
uint32_t ip, serverip;
|
uint32_t ip, serverip;
|
||||||
|
@ -5483,6 +5499,7 @@ static void LuaInit(void) {
|
||||||
hasonprocessdestroy = IsHookDefined("OnProcessDestroy");
|
hasonprocessdestroy = IsHookDefined("OnProcessDestroy");
|
||||||
hasonworkerstart = IsHookDefined("OnWorkerStart");
|
hasonworkerstart = IsHookDefined("OnWorkerStart");
|
||||||
hasonworkerstop = IsHookDefined("OnWorkerStop");
|
hasonworkerstop = IsHookDefined("OnWorkerStop");
|
||||||
|
hasonloglatency = IsHookDefined("OnLogLatency");
|
||||||
} else {
|
} else {
|
||||||
DEBUGF("(srvr) no /.init.lua defined");
|
DEBUGF("(srvr) no /.init.lua defined");
|
||||||
}
|
}
|
||||||
|
@ -6258,6 +6275,7 @@ static bool StreamResponse(char *p) {
|
||||||
|
|
||||||
static bool HandleMessageActual(void) {
|
static bool HandleMessageActual(void) {
|
||||||
int rc;
|
int rc;
|
||||||
|
long reqtime, contime;
|
||||||
char *p;
|
char *p;
|
||||||
long double now;
|
long double now;
|
||||||
if ((rc = ParseHttpMessage(&msg, inbuf.p, amtread)) != -1) {
|
if ((rc = ParseHttpMessage(&msg, inbuf.p, amtread)) != -1) {
|
||||||
|
@ -6298,12 +6316,15 @@ static bool HandleMessageActual(void) {
|
||||||
p = stpcpy(p, referrerpolicy);
|
p = stpcpy(p, referrerpolicy);
|
||||||
p = stpcpy(p, "\r\n");
|
p = stpcpy(p, "\r\n");
|
||||||
}
|
}
|
||||||
if (loglatency || LOGGABLE(kLogDebug)) {
|
if (loglatency || LOGGABLE(kLogDebug) || hasonloglatency) {
|
||||||
now = nowl();
|
now = nowl();
|
||||||
LOGF(kLogDebug, "(stat) %`'.*s latency r: %,ldµs c: %,ldµs",
|
reqtime = (long)((now - startrequest) * 1e6L);
|
||||||
msg.uri.b - msg.uri.a, inbuf.p + msg.uri.a,
|
contime = (long)((now - startconnection) * 1e6L);
|
||||||
(long)((now - startrequest) * 1e6L),
|
if (hasonloglatency) LuaOnLogLatency(reqtime, contime);
|
||||||
(long)((now - startconnection) * 1e6L));
|
if (loglatency || LOGGABLE(kLogDebug))
|
||||||
|
LOGF(kLogDebug, "(stat) %`'.*s latency r: %,ldµs c: %,ldµs",
|
||||||
|
msg.uri.b - msg.uri.a, inbuf.p + msg.uri.a,
|
||||||
|
reqtime, contime);
|
||||||
}
|
}
|
||||||
if (!generator) {
|
if (!generator) {
|
||||||
return TransmitResponse(p);
|
return TransmitResponse(p);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue