Skip to content

Commit 0a0a53c

Browse files
committed
feat: Add modules to reload as a global variable
1 parent 072151d commit 0a0a53c

File tree

2 files changed

+85
-10
lines changed

2 files changed

+85
-10
lines changed

frankenphp.c

+77-10
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ static const char HARDCODED_INI[] = "max_execution_time=0\n"
4040
"max_input_time=-1\n\0";
4141
#endif
4242

43+
#define INITIAL_MODULES_CAPACITY 8
44+
4345
static const char *MODULES_TO_RELOAD[] = {"filter", "session", NULL};
46+
frankenphp_modules_to_reload modules_to_reload = {NULL, 0, 0};
4447

4548
frankenphp_version frankenphp_get_version() {
4649
return (frankenphp_version){
@@ -130,18 +133,45 @@ static void frankenphp_release_temporary_streams() {
130133
ZEND_HASH_FOREACH_END();
131134
}
132135

136+
static void init_modules_to_reload(void) {
137+
if (modules_to_reload.names != NULL) {
138+
return;
139+
}
140+
141+
size_t count = 0;
142+
for (const char **ptr = MODULES_TO_RELOAD; *ptr != NULL; ptr++) {
143+
count++;
144+
}
145+
146+
size_t capacity = count > 0 ? count * 2 : INITIAL_MODULES_CAPACITY;
147+
modules_to_reload.names = malloc(capacity * sizeof(char*));
148+
if (!modules_to_reload.names) {
149+
return; // TODO: handle this as an error
150+
}
151+
152+
for (size_t i = 0; i < count; i++) {
153+
modules_to_reload.names[i] = strdup(MODULES_TO_RELOAD[i]);
154+
}
155+
156+
modules_to_reload.count = count;
157+
modules_to_reload.capacity = capacity;
158+
}
159+
133160
/* Adapted from php_request_shutdown */
134161
static void frankenphp_worker_request_shutdown() {
135162
/* Flush all output buffers */
136163
zend_try { php_output_end_all(); }
137164
zend_end_try();
138165

139-
/* TODO: store the list of modules to reload in a global module variable */
140-
const char **module_name;
166+
if (modules_to_reload.names == NULL) {
167+
init_modules_to_reload();
168+
}
169+
141170
zend_module_entry *module;
142-
for (module_name = MODULES_TO_RELOAD; *module_name; module_name++) {
143-
if ((module = zend_hash_str_find_ptr(&module_registry, *module_name,
144-
strlen(*module_name)))) {
171+
for (size_t i = 0; i < modules_to_reload.count; i++) {
172+
if ((module = zend_hash_str_find_ptr(&module_registry,
173+
modules_to_reload.names[i],
174+
strlen(modules_to_reload.names[i])))) {
145175
module->request_shutdown_func(module->type, module->module_number);
146176
}
147177
}
@@ -159,6 +189,38 @@ static void frankenphp_worker_request_shutdown() {
159189
zend_set_memory_limit(PG(memory_limit));
160190
}
161191

192+
/* API for extensions to register their modules to reload */
193+
bool frankenphp_register_module_to_reload(const char *module_name) {
194+
if (module_name == NULL) {
195+
return false;
196+
}
197+
198+
if (modules_to_reload.names == NULL) {
199+
init_modules_to_reload();
200+
}
201+
202+
for (size_t i = 0; i < modules_to_reload.count; i++) {
203+
if (strcmp(modules_to_reload.names[i], module_name) == 0) {
204+
return true;
205+
}
206+
}
207+
208+
if (modules_to_reload.count >= modules_to_reload.capacity) {
209+
size_t new_capacity = modules_to_reload.capacity * 2;
210+
const char **new_names = realloc(modules_to_reload.names, new_capacity * sizeof(char*));
211+
if (!new_names) {
212+
return false; // Out of memory
213+
}
214+
modules_to_reload.names = new_names;
215+
modules_to_reload.capacity = new_capacity;
216+
}
217+
218+
modules_to_reload.names[modules_to_reload.count] = strdup(module_name);
219+
modules_to_reload.count++;
220+
221+
return true;
222+
}
223+
162224
PHPAPI void get_full_env(zval *track_vars_array) {
163225
struct go_getfullenv_return full_env = go_getfullenv(thread_index);
164226

@@ -225,12 +287,17 @@ static int frankenphp_worker_request_startup() {
225287
frankenphp_server_context *ctx = SG(server_context);
226288
ctx->finished = false;
227289

228-
/* TODO: store the list of modules to reload in a global module variable */
229-
const char **module_name;
290+
/* Initialize modules to reload if needed */
291+
if (modules_to_reload.names == NULL) {
292+
init_modules_to_reload();
293+
}
294+
295+
/* Reload modules */
230296
zend_module_entry *module;
231-
for (module_name = MODULES_TO_RELOAD; *module_name; module_name++) {
232-
if ((module = zend_hash_str_find_ptr(&module_registry, *module_name,
233-
sizeof(*module_name) - 1)) &&
297+
for (size_t i = 0; i < modules_to_reload.count; i++) {
298+
if ((module = zend_hash_str_find_ptr(&module_registry,
299+
modules_to_reload.names[i],
300+
strlen(modules_to_reload.names[i]))) &&
234301
module->request_startup_func) {
235302
module->request_startup_func(module->type, module->module_number);
236303
}

frankenphp.h

+8
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ typedef struct frankenphp_config {
4646
} frankenphp_config;
4747
frankenphp_config frankenphp_get_config();
4848

49+
typedef struct {
50+
const char **names;
51+
size_t count;
52+
size_t capacity;
53+
} frankenphp_modules_to_reload;
54+
4955
int frankenphp_new_main_thread(int num_threads);
5056
bool frankenphp_new_php_thread(uintptr_t thread_index);
5157

@@ -89,4 +95,6 @@ void frankenphp_register_bulk(
8995
ht_key_value_pair auth_type, ht_key_value_pair remote_ident,
9096
ht_key_value_pair request_uri);
9197

98+
bool frankenphp_register_module_to_reload(const char *module_name);
99+
92100
#endif

0 commit comments

Comments
 (0)