Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: decouple worker threads from non-worker threads #1137

Open
wants to merge 38 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
fe1158f
Decouple workers.
Alliballibaba2 Nov 1, 2024
ad34140
Moves code to separate file.
Alliballibaba2 Nov 1, 2024
89b211d
Cleans up the exponential backoff.
Alliballibaba2 Nov 2, 2024
7d2ab8c
Initial working implementation.
Alliballibaba2 Nov 2, 2024
f7e7d41
Refactors php threads to take callbacks.
Alliballibaba2 Nov 2, 2024
c03c59b
Cleanup.
Alliballibaba2 Nov 2, 2024
a9857dc
Cleanup.
Alliballibaba2 Nov 2, 2024
bac9555
Cleanup.
Alliballibaba2 Nov 2, 2024
a2f8d59
Cleanup.
Alliballibaba2 Nov 2, 2024
279924c
Merge branch 'main' into refactor/start-worker-threads-directly
Alliballibaba2 Nov 3, 2024
0825453
Adjusts watcher logic.
Alliballibaba2 Nov 3, 2024
17d5cbe
Adjusts the watcher logic.
Alliballibaba2 Nov 3, 2024
09e0ca6
Fix opcache_reset race condition.
Alliballibaba2 Nov 4, 2024
a726a2c
Merge branch 'main' into refactor/start-worker-threads-directly
Alliballibaba2 Nov 4, 2024
7f13ada
Fixing merge conflicts and formatting.
Alliballibaba2 Nov 4, 2024
13fb4bb
Prevents overlapping of TSRM reservation and script execution.
Alliballibaba2 Nov 5, 2024
a8a00c8
Adjustments as suggested by @dunglas.
Alliballibaba2 Nov 5, 2024
b4dd138
Adds error assertions.
Alliballibaba2 Nov 5, 2024
03f98fa
Adds comments.
Alliballibaba2 Nov 5, 2024
e52dd0f
Removes logs and explicitly compares to C.false.
Alliballibaba2 Nov 5, 2024
cd98e33
Resets check.
Alliballibaba2 Nov 5, 2024
4e2a2c6
Adds cast for safety.
Alliballibaba2 Nov 5, 2024
c51eb93
Fixes waitgroup overflow.
Alliballibaba2 Nov 5, 2024
89d8e26
Resolves waitgroup race condition on startup.
Alliballibaba2 Nov 6, 2024
3587243
Moves worker request logic to worker.go.
Alliballibaba2 Nov 7, 2024
ec32f0c
Removes defer.
Alliballibaba2 Nov 7, 2024
4e35698
Removes call from go to c.
Alliballibaba2 Nov 11, 2024
740fac7
Merge branch 'main' into refactor/start-worker-threads-directly
Alliballibaba2 Nov 15, 2024
8a272cb
Fixes merge conflict.
Alliballibaba2 Nov 15, 2024
ecce5d5
Adds fibers test back in.
Alliballibaba2 Nov 15, 2024
06ebd67
Refactors new thread loop approach.
Alliballibaba2 Nov 15, 2024
c811f4a
Removes redundant check.
Alliballibaba2 Nov 16, 2024
6bd047a
Adds compareAndSwap.
Alliballibaba2 Nov 16, 2024
55ad8ba
Refactor: removes global waitgroups and uses a 'thread state' abstrac…
Alliballibaba2 Nov 17, 2024
3ffbe06
Merge branch 'main' into refactor/start-worker-threads-directly
Alliballibaba2 Nov 17, 2024
01ed92b
Removes unnecessary method.
Alliballibaba2 Nov 17, 2024
790cccc
Updates comment.
Alliballibaba2 Nov 17, 2024
0dd2605
Removes unnecessary booleans.
Alliballibaba2 Nov 18, 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

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

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
Loading