Skip to content

Commit 8ce5b49

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

File tree

2 files changed

+86
-10
lines changed

2 files changed

+86
-10
lines changed

frankenphp.c

Lines changed: 78 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@
3333
ZEND_TSRMLS_CACHE_DEFINE()
3434
#endif
3535

36+
#define INITIAL_MODULES_CAPACITY 8
37+
3638
static const char *MODULES_TO_RELOAD[] = {"filter", "session", NULL};
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,45 @@ 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 count = 0;
127+
for (const char **ptr = MODULES_TO_RELOAD; *ptr != NULL; ptr++) {
128+
count++;
129+
}
130+
131+
size_t capacity = count > 0 ? count * 2 : INITIAL_MODULES_CAPACITY;
132+
modules_to_reload.names = malloc(capacity * sizeof(char *));
133+
if (!modules_to_reload.names) {
134+
return; // TODO: handle this as an error
135+
}
136+
137+
for (size_t i = 0; i < count; i++) {
138+
modules_to_reload.names[i] = strdup(MODULES_TO_RELOAD[i]);
139+
}
140+
141+
modules_to_reload.count = count;
142+
modules_to_reload.capacity = capacity;
143+
}
144+
118145
/* Adapted from php_request_shutdown */
119146
static void frankenphp_worker_request_shutdown() {
120147
/* Flush all output buffers */
121148
zend_try { php_output_end_all(); }
122149
zend_end_try();
123150

124-
/* TODO: store the list of modules to reload in a global module variable */
125-
const char **module_name;
151+
if (modules_to_reload.names == NULL) {
152+
init_modules_to_reload();
153+
}
154+
126155
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)))) {
156+
for (size_t i = 0; i < modules_to_reload.count; i++) {
157+
if ((module = zend_hash_str_find_ptr(&module_registry,
158+
modules_to_reload.names[i],
159+
strlen(modules_to_reload.names[i])))) {
130160
module->request_shutdown_func(module->type, module->module_number);
131161
}
132162
}
@@ -144,6 +174,39 @@ static void frankenphp_worker_request_shutdown() {
144174
zend_set_memory_limit(PG(memory_limit));
145175
}
146176

177+
/* API for extensions to register their modules to reload */
178+
bool frankenphp_register_module_to_reload(const char *module_name) {
179+
if (module_name == NULL) {
180+
return false;
181+
}
182+
183+
if (modules_to_reload.names == NULL) {
184+
init_modules_to_reload();
185+
}
186+
187+
for (size_t i = 0; i < modules_to_reload.count; i++) {
188+
if (strcmp(modules_to_reload.names[i], module_name) == 0) {
189+
return true;
190+
}
191+
}
192+
193+
if (modules_to_reload.count >= modules_to_reload.capacity) {
194+
size_t new_capacity = modules_to_reload.capacity * 2;
195+
const char **new_names =
196+
realloc(modules_to_reload.names, new_capacity * sizeof(char *));
197+
if (!new_names) {
198+
return false; // Out of memory
199+
}
200+
modules_to_reload.names = new_names;
201+
modules_to_reload.capacity = new_capacity;
202+
}
203+
204+
modules_to_reload.names[modules_to_reload.count] = strdup(module_name);
205+
modules_to_reload.count++;
206+
207+
return true;
208+
}
209+
147210
// shutdown the dummy request that starts the worker script
148211
bool frankenphp_shutdown_dummy_request(void) {
149212
if (SG(server_context) == NULL) {
@@ -223,12 +286,17 @@ static int frankenphp_worker_request_startup() {
223286
}
224287
}
225288

226-
/* TODO: store the list of modules to reload in a global module variable */
227-
const char **module_name;
289+
/* Initialize modules to reload if needed */
290+
if (modules_to_reload.names == NULL) {
291+
init_modules_to_reload();
292+
}
293+
294+
/* Reload modules */
228295
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))) &&
296+
for (size_t i = 0; i < modules_to_reload.count; i++) {
297+
if ((module = zend_hash_str_find_ptr(
298+
&module_registry, modules_to_reload.names[i],
299+
strlen(modules_to_reload.names[i]))) &&
232300
module->request_startup_func) {
233301
module->request_startup_func(module->type, module->module_number);
234302
}

frankenphp.h

Lines changed: 8 additions & 0 deletions
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

@@ -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)