Skip to content
Draft
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
22 changes: 15 additions & 7 deletions main/fastcgi.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ typedef struct _fcgi_hash {
typedef struct _fcgi_req_hook fcgi_req_hook;

struct _fcgi_req_hook {
void(*on_accept)(void);
void(*on_read)(void);
void(*on_accept)(bool firstAccept);
void(*on_read)(bool keptAlive);
void(*on_close)(void);
};

Expand Down Expand Up @@ -874,7 +874,11 @@ static void fcgi_hook_dummy(void) {
return;
}

fcgi_request *fcgi_init_request(int listen_socket, void(*on_accept)(void), void(*on_read)(void), void(*on_close)(void))
static void fcgi_hook_dummy_bool(bool) {
return;
}

fcgi_request *fcgi_init_request(int listen_socket, void(*on_accept)(bool), void(*on_read)(bool), void(*on_close)(void))
{
fcgi_request *req = calloc(1, sizeof(fcgi_request));
req->listen_socket = listen_socket;
Expand All @@ -896,8 +900,8 @@ fcgi_request *fcgi_init_request(int listen_socket, void(*on_accept)(void), void(

*/
req->out_pos = req->out_buf;
req->hook.on_accept = on_accept ? on_accept : fcgi_hook_dummy;
req->hook.on_read = on_read ? on_read : fcgi_hook_dummy;
req->hook.on_accept = on_accept ? on_accept : fcgi_hook_dummy_bool;
req->hook.on_read = on_read ? on_read : fcgi_hook_dummy_bool;
req->hook.on_close = on_close ? on_close : fcgi_hook_dummy;

#ifdef _WIN32
Expand Down Expand Up @@ -1364,14 +1368,17 @@ int fcgi_accept_request(fcgi_request *req)
OVERLAPPED ov;
#endif

bool keptAlive = false;
bool firstAccept = false;
while (1) {
if (req->fd < 0) {
while (1) {
if (in_shutdown) {
return -1;
}

req->hook.on_accept();
req->hook.on_accept(firstAccept);
firstAccept = keptAlive = false;
#ifdef _WIN32
if (!req->tcp) {
pipe = (HANDLE)_get_osfhandle(req->listen_socket);
Expand Down Expand Up @@ -1479,7 +1486,8 @@ int fcgi_accept_request(fcgi_request *req)
} else if (in_shutdown) {
return -1;
}
req->hook.on_read();
req->hook.on_read(keptAlive);
keptAlive = true;
int read_result = fcgi_read_request(req);
if (read_result == 1) {
#ifdef _WIN32
Expand Down
2 changes: 1 addition & 1 deletion main/fastcgi.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void fcgi_close(fcgi_request *req, int force, int destroy);
int fcgi_in_shutdown(void);
void fcgi_terminate(void);
int fcgi_listen(const char *path, int backlog);
fcgi_request* fcgi_init_request(int listen_socket, void(*on_accept)(void), void(*on_read)(void), void(*on_close)(void));
fcgi_request* fcgi_init_request(int listen_socket, void(*on_accept)(bool), void(*on_read)(bool), void(*on_close)(void));
void fcgi_destroy_request(fcgi_request *req);
void fcgi_set_allowed_clients(char *ip);
int fcgi_accept_request(fcgi_request *req);
Expand Down
16 changes: 10 additions & 6 deletions sapi/fpm/fpm/fpm_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const char *fpm_request_get_stage_name(int stage) {
return requests_stages[stage];
}

void fpm_request_accepting(void)
void fpm_request_accepting(bool firstAccept)
{
struct fpm_scoreboard_proc_s *proc;
struct timeval now;
Expand All @@ -54,11 +54,13 @@ void fpm_request_accepting(void)
proc->tv = now;
fpm_scoreboard_proc_release(proc);

/* idle++, active-- */
fpm_scoreboard_update_commit(1, -1, 0, 0, 0, 0, 0, FPM_SCOREBOARD_ACTION_INC, NULL);
if (!firstAccept) {
/* idle++, active-- */
fpm_scoreboard_update_commit(1, -1, 0, 0, 0, 0, 0, FPM_SCOREBOARD_ACTION_INC, NULL);
}
}

void fpm_request_reading_headers(void)
void fpm_request_reading_headers(bool keptAlive)
{
struct fpm_scoreboard_proc_s *proc;

Expand Down Expand Up @@ -98,8 +100,10 @@ void fpm_request_reading_headers(void)
proc->content_length = 0;
fpm_scoreboard_proc_release(proc);

/* idle--, active++, request++ */
fpm_scoreboard_update_commit(-1, 1, 0, 0, 1, 0, 0, FPM_SCOREBOARD_ACTION_INC, NULL);
if (!keptAlive) {
/* idle--, active++, request++ */
fpm_scoreboard_update_commit(-1, 1, 0, 0, 1, 0, 0, FPM_SCOREBOARD_ACTION_INC, NULL);
}
}

void fpm_request_info(void)
Expand Down
4 changes: 2 additions & 2 deletions sapi/fpm/fpm/fpm_request.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
#define FPM_REQUEST_H 1

/* hanging in accept() */
void fpm_request_accepting(void);
void fpm_request_accepting(bool fistAccept);
/* start reading fastcgi request from very first byte */
void fpm_request_reading_headers(void);
void fpm_request_reading_headers(bool keptAlive);
/* not a stage really but a point in the php code, where all request params have become known to sapi */
void fpm_request_info(void);
/* the script is executing */
Expand Down
Loading