Browse Source

[core] register server prepare callbacks

personal/stbuehler/wip
Stefan Bühler 9 years ago
parent
commit
e0d9c0d602
  1. 10
      include/lighttpd/server.h
  2. 31
      include/lighttpd/throttle.h
  3. 37
      src/main/server.c

10
include/lighttpd/server.h

@ -14,6 +14,8 @@ typedef void (*liServerSocketReleaseCB)(liServerSocket *srv_sock);
typedef void (*liServerStateWaitCancelled)(liServer *srv, liServerStateWait *w);
typedef void (*liServerPrepareCallbackCB)(liServer *srv, gpointer data, gboolean aborted);
typedef enum {
LI_SERVER_INIT, /** start state */
LI_SERVER_LOADING, /** config loaded, prepare listeing sockets/open log files */
@ -86,6 +88,8 @@ struct liServer {
GHashTable *actions; /**< const gchar* => (liServerAction*) */
GHashTable *setups; /**< const gchar* => (liServerSetup*) */
GArray *prepare_callbacks;
GArray *li_plugins_handle_close; /** list of handle_close callbacks */
GArray *li_plugins_handle_vrclose; /** list of handle_vrclose callbacks */
@ -145,4 +149,10 @@ LI_API void li_server_state_ready(liServer *srv, liServerStateWait *sw);
/** only call from server state plugin hooks; push new wait condition to wait queue */
LI_API void li_server_state_wait(liServer *srv, liServerStateWait *sw);
/** if server is already running execute cb(data) immediately. otherwise runs before
* workers are started, but already initialized.
* if server isn't started it calls cb with aborted = TRUE.
*/
LI_API void li_server_register_prepare_cb(liServer *srv, liServerPrepareCallbackCB cb, gpointer data);
#endif

31
include/lighttpd/throttle.h

@ -3,9 +3,38 @@
#define THROTTLE_GRANULARITY 200 /* defines how frequently (in milliseconds) a magazine is refilled */
/* this makro converts a ev_tstamp to a gint. this is needed for atomic access. millisecond precision, can hold two weeks max */
/* this makro converts a li_tstamp to a gint. this is needed for atomic access. millisecond precision, can hold two weeks max */
#define THROTTLE_EVTSTAMP_TO_GINT(x) ((gint) ((x - ((gint)x - (gint)x % (3600*24*14))) * 1000))
typedef struct liThrottleState liThrottleState;
/* vrequest data */
#if 0
/* I/O throttling */
gboolean throttled; /* TRUE if vrequest is throttled */
struct {
gint magazine; /* currently available for use */
struct {
liThrottlePool *ptr; /* NULL if not in any throttling pool */
GList lnk;
GQueue *queue;
gint magazine;
} pool;
struct {
liThrottlePool *ptr;
GList lnk;
GQueue *queue;
gint magazine;
} ip;
struct {
gint rate; /* maximum transfer rate in bytes per second, 0 if unlimited */
ev_tstamp last_update;
} con;
liWaitQueueElem wqueue_elem;
} throttle;
#endif
typedef enum {
LI_THROTTLE_POOL_NAME,
LI_THROTTLE_POOL_IP

37
src/main/server.c

@ -6,6 +6,11 @@
# include <sys/resource.h>
#endif
typedef struct {
liServerPrepareCallbackCB callback;
gpointer data;
} liServerPrepareCallbackData;
static void li_server_listen_cb(liEventBase *watcher, int events);
static void li_server_stop(liServer *srv);
static void state_ready_cb(liEventBase *watcher, int events);
@ -101,6 +106,8 @@ liServer* li_server_new(const gchar *module_dir, gboolean module_resident) {
srv->actions = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, server_action_free);
srv->setups = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, server_setup_free);
srv->prepare_callbacks = g_array_new(FALSE, TRUE, sizeof(liServerPrepareCallbackData));
srv->li_plugins_handle_close = g_array_new(FALSE, TRUE, sizeof(liPlugin*));
srv->li_plugins_handle_vrclose = g_array_new(FALSE, TRUE, sizeof(liPlugin*));
srv->option_def_values = g_array_new(FALSE, TRUE, sizeof(liOptionValue));
@ -260,6 +267,16 @@ void li_server_free(liServer* srv) {
g_array_free(srv->li_plugins_handle_close, TRUE);
g_array_free(srv->li_plugins_handle_vrclose, TRUE);
if (NULL != srv->prepare_callbacks) {
guint i, len;
for (i = 0, len = srv->prepare_callbacks->len; i < len; ++i) {
liServerPrepareCallbackData *cbd = &g_array_index(srv->prepare_callbacks, liServerPrepareCallbackData, i);
cbd->callback(srv, cbd->data, TRUE);
}
g_array_free(srv->prepare_callbacks, TRUE);
srv->prepare_callbacks = NULL;
}
g_mutex_free(srv->action_mutex);
#ifdef LIGHTY_OS_LINUX
@ -362,7 +379,14 @@ static gboolean li_server_worker_init(liServer *srv) {
}
static void li_server_worker_run(liServer *srv) {
guint i;
guint i, len;
for (i = 0, len = srv->prepare_callbacks->len; i < len; ++i) {
liServerPrepareCallbackData *cbd = &g_array_index(srv->prepare_callbacks, liServerPrepareCallbackData, i);
cbd->callback(srv, cbd->data, FALSE);
}
g_array_free(srv->prepare_callbacks, TRUE);
srv->prepare_callbacks = NULL;
li_plugins_prepare_worker(srv->main_worker);
for (i = 1; i < srv->worker_count; i++) {
@ -892,3 +916,14 @@ void li_server_state_wait(liServer *srv, liServerStateWait *sw) {
g_mutex_unlock(srv->statelock);
}
void li_server_register_prepare_cb(liServer *srv, liServerPrepareCallbackCB cb, gpointer data) {
if (g_atomic_int_get(&srv->state) != LI_SERVER_INIT) {
cb(srv, data, FALSE);
} else {
liServerPrepareCallbackData cbd;
cbd.callback = cb;
cbd.data = data;
g_array_append_val(srv->prepare_callbacks, cbd);
}
}

Loading…
Cancel
Save