From c7b9858b03cd176e4be7f3c07bd18afcc9cebf40 Mon Sep 17 00:00:00 2001 From: Jan Kneschke Date: Thu, 14 Sep 2006 12:00:32 +0000 Subject: [PATCH] added support for lighty.status[] in mod_magnet - moved the status_counter_* functions from mod_fastcgi.c to status_counter.[ch] git-svn-id: svn://svn.lighttpd.net/lighttpd/branches/lighttpd-1.4.11-ssl-fixes@1298 152afb58-edef-0310-8abb-c4023f1b3aa9 --- src/Makefile.am | 4 +-- src/SConscript | 1 + src/mod_fastcgi.c | 44 +------------------------------- src/mod_magnet.c | 46 +++++++++++++++++++++++++++++++++ src/status_counter.c | 60 ++++++++++++++++++++++++++++++++++++++++++++ src/status_counter.h | 14 +++++++++++ 6 files changed, 124 insertions(+), 45 deletions(-) create mode 100644 src/status_counter.c create mode 100644 src/status_counter.h diff --git a/src/Makefile.am b/src/Makefile.am index bf32b514..ff2123ac 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -46,7 +46,7 @@ common_src=buffer.c log.c \ network_write.c network_linux_sendfile.c \ network_freebsd_sendfile.c network_writev.c \ network_solaris_sendfilev.c network_openssl.c \ - splaytree.c + splaytree.c status_counter.c src = server.c response.c connections.c network.c \ configfile.c configparser.c request.c proc_open.c @@ -246,7 +246,7 @@ hdr = server.h buffer.h network.h log.h keyvalue.h \ mod_ssi.h mod_ssi_expr.h inet_ntop_cache.h \ configparser.h mod_ssi_exprparser.h \ sys-mmap.h sys-socket.h mod_cml.h mod_cml_funcs.h \ - splaytree.h proc_open.h + splaytree.h proc_open.h status_counter.h DEFS= @DEFS@ -DLIBRARY_DIR="\"$(libdir)\"" diff --git a/src/SConscript b/src/SConscript index c2ce916d..a25f41c8 100644 --- a/src/SConscript +++ b/src/SConscript @@ -22,6 +22,7 @@ common_src = Split("buffer.c log.c \ network_write.c network_linux_sendfile.c \ network_freebsd_sendfile.c \ network_solaris_sendfilev.c network_openssl.c \ + status_counter.c \ ") src = Split("server.c response.c connections.c network.c \ diff --git a/src/mod_fastcgi.c b/src/mod_fastcgi.c index e64351ad..4f7260ba 100644 --- a/src/mod_fastcgi.c +++ b/src/mod_fastcgi.c @@ -23,6 +23,7 @@ #include "inet_ntop_cache.h" #include "stat_cache.h" +#include "status_counter.h" #include #include @@ -363,49 +364,6 @@ typedef struct { /* ok, we need a prototype */ static handler_t fcgi_handle_fdevent(void *s, void *ctx, int revents); -data_integer *status_counter_get_counter(server *srv, const char *s, size_t len) { - data_integer *di; - - if (NULL == (di = (data_integer *)array_get_element(srv->status, s))) { - /* not found, create it */ - - if (NULL == (di = (data_integer *)array_get_unused_element(srv->status, TYPE_INTEGER))) { - di = data_integer_init(); - } - buffer_copy_string_len(di->key, s, len); - di->value = 0; - - array_insert_unique(srv->status, (data_unset *)di); - } - return di; -} - -/* dummies of the statistic framework functions - * they will be moved to a statistics.c later */ -int status_counter_inc(server *srv, const char *s, size_t len) { - data_integer *di = status_counter_get_counter(srv, s, len); - - di->value++; - - return 0; -} - -int status_counter_dec(server *srv, const char *s, size_t len) { - data_integer *di = status_counter_get_counter(srv, s, len); - - if (di->value > 0) di->value--; - - return 0; -} - -int status_counter_set(server *srv, const char *s, size_t len, int val) { - data_integer *di = status_counter_get_counter(srv, s, len); - - di->value = val; - - return 0; -} - int fastcgi_status_copy_procname(buffer *b, fcgi_extension_host *host, fcgi_proc *proc) { buffer_copy_string(b, "fastcgi.backend."); buffer_append_string_buffer(b, host->id); diff --git a/src/mod_magnet.c b/src/mod_magnet.c index 5b723341..6a42ccdd 100644 --- a/src/mod_magnet.c +++ b/src/mod_magnet.c @@ -16,6 +16,7 @@ #include "mod_magnet_cache.h" #include "response.h" #include "stat_cache.h" +#include "status_counter.h" #define MAGNET_CONFIG_RAW_URL "magnet.attract-raw-url-to" @@ -214,6 +215,42 @@ static int magnet_reqhdr_get(lua_State *L) { return 1; } +static int magnet_status_get(lua_State *L) { + data_integer *di; + server *srv; + size_t key_len = 0; + + const char *key = luaL_checklstring(L, 2, &key_len); + + lua_pushstring(L, "lighty.srv"); + lua_gettable(L, LUA_REGISTRYINDEX); + srv = lua_touserdata(L, -1); + lua_pop(L, 1); + + di = status_counter_get_counter(srv, key, key_len); + + lua_pushnumber(L, (double)di->value); + + return 1; +} + +static int magnet_status_set(lua_State *L) { + size_t key_len = 0; + server *srv; + + const char *key = luaL_checklstring(L, 2, &key_len); + int counter = luaL_checkint(L, 3); + + lua_pushstring(L, "lighty.srv"); + lua_gettable(L, LUA_REGISTRYINDEX); + srv = lua_touserdata(L, -1); + lua_pop(L, 1); + + status_counter_set(srv, key, key_len, counter); + + return 0; +} + typedef struct { const char *name; enum { @@ -511,6 +548,15 @@ static handler_t magnet_attract(server *srv, connection *con, plugin_data *p, bu lua_setmetatable(L, -2); /* tie the metatable to request (sp -= 1) */ lua_setfield(L, -2, "env"); /* content = {} (sp -= 1) */ + lua_newtable(L); /* {} (sp += 1) */ + lua_newtable(L); /* the meta-table for the request-table (sp += 1) */ + lua_pushcfunction(L, magnet_status_get); /* (sp += 1) */ + lua_setfield(L, -2, "__index"); /* (sp -= 1) */ + lua_pushcfunction(L, magnet_status_set); /* (sp += 1) */ + lua_setfield(L, -2, "__newindex"); /* (sp -= 1) */ + lua_setmetatable(L, -2); /* tie the metatable to request (sp -= 1) */ + lua_setfield(L, -2, "status"); /* content = {} (sp -= 1) */ + /* add empty 'content' and 'header' tables */ lua_newtable(L); /* {} (sp += 1) */ lua_setfield(L, -2, "content"); /* content = {} (sp -= 1) */ diff --git a/src/status_counter.c b/src/status_counter.c new file mode 100644 index 00000000..3b345cd9 --- /dev/null +++ b/src/status_counter.c @@ -0,0 +1,60 @@ +#include + +#include "status_counter.h" +/** + * The status array can carry all the status information you want + * the key to the array is . + * and the values are counters + * + * example: + * fastcgi.backends = 10 + * fastcgi.active-backends = 6 + * fastcgi.backend..load = 24 + * fastcgi.backend..... + * + * fastcgi.backend..disconnects = ... + */ + +data_integer *status_counter_get_counter(server *srv, const char *s, size_t len) { + data_integer *di; + + if (NULL == (di = (data_integer *)array_get_element(srv->status, s))) { + /* not found, create it */ + + if (NULL == (di = (data_integer *)array_get_unused_element(srv->status, TYPE_INTEGER))) { + di = data_integer_init(); + } + buffer_copy_string_len(di->key, s, len); + di->value = 0; + + array_insert_unique(srv->status, (data_unset *)di); + } + return di; +} + +/* dummies of the statistic framework functions + * they will be moved to a statistics.c later */ +int status_counter_inc(server *srv, const char *s, size_t len) { + data_integer *di = status_counter_get_counter(srv, s, len); + + di->value++; + + return 0; +} + +int status_counter_dec(server *srv, const char *s, size_t len) { + data_integer *di = status_counter_get_counter(srv, s, len); + + if (di->value > 0) di->value--; + + return 0; +} + +int status_counter_set(server *srv, const char *s, size_t len, int val) { + data_integer *di = status_counter_get_counter(srv, s, len); + + di->value = val; + + return 0; +} + diff --git a/src/status_counter.h b/src/status_counter.h new file mode 100644 index 00000000..431bd9c8 --- /dev/null +++ b/src/status_counter.h @@ -0,0 +1,14 @@ +#ifndef _STATUS_COUNTER_H_ +#define _STATUS_COUNTER_H_ + +#include + +#include "array.h" +#include "base.h" + +data_integer *status_counter_get_counter(server *srv, const char *s, size_t len); +int status_counter_inc(server *srv, const char *s, size_t len); +int status_counter_dec(server *srv, const char *s, size_t len); +int status_counter_set(server *srv, const char *s, size_t len, int val); + +#endif