kore

An easy to use, scalable and secure web application framework for writing web APIs in C.
Commits | Files | Refs | README | LICENSE | git clone https://git.kore.io/kore.git

commit ee3fd3c039399607d074fcd47c22d6000ab01d17
parent 95c8b8e12669e951a5c9902cbe13f41685bed018
Author: Joris Vink <joris@coders.se>
Date:   Tue,  3 Sep 2013 08:40:00 +0200

Allow the user defined callback to run on workers as well.

Diffstat:
includes/kore.h | 1+
modules/example/module.conf | 7+++++--
modules/example/src/example.c | 10++++++++++
src/config.c | 24++++++++++++++++++++++++
src/kore.c | 13++++++++-----
src/worker.c | 11++++++++++-
6 files changed, 58 insertions(+), 8 deletions(-)

diff --git a/includes/kore.h b/includes/kore.h @@ -233,6 +233,7 @@ extern char *kore_cb_name; extern char *kore_ssl_cipher_list; extern DH *ssl_dhparam; extern int ssl_no_compression; +extern int kore_cb_worker; extern u_int8_t nlisteners; extern u_int64_t spdy_idle_time; diff --git a/modules/example/module.conf b/modules/example/module.conf @@ -24,11 +24,14 @@ workers 4 # The onload function is called everytime the module is loaded or reloaded. #onload myinit -# You can define a callback Kore calls from its parent process everytime -# the kore_cb_interval timer (in milliseconds) is reached. +# You can define a callback Kore calls from its parent process or +# workers everytime # the kore_cb_interval timer (in milliseconds) is reached. +# # NOTE: Remember that the parent process runs as root and is not chroot(). +# NOTE: If you want the cb to run on a worker, be sure to set kore_cb_worker. #kore_cb my_callback #kore_cb_interval 1000 +#kore_cb_worker 3 # Specifies what module to be loaded. load modules/example/example.module diff --git a/modules/example/src/example.c b/modules/example/src/example.c @@ -25,6 +25,7 @@ int serve_intro(struct http_request *); int serve_b64test(struct http_request *); int serve_spdyreset(struct http_request *); +void my_callback(void); void test_base64(u_int8_t *, u_int32_t, struct kore_buf *); char *b64tests[] = { @@ -148,3 +149,12 @@ test_base64(u_int8_t *src, u_int32_t slen, struct kore_buf *res) kore_buf_appendf(res, "\n"); } + +void +my_callback(void) +{ + if (worker != NULL) + kore_log(LOG_NOTICE, "running on worker %d", worker->id); + else + kore_log(LOG_NOTICE, "running from parent"); +} diff --git a/src/config.c b/src/config.c @@ -40,6 +40,7 @@ static int configure_ssl_no_compression(char **); static int configure_spdy_idle_time(char **); static int configure_kore_cb(char **); static int configure_kore_cb_interval(char **); +static int configure_kore_cb_worker(char **); static void domain_sslstart(void); static struct { @@ -65,6 +66,7 @@ static struct { { "certfile", configure_certfile }, { "certkey", configure_certkey }, { "kore_cb", configure_kore_cb }, + { "kore_cb_worker", configure_kore_cb_worker }, { "kore_cb_interval", configure_kore_cb_interval }, { NULL, NULL }, }; @@ -476,6 +478,28 @@ configure_kore_cb_interval(char **argv) return (KORE_RESULT_OK); } +static int +configure_kore_cb_worker(char **argv) +{ + int err; + + if (argv[1] == NULL) + return (KORE_RESULT_ERROR); + + if (kore_cb_worker != -1) { + kore_debug("kore_cb_worker already set"); + return (KORE_RESULT_ERROR); + } + + kore_cb_worker = kore_strtonum(argv[1], 10, 0, worker_count, &err); + if (err != KORE_RESULT_OK) { + kore_debug("invalid value for kore_cb_worker"); + return (KORE_RESULT_ERROR); + } + + return (KORE_RESULT_OK); +} + static void domain_sslstart(void) { diff --git a/src/kore.c b/src/kore.c @@ -29,11 +29,12 @@ struct passwd *pw = NULL; pid_t kore_pid = -1; u_int16_t cpu_count = 1; int kore_debug = 0; -void (*kore_cb)(void); u_int8_t worker_count = 0; char *runas_user = NULL; char *chroot_path = NULL; +int kore_cb_worker = -1; u_int64_t kore_cb_interval = 0; +void (*kore_cb)(void) = NULL; char *kore_pidfile = KORE_PIDFILE_DEFAULT; char *kore_ssl_cipher_list = KORE_DEFAULT_CIPHER_LIST; @@ -254,10 +255,12 @@ kore_server_start(void) if (!kore_accesslog_wait()) break; - now = kore_time_ms(); - if ((now - last_cb_run) >= kore_cb_interval) { - last_cb_run = now; - kore_cb(); + if (kore_cb != NULL && kore_cb_worker == -1) { + now = kore_time_ms(); + if ((now - last_cb_run) >= kore_cb_interval) { + last_cb_run = now; + kore_cb(); + } } kore_worker_wait(0); diff --git a/src/worker.c b/src/worker.c @@ -169,7 +169,7 @@ kore_worker_entry(struct kore_worker *kw) int quit; char buf[16]; struct connection *c, *cnext; - u_int64_t now, idle_check; + u_int64_t now, idle_check, last_cb_run; worker = kw; @@ -208,6 +208,7 @@ kore_worker_entry(struct kore_worker *kw) now = idle_check = 0; kore_platform_event_init(); kore_accesslog_worker_init(); + last_cb_run = kore_time_ms(); worker->accept_treshold = worker_max_connections / 10; kore_log(LOG_NOTICE, "worker %d started (cpu#%d)", kw->id, kw->cpu); @@ -251,6 +252,14 @@ kore_worker_entry(struct kore_worker *kw) } } + if (kore_cb != NULL && kore_cb_worker != -1 && + kore_cb_worker == worker->id) { + if ((now - last_cb_run) >= kore_cb_interval) { + last_cb_run = now; + kore_cb(); + } + } + for (c = TAILQ_FIRST(&disconnected); c != NULL; c = cnext) { cnext = TAILQ_NEXT(c, list); TAILQ_REMOVE(&disconnected, c, list);