kore/src/module.c

253 lines
5.5 KiB
C
Raw Normal View History

2013-05-01 16:03:48 +02:00
/*
2022-01-31 22:02:06 +01:00
* Copyright (c) 2013-2022 Joris Vink <joris@coders.se>
2013-05-01 16:03:48 +02:00
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
2013-05-01 16:03:48 +02:00
#include <sys/stat.h>
#include <dlfcn.h>
#include "kore.h"
#if !defined(KORE_NO_HTTP)
#include "http.h"
#endif
#if defined(KORE_USE_PYTHON)
#include "python_api.h"
#endif
#if defined(KORE_USE_LUA)
#include "lua_api.h"
#endif
static TAILQ_HEAD(, kore_module) modules;
static void native_free(struct kore_module *);
Rework HTTP and worker processes. The HTTP layer used to make a copy of each incoming header and its value for a request. Stop doing that and make HTTP headers zero-copy all across the board. This change comes with some api function changes, notably the http_request_header() function which now takes a const char ** rather than a char ** out pointer. This commit also constifies several members of http_request, beware. Additional rework how the worker processes deal with the accept lock. Before: if a worker held the accept lock and it accepted a new connection it would release the lock for others and back off for 500ms before attempting to grab the lock again. This approach worked but under high load this starts becoming obvious. Now: - workers not holding the accept lock and not having any connections will wait less long before returning from kore_platform_event_wait(). - workers not holding the accept lock will no longer blindly wait an arbitrary amount in kore_platform_event_wait() but will look at how long until the next lock grab is and base their timeout on that. - if a worker its next_lock timeout is up and failed to grab the lock it will try again in half the time again. - the worker process holding the lock will when releasing the lock double check if it still has space for newer connections, if it does it will keep the lock until it is full. This prevents the lock from bouncing between several non busy worker processes all the time. Additional fixes: - Reduce the number of times we check the timeout list, only do it twice per second rather then every event tick. - Fix solo worker count for TLS (we actually hold two processes, not one). - Make sure we don't accidentally miscalculate the idle time causing new connections under heavy load to instantly drop. - Swap from gettimeofday() to clock_gettime() now that MacOS caught up.
2018-02-14 13:48:49 +01:00
static void native_load(struct kore_module *);
static void native_reload(struct kore_module *);
static void *native_getsym(struct kore_module *, const char *);
struct kore_module_functions kore_native_module = {
.free = native_free,
.load = native_load,
.getsym = native_getsym,
.reload = native_reload,
};
void
kore_module_init(void)
{
TAILQ_INIT(&modules);
}
2013-05-01 16:03:48 +02:00
void
kore_module_cleanup(void)
{
struct kore_module *module, *next;
for (module = TAILQ_FIRST(&modules); module != NULL; module = next) {
next = TAILQ_NEXT(module, list);
TAILQ_REMOVE(&modules, module, list);
module->fun->free(module);
}
}
struct kore_module *
kore_module_load(const char *path, const char *onload, int type)
2013-05-01 16:03:48 +02:00
{
struct stat st;
struct kore_module *module;
2013-05-01 16:03:48 +02:00
module = kore_malloc(sizeof(struct kore_module));
module->ocb = NULL;
module->type = type;
module->onload = NULL;
module->handle = NULL;
if (path != NULL) {
if (stat(path, &st) == -1)
fatal("stat(%s): %s", path, errno_s);
2013-05-01 16:03:48 +02:00
module->path = kore_strdup(path);
} else {
module->path = NULL;
}
2013-05-01 16:03:48 +02:00
switch (module->type) {
case KORE_MODULE_NATIVE:
module->fun = &kore_native_module;
module->runtime = &kore_native_runtime;
break;
#if defined(KORE_USE_PYTHON)
case KORE_MODULE_PYTHON:
module->fun = &kore_python_module;
module->runtime = &kore_python_runtime;
break;
#endif
#if defined(KORE_USE_LUA)
case KORE_MODULE_LUA:
module->fun = &kore_lua_module;
module->runtime = &kore_lua_runtime;
break;
#endif
default:
fatal("kore_module_load: unknown type %d", type);
}
Rework HTTP and worker processes. The HTTP layer used to make a copy of each incoming header and its value for a request. Stop doing that and make HTTP headers zero-copy all across the board. This change comes with some api function changes, notably the http_request_header() function which now takes a const char ** rather than a char ** out pointer. This commit also constifies several members of http_request, beware. Additional rework how the worker processes deal with the accept lock. Before: if a worker held the accept lock and it accepted a new connection it would release the lock for others and back off for 500ms before attempting to grab the lock again. This approach worked but under high load this starts becoming obvious. Now: - workers not holding the accept lock and not having any connections will wait less long before returning from kore_platform_event_wait(). - workers not holding the accept lock will no longer blindly wait an arbitrary amount in kore_platform_event_wait() but will look at how long until the next lock grab is and base their timeout on that. - if a worker its next_lock timeout is up and failed to grab the lock it will try again in half the time again. - the worker process holding the lock will when releasing the lock double check if it still has space for newer connections, if it does it will keep the lock until it is full. This prevents the lock from bouncing between several non busy worker processes all the time. Additional fixes: - Reduce the number of times we check the timeout list, only do it twice per second rather then every event tick. - Fix solo worker count for TLS (we actually hold two processes, not one). - Make sure we don't accidentally miscalculate the idle time causing new connections under heavy load to instantly drop. - Swap from gettimeofday() to clock_gettime() now that MacOS caught up.
2018-02-14 13:48:49 +01:00
module->fun->load(module);
TAILQ_INSERT_TAIL(&modules, module, list);
if (onload != NULL) {
module->onload = kore_strdup(onload);
module->ocb = kore_malloc(sizeof(*module->ocb));
module->ocb->runtime = module->runtime;
module->ocb->addr = module->fun->getsym(module, onload);
if (module->ocb->addr == NULL) {
fatal("%s: onload '%s' not present",
module->path, onload);
}
}
return (module);
}
void
kore_module_onload(void)
{
struct kore_module *module;
TAILQ_FOREACH(module, &modules, list) {
if (module->path == NULL || module->ocb == NULL)
continue;
kore_runtime_onload(module->ocb, KORE_MODULE_LOAD);
}
}
void
kore_module_reload(int cbs)
{
struct stat st;
int ret;
struct kore_module *module;
TAILQ_FOREACH(module, &modules, list) {
if (module->path == NULL)
continue;
if (stat(module->path, &st) == -1) {
kore_log(LOG_NOTICE, "stat(%s): %s, skipping reload",
module->path, errno_s);
continue;
}
if (module->ocb != NULL && cbs == 1) {
ret = kore_runtime_onload(module->ocb,
KORE_MODULE_UNLOAD);
if (ret == KORE_RESULT_ERROR) {
kore_log(LOG_NOTICE,
"%s forced no reloaded", module->path);
continue;
}
}
module->fun->reload(module);
if (module->onload != NULL) {
kore_free(module->ocb);
module->ocb = kore_malloc(sizeof(*module->ocb));
module->ocb->runtime = module->runtime;
module->ocb->addr =
module->fun->getsym(module, module->onload);
if (module->ocb->addr == NULL) {
fatal("%s: onload '%s' not present",
module->path, module->onload);
}
}
if (module->ocb != NULL && cbs == 1)
kore_runtime_onload(module->ocb, KORE_MODULE_LOAD);
kore_log(LOG_NOTICE, "reloaded '%s' module", module->path);
}
#if !defined(KORE_NO_HTTP)
kore_route_reload();
kore_validator_reload();
#endif
2013-05-01 16:03:48 +02:00
}
int
kore_module_loaded(void)
{
if (TAILQ_EMPTY(&modules))
return (0);
return (1);
2013-05-01 16:03:48 +02:00
}
void *
kore_module_getsym(const char *symbol, struct kore_runtime **runtime)
{
void *ptr;
struct kore_module *module;
if (runtime != NULL)
*runtime = NULL;
TAILQ_FOREACH(module, &modules, list) {
ptr = module->fun->getsym(module, symbol);
if (ptr != NULL) {
if (runtime != NULL)
*runtime = module->runtime;
return (ptr);
}
}
return (NULL);
}
static void *
native_getsym(struct kore_module *module, const char *symbol)
{
return (dlsym(module->handle, symbol));
}
static void
native_free(struct kore_module *module)
{
kore_free(module->path);
(void)dlclose(module->handle);
kore_free(module);
}
static void
native_reload(struct kore_module *module)
{
if (dlclose(module->handle))
fatal("cannot close existing module: %s", dlerror());
Rework HTTP and worker processes. The HTTP layer used to make a copy of each incoming header and its value for a request. Stop doing that and make HTTP headers zero-copy all across the board. This change comes with some api function changes, notably the http_request_header() function which now takes a const char ** rather than a char ** out pointer. This commit also constifies several members of http_request, beware. Additional rework how the worker processes deal with the accept lock. Before: if a worker held the accept lock and it accepted a new connection it would release the lock for others and back off for 500ms before attempting to grab the lock again. This approach worked but under high load this starts becoming obvious. Now: - workers not holding the accept lock and not having any connections will wait less long before returning from kore_platform_event_wait(). - workers not holding the accept lock will no longer blindly wait an arbitrary amount in kore_platform_event_wait() but will look at how long until the next lock grab is and base their timeout on that. - if a worker its next_lock timeout is up and failed to grab the lock it will try again in half the time again. - the worker process holding the lock will when releasing the lock double check if it still has space for newer connections, if it does it will keep the lock until it is full. This prevents the lock from bouncing between several non busy worker processes all the time. Additional fixes: - Reduce the number of times we check the timeout list, only do it twice per second rather then every event tick. - Fix solo worker count for TLS (we actually hold two processes, not one). - Make sure we don't accidentally miscalculate the idle time causing new connections under heavy load to instantly drop. - Swap from gettimeofday() to clock_gettime() now that MacOS caught up.
2018-02-14 13:48:49 +01:00
module->fun->load(module);
}
static void
Rework HTTP and worker processes. The HTTP layer used to make a copy of each incoming header and its value for a request. Stop doing that and make HTTP headers zero-copy all across the board. This change comes with some api function changes, notably the http_request_header() function which now takes a const char ** rather than a char ** out pointer. This commit also constifies several members of http_request, beware. Additional rework how the worker processes deal with the accept lock. Before: if a worker held the accept lock and it accepted a new connection it would release the lock for others and back off for 500ms before attempting to grab the lock again. This approach worked but under high load this starts becoming obvious. Now: - workers not holding the accept lock and not having any connections will wait less long before returning from kore_platform_event_wait(). - workers not holding the accept lock will no longer blindly wait an arbitrary amount in kore_platform_event_wait() but will look at how long until the next lock grab is and base their timeout on that. - if a worker its next_lock timeout is up and failed to grab the lock it will try again in half the time again. - the worker process holding the lock will when releasing the lock double check if it still has space for newer connections, if it does it will keep the lock until it is full. This prevents the lock from bouncing between several non busy worker processes all the time. Additional fixes: - Reduce the number of times we check the timeout list, only do it twice per second rather then every event tick. - Fix solo worker count for TLS (we actually hold two processes, not one). - Make sure we don't accidentally miscalculate the idle time causing new connections under heavy load to instantly drop. - Swap from gettimeofday() to clock_gettime() now that MacOS caught up.
2018-02-14 13:48:49 +01:00
native_load(struct kore_module *module)
{
module->handle = dlopen(module->path, RTLD_NOW | RTLD_GLOBAL);
if (module->handle == NULL)
fatal("%s: %s", module->path, dlerror());
}