diff options
| author | 2024-01-08 08:21:22 +0100 | |
|---|---|---|
| committer | 2024-01-08 08:21:22 +0100 | |
| commit | 0289860d04ee11d8b00a6a2bd3c59ce6f0d7ea10 (patch) | |
| tree | 6e200f43e40245dc1e7088660a2f32b266614463 /httpd.c | |
| parent | Rewritten part of the job threads to be leaner and faster. (diff) | |
| download | snac2-0289860d04ee11d8b00a6a2bd3c59ce6f0d7ea10.tar.gz snac2-0289860d04ee11d8b00a6a2bd3c59ce6f0d7ea10.tar.xz snac2-0289860d04ee11d8b00a6a2bd3c59ce6f0d7ea10.zip | |
Non-important variable renaming.
Diffstat (limited to 'httpd.c')
| -rw-r--r-- | httpd.c | 69 |
1 files changed, 35 insertions, 34 deletions
| @@ -28,8 +28,8 @@ | |||
| 28 | 28 | ||
| 29 | /** server stat **/ | 29 | /** server stat **/ |
| 30 | 30 | ||
| 31 | srv_stat s_stat = {0}; | 31 | srv_state s_state = {0}; |
| 32 | srv_stat *p_stat = NULL; | 32 | srv_state *p_state = NULL; |
| 33 | 33 | ||
| 34 | 34 | ||
| 35 | /** job control **/ | 35 | /** job control **/ |
| @@ -49,6 +49,13 @@ static job_fifo_item *job_fifo_first = NULL; | |||
| 49 | static job_fifo_item *job_fifo_last = NULL; | 49 | static job_fifo_item *job_fifo_last = NULL; |
| 50 | 50 | ||
| 51 | 51 | ||
| 52 | /** other global data **/ | ||
| 53 | |||
| 54 | static jmp_buf on_break; | ||
| 55 | |||
| 56 | |||
| 57 | /** code **/ | ||
| 58 | |||
| 52 | /* nodeinfo 2.0 template */ | 59 | /* nodeinfo 2.0 template */ |
| 53 | const char *nodeinfo_2_0_template = "" | 60 | const char *nodeinfo_2_0_template = "" |
| 54 | "{\"version\":\"2.0\"," | 61 | "{\"version\":\"2.0\"," |
| @@ -234,9 +241,9 @@ int server_get_handler(xs_dict *req, const char *q_path, | |||
| 234 | *ctype = "text/plain"; | 241 | *ctype = "text/plain"; |
| 235 | *body = xs_str_new("UP\n"); | 242 | *body = xs_str_new("UP\n"); |
| 236 | 243 | ||
| 237 | xs *uptime = xs_str_time_diff(time(NULL) - p_stat->srv_start_time); | 244 | xs *uptime = xs_str_time_diff(time(NULL) - p_state->srv_start_time); |
| 238 | srv_log(xs_fmt("status: uptime: %s", uptime)); | 245 | srv_log(xs_fmt("status: uptime: %s", uptime)); |
| 239 | srv_log(xs_fmt("status: job_fifo len: %d", p_stat->job_fifo_size)); | 246 | srv_log(xs_fmt("status: job_fifo len: %d", p_state->job_fifo_size)); |
| 240 | } | 247 | } |
| 241 | 248 | ||
| 242 | if (status != 0) | 249 | if (status != 0) |
| @@ -263,7 +270,7 @@ void httpd_connection(FILE *f) | |||
| 263 | char *p; | 270 | char *p; |
| 264 | int fcgi_id; | 271 | int fcgi_id; |
| 265 | 272 | ||
| 266 | if (p_stat->use_fcgi) | 273 | if (p_state->use_fcgi) |
| 267 | req = xs_fcgi_request(f, &payload, &p_size, &fcgi_id); | 274 | req = xs_fcgi_request(f, &payload, &p_size, &fcgi_id); |
| 268 | else | 275 | else |
| 269 | req = xs_httpd_request(f, &payload, &p_size); | 276 | req = xs_httpd_request(f, &payload, &p_size); |
| @@ -401,7 +408,7 @@ void httpd_connection(FILE *f) | |||
| 401 | headers = xs_dict_append(headers, "access-control-allow-origin", "*"); | 408 | headers = xs_dict_append(headers, "access-control-allow-origin", "*"); |
| 402 | headers = xs_dict_append(headers, "access-control-allow-headers", "*"); | 409 | headers = xs_dict_append(headers, "access-control-allow-headers", "*"); |
| 403 | 410 | ||
| 404 | if (p_stat->use_fcgi) | 411 | if (p_state->use_fcgi) |
| 405 | xs_fcgi_response(f, status, headers, body, b_size, fcgi_id); | 412 | xs_fcgi_response(f, status, headers, body, b_size, fcgi_id); |
| 406 | else | 413 | else |
| 407 | xs_httpd_response(f, status, headers, body, b_size); | 414 | xs_httpd_response(f, status, headers, body, b_size); |
| @@ -448,7 +455,7 @@ void job_post(const xs_val *job, int urgent) | |||
| 448 | job_fifo_last = i; | 455 | job_fifo_last = i; |
| 449 | } | 456 | } |
| 450 | 457 | ||
| 451 | p_stat->job_fifo_size++; | 458 | p_state->job_fifo_size++; |
| 452 | 459 | ||
| 453 | /* unlock the mutex */ | 460 | /* unlock the mutex */ |
| 454 | pthread_mutex_unlock(&job_mutex); | 461 | pthread_mutex_unlock(&job_mutex); |
| @@ -480,7 +487,7 @@ void job_wait(xs_val **job) | |||
| 480 | *job = i->job; | 487 | *job = i->job; |
| 481 | xs_free(i); | 488 | xs_free(i); |
| 482 | 489 | ||
| 483 | p_stat->job_fifo_size--; | 490 | p_state->job_fifo_size--; |
| 484 | } | 491 | } |
| 485 | 492 | ||
| 486 | /* unlock the mutex */ | 493 | /* unlock the mutex */ |
| @@ -489,10 +496,6 @@ void job_wait(xs_val **job) | |||
| 489 | } | 496 | } |
| 490 | 497 | ||
| 491 | 498 | ||
| 492 | #ifndef MAX_THREADS | ||
| 493 | #define MAX_THREADS 256 | ||
| 494 | #endif | ||
| 495 | |||
| 496 | static void *job_thread(void *arg) | 499 | static void *job_thread(void *arg) |
| 497 | /* job thread */ | 500 | /* job thread */ |
| 498 | { | 501 | { |
| @@ -549,7 +552,7 @@ static void *background_thread(void *arg) | |||
| 549 | 552 | ||
| 550 | srv_log(xs_fmt("background thread started")); | 553 | srv_log(xs_fmt("background thread started")); |
| 551 | 554 | ||
| 552 | while (p_stat->srv_running) { | 555 | while (p_state->srv_running) { |
| 553 | time_t t; | 556 | time_t t; |
| 554 | int cnt = 0; | 557 | int cnt = 0; |
| 555 | 558 | ||
| @@ -606,8 +609,6 @@ static void *background_thread(void *arg) | |||
| 606 | } | 609 | } |
| 607 | 610 | ||
| 608 | 611 | ||
| 609 | static jmp_buf on_break; | ||
| 610 | |||
| 611 | void term_handler(int s) | 612 | void term_handler(int s) |
| 612 | { | 613 | { |
| 613 | (void)s; | 614 | (void)s; |
| @@ -629,12 +630,12 @@ void httpd(void) | |||
| 629 | 630 | ||
| 630 | /* setup the server stat structure */ | 631 | /* setup the server stat structure */ |
| 631 | { | 632 | { |
| 632 | p_stat = &s_stat; | 633 | p_state = &s_state; |
| 633 | } | 634 | } |
| 634 | 635 | ||
| 635 | p_stat->srv_start_time = time(NULL); | 636 | p_state->srv_start_time = time(NULL); |
| 636 | 637 | ||
| 637 | p_stat->use_fcgi = xs_type(xs_dict_get(srv_config, "fastcgi")) == XSTYPE_TRUE; | 638 | p_state->use_fcgi = xs_type(xs_dict_get(srv_config, "fastcgi")) == XSTYPE_TRUE; |
| 638 | 639 | ||
| 639 | address = xs_dict_get(srv_config, "address"); | 640 | address = xs_dict_get(srv_config, "address"); |
| 640 | port = xs_number_str(xs_dict_get(srv_config, "port")); | 641 | port = xs_number_str(xs_dict_get(srv_config, "port")); |
| @@ -644,13 +645,13 @@ void httpd(void) | |||
| 644 | return; | 645 | return; |
| 645 | } | 646 | } |
| 646 | 647 | ||
| 647 | p_stat->srv_running = 1; | 648 | p_state->srv_running = 1; |
| 648 | 649 | ||
| 649 | signal(SIGPIPE, SIG_IGN); | 650 | signal(SIGPIPE, SIG_IGN); |
| 650 | signal(SIGTERM, term_handler); | 651 | signal(SIGTERM, term_handler); |
| 651 | signal(SIGINT, term_handler); | 652 | signal(SIGINT, term_handler); |
| 652 | 653 | ||
| 653 | srv_log(xs_fmt("httpd%s start %s:%s %s", p_stat->use_fcgi ? " (FastCGI)" : "", | 654 | srv_log(xs_fmt("httpd%s start %s:%s %s", p_state->use_fcgi ? " (FastCGI)" : "", |
| 654 | address, port, USER_AGENT)); | 655 | address, port, USER_AGENT)); |
| 655 | 656 | ||
| 656 | /* show the number of usable file descriptors */ | 657 | /* show the number of usable file descriptors */ |
| @@ -679,29 +680,29 @@ void httpd(void) | |||
| 679 | pthread_mutex_init(&sleep_mutex, NULL); | 680 | pthread_mutex_init(&sleep_mutex, NULL); |
| 680 | pthread_cond_init(&sleep_cond, NULL); | 681 | pthread_cond_init(&sleep_cond, NULL); |
| 681 | 682 | ||
| 682 | p_stat->n_threads = xs_number_get(xs_dict_get(srv_config, "num_threads")); | 683 | p_state->n_threads = xs_number_get(xs_dict_get(srv_config, "num_threads")); |
| 683 | 684 | ||
| 684 | #ifdef _SC_NPROCESSORS_ONLN | 685 | #ifdef _SC_NPROCESSORS_ONLN |
| 685 | if (p_stat->n_threads == 0) { | 686 | if (p_state->n_threads == 0) { |
| 686 | /* get number of CPUs on the machine */ | 687 | /* get number of CPUs on the machine */ |
| 687 | p_stat->n_threads = sysconf(_SC_NPROCESSORS_ONLN); | 688 | p_state->n_threads = sysconf(_SC_NPROCESSORS_ONLN); |
| 688 | } | 689 | } |
| 689 | #endif | 690 | #endif |
| 690 | 691 | ||
| 691 | if (p_stat->n_threads < 4) | 692 | if (p_state->n_threads < 4) |
| 692 | p_stat->n_threads = 4; | 693 | p_state->n_threads = 4; |
| 693 | 694 | ||
| 694 | if (p_stat->n_threads > MAX_THREADS) | 695 | if (p_state->n_threads > MAX_THREADS) |
| 695 | p_stat->n_threads = MAX_THREADS; | 696 | p_state->n_threads = MAX_THREADS; |
| 696 | 697 | ||
| 697 | srv_debug(0, xs_fmt("using %d threads", p_stat->n_threads)); | 698 | srv_debug(0, xs_fmt("using %d threads", p_state->n_threads)); |
| 698 | 699 | ||
| 699 | /* thread #0 is the background thread */ | 700 | /* thread #0 is the background thread */ |
| 700 | pthread_create(&threads[0], NULL, background_thread, NULL); | 701 | pthread_create(&threads[0], NULL, background_thread, NULL); |
| 701 | 702 | ||
| 702 | /* the rest of threads are for job processing */ | 703 | /* the rest of threads are for job processing */ |
| 703 | char *ptr = (char *) 0x1; | 704 | char *ptr = (char *) 0x1; |
| 704 | for (n = 1; n < p_stat->n_threads; n++) | 705 | for (n = 1; n < p_state->n_threads; n++) |
| 705 | pthread_create(&threads[n], NULL, job_thread, ptr++); | 706 | pthread_create(&threads[n], NULL, job_thread, ptr++); |
| 706 | 707 | ||
| 707 | if (setjmp(on_break) == 0) { | 708 | if (setjmp(on_break) == 0) { |
| @@ -717,22 +718,22 @@ void httpd(void) | |||
| 717 | } | 718 | } |
| 718 | } | 719 | } |
| 719 | 720 | ||
| 720 | p_stat->srv_running = 0; | 721 | p_state->srv_running = 0; |
| 721 | 722 | ||
| 722 | /* send as many exit jobs as working threads */ | 723 | /* send as many exit jobs as working threads */ |
| 723 | for (n = 1; n < p_stat->n_threads; n++) | 724 | for (n = 1; n < p_state->n_threads; n++) |
| 724 | job_post(xs_stock_false, 0); | 725 | job_post(xs_stock_false, 0); |
| 725 | 726 | ||
| 726 | /* wait for all the threads to exit */ | 727 | /* wait for all the threads to exit */ |
| 727 | for (n = 0; n < p_stat->n_threads; n++) | 728 | for (n = 0; n < p_state->n_threads; n++) |
| 728 | pthread_join(threads[n], NULL); | 729 | pthread_join(threads[n], NULL); |
| 729 | 730 | ||
| 730 | sem_close(job_sem); | 731 | sem_close(job_sem); |
| 731 | sem_unlink(sem_name); | 732 | sem_unlink(sem_name); |
| 732 | 733 | ||
| 733 | xs *uptime = xs_str_time_diff(time(NULL) - p_stat->srv_start_time); | 734 | xs *uptime = xs_str_time_diff(time(NULL) - p_state->srv_start_time); |
| 734 | 735 | ||
| 735 | srv_log(xs_fmt("httpd%s stop %s:%s (run time: %s)", | 736 | srv_log(xs_fmt("httpd%s stop %s:%s (run time: %s)", |
| 736 | p_stat->use_fcgi ? " (FastCGI)" : "", | 737 | p_state->use_fcgi ? " (FastCGI)" : "", |
| 737 | address, port, uptime)); | 738 | address, port, uptime)); |
| 738 | } | 739 | } |