Skip to content

Commit f069b7e

Browse files
committed
feat: Add modules to reload as a global variable
1 parent 729cf9b commit f069b7e

File tree

2 files changed

+80
-12
lines changed

2 files changed

+80
-12
lines changed

frankenphp.c

+72-12
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@
3333
ZEND_TSRMLS_CACHE_DEFINE()
3434
#endif
3535

36-
static const char *MODULES_TO_RELOAD[] = {"filter", "session", NULL};
36+
#define INITIAL_MODULES_CAPACITY 8
37+
38+
static const char *MODULES_TO_RELOAD[] = {"filter", "session"};
39+
frankenphp_modules_to_reload modules_to_reload = {NULL, 0, 0};
3740

3841
frankenphp_version frankenphp_get_version() {
3942
return (frankenphp_version){
@@ -115,18 +118,42 @@ static void frankenphp_release_temporary_streams() {
115118
ZEND_HASH_FOREACH_END();
116119
}
117120

121+
static void init_modules_to_reload(void) {
122+
if (modules_to_reload.names != NULL) {
123+
return;
124+
}
125+
126+
size_t length = sizeof MODULES_TO_RELOAD / sizeof *MODULES_TO_RELOAD;
127+
128+
size_t capacity = length > 0 ? length * 2 : INITIAL_MODULES_CAPACITY;
129+
modules_to_reload.names = malloc(capacity * sizeof(char *));
130+
if (!modules_to_reload.names) {
131+
return; // TODO: handle this as an error
132+
}
133+
134+
for (size_t i = 0; i < length; i++) {
135+
modules_to_reload.names[i] = strdup(MODULES_TO_RELOAD[i]);
136+
}
137+
138+
modules_to_reload.length = length;
139+
modules_to_reload.capacity = capacity;
140+
}
141+
118142
/* Adapted from php_request_shutdown */
119143
static void frankenphp_worker_request_shutdown() {
120144
/* Flush all output buffers */
121145
zend_try { php_output_end_all(); }
122146
zend_end_try();
123147

124-
/* TODO: store the list of modules to reload in a global module variable */
125-
const char **module_name;
148+
init_modules_to_reload();
149+
126150
zend_module_entry *module;
127-
for (module_name = MODULES_TO_RELOAD; *module_name; module_name++) {
128-
if ((module = zend_hash_str_find_ptr(&module_registry, *module_name,
129-
strlen(*module_name)))) {
151+
for (size_t i = 0; i < modules_to_reload.length; i++) {
152+
module =
153+
zend_hash_str_find_ptr(&module_registry, modules_to_reload.names[i],
154+
strlen(modules_to_reload.names[i]));
155+
156+
if (module && module->request_shutdown_func) {
130157
module->request_shutdown_func(module->type, module->module_number);
131158
}
132159
}
@@ -144,6 +171,37 @@ static void frankenphp_worker_request_shutdown() {
144171
zend_set_memory_limit(PG(memory_limit));
145172
}
146173

174+
/* API for extensions to register their modules to reload */
175+
bool frankenphp_register_module_to_reload(const char *module_name) {
176+
if (module_name == NULL) {
177+
return false;
178+
}
179+
180+
init_modules_to_reload();
181+
182+
for (size_t i = 0; i < modules_to_reload.length; i++) {
183+
if (strcmp(modules_to_reload.names[i], module_name) == 0) {
184+
return true;
185+
}
186+
}
187+
188+
if (modules_to_reload.length >= modules_to_reload.capacity) {
189+
size_t new_capacity = modules_to_reload.capacity * 2;
190+
const char **new_names =
191+
realloc(modules_to_reload.names, new_capacity * sizeof(char *));
192+
if (!new_names) {
193+
return false; // Out of memory
194+
}
195+
modules_to_reload.names = new_names;
196+
modules_to_reload.capacity = new_capacity;
197+
}
198+
199+
modules_to_reload.names[modules_to_reload.length] = strdup(module_name);
200+
modules_to_reload.length++;
201+
202+
return true;
203+
}
204+
147205
// shutdown the dummy request that starts the worker script
148206
bool frankenphp_shutdown_dummy_request(void) {
149207
if (SG(server_context) == NULL) {
@@ -223,13 +281,15 @@ static int frankenphp_worker_request_startup() {
223281
}
224282
}
225283

226-
/* TODO: store the list of modules to reload in a global module variable */
227-
const char **module_name;
284+
init_modules_to_reload();
285+
286+
/* Reload modules */
228287
zend_module_entry *module;
229-
for (module_name = MODULES_TO_RELOAD; *module_name; module_name++) {
230-
if ((module = zend_hash_str_find_ptr(&module_registry, *module_name,
231-
strlen(*module_name))) &&
232-
module->request_startup_func) {
288+
for (size_t i = 0; i < modules_to_reload.length; i++) {
289+
module =
290+
zend_hash_str_find_ptr(&module_registry, modules_to_reload.names[i],
291+
strlen(modules_to_reload.names[i]));
292+
if (module && module->request_startup_func) {
233293
module->request_startup_func(module->type, module->module_number);
234294
}
235295
}

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 length;
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

@@ -91,4 +97,6 @@ void frankenphp_register_bulk(
9197
ht_key_value_pair auth_type, ht_key_value_pair remote_ident,
9298
ht_key_value_pair request_uri);
9399

100+
bool frankenphp_register_module_to_reload(const char *module_name);
101+
94102
#endif

0 commit comments

Comments
 (0)