kore

Kore is a web application platform for writing scalable, concurrent web based processes in C or Python.
Commits | Files | Refs | README | LICENSE | git clone https://git.kore.io/kore.git

commit c06ecf1c709029c1bb2c5b19c32e6eb2b823d5f0
parent 3023ca0e577e8c5826f1a8babc144418fdcce8c4
Author: Joris Vink <joris@coders.se>
Date:   Thu, 26 Jan 2017 13:26:55 +0100

Teach single binaries about SIGHUP.

Make sure kore_onload() and kore_preload() can be called
either in native or python runtime.

Diffstat:
includes/kore.h | 3++-
src/kore.c | 4+---
src/python.c | 26+++++++++++++++++++++++++-
src/runtime.c | 19++++++++++++++++++-
src/worker.c | 7++-----
5 files changed, 48 insertions(+), 11 deletions(-)

diff --git a/includes/kore.h b/includes/kore.h @@ -216,7 +216,7 @@ struct kore_runtime { int (*http_request)(void *, struct http_request *); int (*validator)(void *, struct http_request *, void *); #endif - int (*execute)(void *, void *); + void (*execute)(void *); int (*onload)(void *, int); void (*connect)(void *, struct connection *); }; @@ -618,6 +618,7 @@ void kore_module_handler_free(struct kore_module_handle *); struct kore_runtime_call *kore_runtime_getcall(const char *); +void kore_runtime_execute(struct kore_runtime_call *); int kore_runtime_onload(struct kore_runtime_call *, int); void kore_runtime_connect(struct kore_runtime_call *, struct connection *); #if !defined(KORE_NO_HTTP) diff --git a/src/kore.c b/src/kore.c @@ -434,7 +434,7 @@ kore_server_start(void) #if defined(KORE_SINGLE_BINARY) rcall = kore_runtime_getcall("kore_preload"); if (rcall != NULL) { - rcall->runtime->execute(rcall->addr, NULL); + kore_runtime_execute(rcall); kore_free(rcall); } #endif @@ -458,10 +458,8 @@ kore_server_start(void) if (sig_recv != 0) { switch (sig_recv) { case SIGHUP: -#if !defined(KORE_SINGLE_BINARY) kore_worker_dispatch_signal(sig_recv); kore_module_reload(0); -#endif break; case SIGINT: case SIGQUIT: diff --git a/src/python.c b/src/python.c @@ -46,6 +46,7 @@ static void python_push_type(const char *, PyObject *, PyTypeObject *); static int python_runtime_http_request(void *, struct http_request *); static int python_runtime_validator(void *, struct http_request *, void *); #endif +static void python_runtime_execute(void *); static int python_runtime_onload(void *, int); static void python_runtime_connect(void *, struct connection *); @@ -73,7 +74,8 @@ struct kore_runtime kore_python_runtime = { .validator = python_runtime_validator, #endif .onload = python_runtime_onload, - .connect = python_runtime_connect + .connect = python_runtime_connect, + .execute = python_runtime_execute }; static struct { @@ -339,6 +341,28 @@ python_runtime_validator(void *addr, struct http_request *req, void *data) } #endif +static void +python_runtime_execute(void *addr) +{ + PyObject *callable, *args, *pyret; + + callable = (PyObject *)addr; + + if ((args = PyTuple_New(0)) == NULL) + fatal("python_runtime_execute: PyTuple_New failed"); + + PyErr_Clear(); + pyret = PyObject_Call(callable, args, NULL); + Py_DECREF(args); + + if (pyret == NULL) { + python_log_error("python_runtime_execute"); + fatal("failed to execute python call"); + } + + Py_DECREF(pyret); +} + static int python_runtime_onload(void *addr, int action) { diff --git a/src/runtime.c b/src/runtime.c @@ -26,6 +26,7 @@ #include "python_api.h" #endif +static void native_runtime_execute(void *); static int native_runtime_onload(void *, int); static void native_runtime_connect(void *, struct connection *); #if !defined(KORE_NO_HTTP) @@ -40,7 +41,8 @@ struct kore_runtime kore_native_runtime = { .validator = native_runtime_validator, #endif .onload = native_runtime_onload, - .connect = native_runtime_connect + .connect = native_runtime_connect, + .execute = native_runtime_execute }; struct kore_runtime_call * @@ -61,6 +63,12 @@ kore_runtime_getcall(const char *symbol) return (rcall); } +void +kore_runtime_execute(struct kore_runtime_call *rcall) +{ + rcall->runtime->execute(rcall->addr); +} + int kore_runtime_onload(struct kore_runtime_call *rcall, int action) { @@ -90,6 +98,15 @@ kore_runtime_validator(struct kore_runtime_call *rcall, #endif static void +native_runtime_execute(void *addr) +{ + void (*cb)(void); + + *(void **)&(cb) = addr; + cb(); +} + +static void native_runtime_connect(void *addr, struct connection *c) { void (*cb)(struct connection *); diff --git a/src/worker.c b/src/worker.c @@ -338,20 +338,17 @@ kore_worker_entry(struct kore_worker *kw) #if defined(KORE_SINGLE_BINARY) rcall = kore_runtime_getcall("kore_onload"); if (rcall != NULL) { - rcall->runtime->execute(rcall->addr, NULL); + kore_runtime_execute(rcall); kore_free(rcall); } -#else - kore_module_onload(); #endif + kore_module_onload(); for (;;) { if (sig_recv != 0) { switch (sig_recv) { case SIGHUP: -#if !defined(KORE_SINGLE_BINARY) kore_module_reload(1); -#endif break; case SIGQUIT: case SIGINT: