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

perf: Remove all Cgo handles #1073

Merged
merged 9 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
61 changes: 27 additions & 34 deletions frankenphp.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,15 @@ frankenphp_config frankenphp_get_config() {
}

typedef struct frankenphp_server_context {
uintptr_t current_request;
uintptr_t main_request;
bool has_main_request;
bool has_active_request;
bool worker_ready;
char *cookie_data;
bool finished;
} frankenphp_server_context;

__thread frankenphp_server_context *local_ctx = NULL;
__thread uintptr_t thread_index;

static void frankenphp_free_request_context() {
frankenphp_server_context *ctx = SG(server_context);
Expand Down Expand Up @@ -231,9 +232,8 @@ PHP_FUNCTION(frankenphp_finish_request) { /* {{{ */
php_output_end_all();
php_header();

if (ctx->current_request != 0) {
go_frankenphp_finish_request(ctx->main_request, ctx->current_request,
false);
if (ctx->has_active_request) {
go_frankenphp_finish_request(thread_index, false);
}

ctx->finished = true;
Expand All @@ -249,7 +249,7 @@ PHP_FUNCTION(frankenphp_request_headers) {

frankenphp_server_context *ctx = SG(server_context);
struct go_apache_request_headers_return headers =
go_apache_request_headers(ctx->current_request, ctx->main_request);
go_apache_request_headers(thread_index, ctx->has_active_request);

array_init_size(return_value, headers.r1);

Expand All @@ -260,7 +260,7 @@ PHP_FUNCTION(frankenphp_request_headers) {
add_assoc_stringl_ex(return_value, key.data, key.len, val.data, val.len);
}

go_apache_request_cleanup(headers.r2);
go_apache_request_cleanup(thread_index);
}
/* }}} */

Expand Down Expand Up @@ -327,7 +327,7 @@ PHP_FUNCTION(frankenphp_handle_request) {

frankenphp_server_context *ctx = SG(server_context);

if (ctx->main_request == 0) {
if (!ctx->has_main_request) {
/* not a worker, throw an error */
zend_throw_exception(
spl_ce_RuntimeException,
Expand All @@ -340,18 +340,14 @@ PHP_FUNCTION(frankenphp_handle_request) {
frankenphp_worker_request_shutdown();

ctx->worker_ready = true;

/* Mark the worker as ready to handle requests */
go_frankenphp_worker_ready(ctx->main_request);
}

#ifdef ZEND_MAX_EXECUTION_TIMERS
/* Disable timeouts while waiting for a request to handle */
zend_unset_timeout();
#endif

uintptr_t request =
go_frankenphp_worker_handle_request_start(ctx->main_request);
bool request = go_frankenphp_worker_handle_request_start(thread_index);
if (frankenphp_worker_request_startup() == FAILURE
/* Shutting down */
|| !request) {
Expand Down Expand Up @@ -384,8 +380,8 @@ PHP_FUNCTION(frankenphp_handle_request) {
}

frankenphp_worker_request_shutdown();
ctx->current_request = 0;
go_frankenphp_finish_request(ctx->main_request, request, true);
ctx->has_active_request = false;
go_frankenphp_finish_request(thread_index, true);

RETURN_TRUE;
}
Expand Down Expand Up @@ -426,7 +422,7 @@ static zend_module_entry frankenphp_module = {
static void frankenphp_request_shutdown() {
frankenphp_server_context *ctx = SG(server_context);

if (ctx->main_request && ctx->current_request) {
if (ctx->has_main_request && ctx->has_active_request) {
frankenphp_destroy_super_globals();
}

Expand All @@ -437,7 +433,7 @@ static void frankenphp_request_shutdown() {
}

int frankenphp_update_server_context(
bool create, uintptr_t current_request, uintptr_t main_request,
bool create, bool has_main_request, bool has_active_request,

const char *request_method, char *query_string, zend_long content_length,
char *path_translated, char *request_uri, const char *content_type,
Expand All @@ -459,8 +455,8 @@ int frankenphp_update_server_context(
// It is not reset by zend engine, set it to 200.
SG(sapi_headers).http_response_code = 200;

ctx->main_request = main_request;
ctx->current_request = current_request;
ctx->has_main_request = has_main_request;
ctx->has_active_request = has_active_request;

SG(request_info).auth_password = auth_password;
SG(request_info).auth_user = auth_user;
Expand Down Expand Up @@ -493,9 +489,8 @@ static size_t frankenphp_ub_write(const char *str, size_t str_length) {
return 0;
}

struct go_ub_write_return result = go_ub_write(
ctx->current_request ? ctx->current_request : ctx->main_request,
(char *)str, str_length);
struct go_ub_write_return result =
go_ub_write(thread_index, (char *)str, str_length);

if (result.r1) {
php_handle_aborted_connection();
Expand All @@ -512,7 +507,7 @@ static int frankenphp_send_headers(sapi_headers_struct *sapi_headers) {
int status;
frankenphp_server_context *ctx = SG(server_context);

if (ctx->current_request == 0) {
if (!ctx->has_active_request) {
return SAPI_HEADER_SEND_FAILED;
}

Expand All @@ -526,35 +521,35 @@ static int frankenphp_send_headers(sapi_headers_struct *sapi_headers) {
}
}

go_write_headers(ctx->current_request, status, &sapi_headers->headers);
go_write_headers(thread_index, status, &sapi_headers->headers);

return SAPI_HEADER_SENT_SUCCESSFULLY;
}

static void frankenphp_sapi_flush(void *server_context) {
frankenphp_server_context *ctx = (frankenphp_server_context *)server_context;

if (ctx && ctx->current_request != 0 && go_sapi_flush(ctx->current_request)) {
if (ctx && ctx->has_active_request && go_sapi_flush(thread_index)) {
php_handle_aborted_connection();
}
}

static size_t frankenphp_read_post(char *buffer, size_t count_bytes) {
frankenphp_server_context *ctx = SG(server_context);

return ctx->current_request
? go_read_post(ctx->current_request, buffer, count_bytes)
return ctx->has_active_request
? go_read_post(thread_index, buffer, count_bytes)
: 0;
}

static char *frankenphp_read_cookies(void) {
frankenphp_server_context *ctx = SG(server_context);

if (ctx->current_request == 0) {
if (!ctx->has_active_request) {
return "";
}

ctx->cookie_data = go_read_cookies(ctx->current_request);
ctx->cookie_data = go_read_cookies(thread_index);

return ctx->cookie_data;
}
Expand Down Expand Up @@ -664,16 +659,13 @@ void frankenphp_register_bulk_variables(go_string known_variables[27],

static void frankenphp_register_variables(zval *track_vars_array) {
/* https://www.php.net/manual/en/reserved.variables.server.php */
frankenphp_server_context *ctx = SG(server_context);

/* In CGI mode, we consider the environment to be a part of the server
* variables
*/
php_import_environment_variables(track_vars_array);

go_register_variables(ctx->current_request ? ctx->current_request
: ctx->main_request,
track_vars_array);
go_register_variables(thread_index, track_vars_array);
}

static void frankenphp_log_message(const char *message, int syslog_type_int) {
Expand Down Expand Up @@ -732,6 +724,7 @@ 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;
set_thread_name(thread_name);

#ifdef ZTS
Expand All @@ -744,7 +737,7 @@ static void *php_thread(void *arg) {

local_ctx = malloc(sizeof(frankenphp_server_context));

while (go_handle_request()) {
while (go_handle_request(thread_index)) {
}

#ifdef ZTS
Expand Down
Loading
Loading