Skip to content
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
fe1158f
Decouple workers.
AlliBalliBaba Nov 1, 2024
ad34140
Moves code to separate file.
AlliBalliBaba Nov 1, 2024
89b211d
Cleans up the exponential backoff.
AlliBalliBaba Nov 2, 2024
7d2ab8c
Initial working implementation.
AlliBalliBaba Nov 2, 2024
f7e7d41
Refactors php threads to take callbacks.
AlliBalliBaba Nov 2, 2024
c03c59b
Cleanup.
AlliBalliBaba Nov 2, 2024
a9857dc
Cleanup.
AlliBalliBaba Nov 2, 2024
bac9555
Cleanup.
AlliBalliBaba Nov 2, 2024
a2f8d59
Cleanup.
AlliBalliBaba Nov 2, 2024
279924c
Merge branch 'main' into refactor/start-worker-threads-directly
AlliBalliBaba Nov 3, 2024
0825453
Adjusts watcher logic.
AlliBalliBaba Nov 3, 2024
17d5cbe
Adjusts the watcher logic.
AlliBalliBaba Nov 3, 2024
09e0ca6
Fix opcache_reset race condition.
AlliBalliBaba Nov 4, 2024
a726a2c
Merge branch 'main' into refactor/start-worker-threads-directly
AlliBalliBaba Nov 4, 2024
7f13ada
Fixing merge conflicts and formatting.
AlliBalliBaba Nov 4, 2024
13fb4bb
Prevents overlapping of TSRM reservation and script execution.
AlliBalliBaba Nov 5, 2024
a8a00c8
Adjustments as suggested by @dunglas.
AlliBalliBaba Nov 5, 2024
b4dd138
Adds error assertions.
AlliBalliBaba Nov 5, 2024
03f98fa
Adds comments.
AlliBalliBaba Nov 5, 2024
e52dd0f
Removes logs and explicitly compares to C.false.
AlliBalliBaba Nov 5, 2024
cd98e33
Resets check.
AlliBalliBaba Nov 5, 2024
4e2a2c6
Adds cast for safety.
AlliBalliBaba Nov 5, 2024
c51eb93
Fixes waitgroup overflow.
AlliBalliBaba Nov 5, 2024
89d8e26
Resolves waitgroup race condition on startup.
AlliBalliBaba Nov 6, 2024
3587243
Moves worker request logic to worker.go.
AlliBalliBaba Nov 7, 2024
ec32f0c
Removes defer.
AlliBalliBaba Nov 7, 2024
4e35698
Removes call from go to c.
AlliBalliBaba Nov 11, 2024
740fac7
Merge branch 'main' into refactor/start-worker-threads-directly
AlliBalliBaba Nov 15, 2024
8a272cb
Fixes merge conflict.
AlliBalliBaba Nov 15, 2024
ecce5d5
Adds fibers test back in.
AlliBalliBaba Nov 15, 2024
06ebd67
Refactors new thread loop approach.
AlliBalliBaba Nov 15, 2024
c811f4a
Removes redundant check.
AlliBalliBaba Nov 16, 2024
6bd047a
Adds compareAndSwap.
AlliBalliBaba Nov 16, 2024
55ad8ba
Refactor: removes global waitgroups and uses a 'thread state' abstrac…
AlliBalliBaba Nov 17, 2024
3ffbe06
Merge branch 'main' into refactor/start-worker-threads-directly
AlliBalliBaba Nov 17, 2024
01ed92b
Removes unnecessary method.
AlliBalliBaba Nov 17, 2024
790cccc
Updates comment.
AlliBalliBaba Nov 17, 2024
0dd2605
Removes unnecessary booleans.
AlliBalliBaba Nov 18, 2024
60a66b4
test
AlliBalliBaba Nov 24, 2024
4719fa8
First state machine steps.
AlliBalliBaba Nov 25, 2024
f72e8cb
Splits threads.
AlliBalliBaba Dec 6, 2024
d20e706
Minimal working implementation with broken tests.
AlliBalliBaba Dec 6, 2024
6747d15
Fixes tests.
AlliBalliBaba Dec 6, 2024
54dc267
Refactoring.
AlliBalliBaba Dec 7, 2024
660efcd
Merge branch 'main' into refactor/start-worker-threads-directly
AlliBalliBaba Dec 7, 2024
6214754
Fixes merge conflicts.
AlliBalliBaba Dec 7, 2024
00eb834
Formatting
AlliBalliBaba Dec 7, 2024
02b73b1
C formatting.
AlliBalliBaba Dec 7, 2024
421904e
More cleanup.
AlliBalliBaba Dec 7, 2024
cca2a00
Allows for clean state transitions.
AlliBalliBaba Dec 7, 2024
ec8aeb7
Adds state tests.
AlliBalliBaba Dec 7, 2024
b598bd3
Adds support for thread transitioning.
AlliBalliBaba Dec 7, 2024
66cedbd
Fixes the testdata path.
AlliBalliBaba Dec 7, 2024
06af5d5
Formatting.
AlliBalliBaba Dec 7, 2024
71c16bc
Allows transitioning back to inactive state.
AlliBalliBaba Dec 7, 2024
5095342
Fixes go linting.
AlliBalliBaba Dec 7, 2024
4b18059
Formatting.
AlliBalliBaba Dec 7, 2024
15429d9
Removes duplication.
AlliBalliBaba Dec 7, 2024
c080608
Applies suggestions by @dunglas
AlliBalliBaba Dec 7, 2024
a166b87
Removes redundant check.
AlliBalliBaba Dec 7, 2024
9491e6b
Locks the handler on restart.
AlliBalliBaba Dec 7, 2024
e795c86
Removes unnecessary log.
AlliBalliBaba Dec 8, 2024
ef1bd0d
Changes Unpin() logic as suggested by @withinboredom
AlliBalliBaba Dec 9, 2024
a8a4545
Adds suggestions by @dunglas and resolves TODO.
AlliBalliBaba Dec 10, 2024
23a6362
Makes restarts fully safe.
AlliBalliBaba Dec 10, 2024
18e3e58
Will make the initial startup fail even if the watcher is enabled (as…
AlliBalliBaba Dec 10, 2024
3672c60
Also adds compareAndSwap to the test.
AlliBalliBaba Dec 10, 2024
38f87b7
Adds comment.
AlliBalliBaba Dec 10, 2024
d97ebfe
Prevents panic on initial watcher startup.
AlliBalliBaba Dec 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions exponential_backoff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package frankenphp

import (
"sync"
"time"
)

const maxBackoff = 1 * time.Second
const minBackoff = 100 * time.Millisecond
const maxConsecutiveFailures = 6

type exponentialBackoff struct {
backoff time.Duration
failureCount int
mu sync.RWMutex
upFunc sync.Once
}

func newExponentialBackoff() *exponentialBackoff {
return &exponentialBackoff{backoff: minBackoff}
}

func (e *exponentialBackoff) reset() {
e.mu.Lock()
e.upFunc = sync.Once{}
wait := e.backoff * 2
e.mu.Unlock()
go func() {
time.Sleep(wait)
e.mu.Lock()
defer e.mu.Unlock()
e.upFunc.Do(func() {
// if we come back to a stable state, reset the failure count
if e.backoff == minBackoff {
e.failureCount = 0
}

// earn back the backoff over time
if e.failureCount > 0 {
e.backoff = max(e.backoff/2, minBackoff)
}
})
}()
}

func (e *exponentialBackoff) trigger(onMaxFailures func(failureCount int)) {
e.mu.RLock()
e.upFunc.Do(func() {
if e.failureCount >= maxConsecutiveFailures {
onMaxFailures(e.failureCount)
}
e.failureCount += 1
})
wait := e.backoff
e.mu.RUnlock()
time.Sleep(wait)
e.mu.Lock()
e.backoff = min(e.backoff*2, maxBackoff)
e.mu.Unlock()
}
59 changes: 21 additions & 38 deletions frankenphp.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ PHP_FUNCTION(frankenphp_finish_request) { /* {{{ */
php_header();

if (ctx->has_active_request) {
go_frankenphp_finish_request(thread_index, false);
go_frankenphp_finish_php_request(thread_index);
}

ctx->finished = true;
Expand Down Expand Up @@ -443,7 +443,7 @@ PHP_FUNCTION(frankenphp_handle_request) {

frankenphp_worker_request_shutdown();
ctx->has_active_request = false;
go_frankenphp_finish_request(thread_index, true);
go_frankenphp_finish_worker_request(thread_index);

RETURN_TRUE;
}
Expand Down Expand Up @@ -802,9 +802,9 @@ static void set_thread_name(char *thread_name) {
}

static void *php_thread(void *arg) {
char thread_name[16] = {0};
snprintf(thread_name, 16, "php-%" PRIxPTR, (uintptr_t)arg);
thread_index = (uintptr_t)arg;
char thread_name[16] = {0};
snprintf(thread_name, 16, "php-%" PRIxPTR, thread_index);
set_thread_name(thread_name);

#ifdef ZTS
Expand All @@ -814,7 +814,6 @@ static void *php_thread(void *arg) {
ZEND_TSRMLS_CACHE_UPDATE();
#endif
#endif

Comment thread
AlliBalliBaba marked this conversation as resolved.
local_ctx = malloc(sizeof(frankenphp_server_context));

/* check if a default filter is set in php.ini and only filter if
Expand All @@ -823,7 +822,8 @@ static void *php_thread(void *arg) {
cfg_get_string("filter.default", &default_filter);
should_filter_var = default_filter != NULL;

while (go_handle_request(thread_index)) {
// perform work until go signals to stop
while (go_frankenphp_on_thread_work(thread_index)) {
}

go_frankenphp_release_known_variable_keys(thread_index);
Expand All @@ -832,6 +832,8 @@ static void *php_thread(void *arg) {
ts_free_thread();
#endif

go_frankenphp_on_thread_shutdown(thread_index);

return NULL;
}

Expand All @@ -849,13 +851,11 @@ static void *php_main(void *arg) {
exit(EXIT_FAILURE);
}

intptr_t num_threads = (intptr_t)arg;

set_thread_name("php-main");

#ifdef ZTS
#if (PHP_VERSION_ID >= 80300)
php_tsrm_startup_ex(num_threads);
php_tsrm_startup_ex((intptr_t)arg);
#else
php_tsrm_startup();
#endif
Expand Down Expand Up @@ -883,28 +883,7 @@ static void *php_main(void *arg) {

frankenphp_sapi_module.startup(&frankenphp_sapi_module);

pthread_t *threads = malloc(num_threads * sizeof(pthread_t));
if (threads == NULL) {
perror("malloc failed");
exit(EXIT_FAILURE);
}

for (uintptr_t i = 0; i < num_threads; i++) {
if (pthread_create(&(*(threads + i)), NULL, &php_thread, (void *)i) != 0) {
perror("failed to create PHP thread");
free(threads);
exit(EXIT_FAILURE);
}
}

for (int i = 0; i < num_threads; i++) {
if (pthread_join((*(threads + i)), NULL) != 0) {
perror("failed to join PHP thread");
free(threads);
exit(EXIT_FAILURE);
}
}
free(threads);
go_frankenphp_main_thread_is_ready();

/* channel closed, shutdown gracefully */
frankenphp_sapi_module.shutdown(&frankenphp_sapi_module);
Expand All @@ -920,25 +899,29 @@ static void *php_main(void *arg) {
frankenphp_sapi_module.ini_entries = NULL;
}
#endif

go_shutdown();

go_frankenphp_shutdown_main_thread();
return NULL;
}

int frankenphp_init(int num_threads) {
int frankenphp_new_main_thread(int num_threads) {
pthread_t thread;

if (pthread_create(&thread, NULL, &php_main, (void *)(intptr_t)num_threads) !=
0) {
go_shutdown();

return -1;
}

Comment thread
AlliBalliBaba marked this conversation as resolved.
return pthread_detach(thread);
}

bool frankenphp_new_php_thread(uintptr_t thread_index) {
pthread_t thread;
if (pthread_create(&thread, NULL, &php_thread, (void *)thread_index) != 0) {
return false;
}
pthread_detach(thread);
return true;
}

int frankenphp_request_startup() {
if (php_request_startup() == SUCCESS) {
return SUCCESS;
Expand Down
Loading