Browse Source

[core] keep pool of (request_st *) for HTTP/2

master
Glenn Strauss 1 year ago
parent
commit
878073d1c9
  1. 4
      src/connections.c
  2. 56
      src/reqpool.c
  3. 12
      src/reqpool.h
  4. 4
      src/server.c

4
src/connections.c

@ -458,7 +458,7 @@ static connection *connection_init(server *srv) {
con->config_data_base = srv->config_data_base;
request_st * const r = &con->request;
request_init(r, con, srv);
request_init_data(r, con, srv);
config_reset_config(r);
con->write_queue = r->write_queue;
con->read_queue = r->read_queue;
@ -482,7 +482,7 @@ void connections_free(server *srv) {
chunkqueue_free(con->write_queue);
if (con->read_queue != r->read_queue)
chunkqueue_free(con->read_queue);
request_free(r);
request_free_data(r);
free(con->plugin_ctx);
buffer_free(con->dst_addr_buf);

56
src/reqpool.c

@ -19,7 +19,7 @@
void
request_init (request_st * const r, connection * const con, server * const srv)
request_init_data (request_st * const r, connection * const con, server * const srv)
{
r->write_queue = chunkqueue_init();
r->read_queue = chunkqueue_init();
@ -166,7 +166,7 @@ request_reset_ex (request_st * const r)
void
request_free (request_st * const r)
request_free_data (request_st * const r)
{
chunkqueue_free(r->reqbody_queue);
chunkqueue_free(r->write_queue);
@ -200,6 +200,32 @@ request_free (request_st * const r)
}
/* linked list of (request_st *) cached for reuse */
static request_st *reqpool;
/* max num of (request_st *) to cache */
static uint32_t reqspace;
void
request_pool_init (uint32_t sz)
{
reqspace = sz;
}
void
request_pool_free (void)
{
while (reqpool) {
request_st * const r = reqpool;
reqpool = (request_st *)r->con; /*(reuse r->con as next ptr)*/
request_free_data(r);
free(r);
++reqspace;
}
}
void
request_release (request_st * const r)
{
@ -217,19 +243,31 @@ request_release (request_st * const r)
request_reset_ex(r);
r->state = CON_STATE_CONNECT;
/* future: might keep a pool of reusable (request_st *) */
request_free(r);
free(r);
if (reqspace) {
--reqspace;
r->con = (connection *)reqpool; /*(reuse r->con as next ptr)*/
reqpool = r;
}
else {
request_free_data(r);
free(r);
}
}
request_st *
request_acquire (connection * const con)
{
/* future: might keep a pool of reusable (request_st *) */
request_st * const r = calloc(1, sizeof(request_st));
force_assert(r);
request_init(r, con, con->srv);
request_st *r = reqpool;
if (r) {
reqpool = (request_st *)r->con; /*(reuse r->con as next ptr)*/
++reqspace;
}
else {
r = calloc(1, sizeof(request_st));
force_assert(r);
request_init_data(r, con, con->srv);
}
r->con = con;
r->tmp_buf = con->srv->tmp_buf;

12
src/reqpool.h

@ -4,11 +4,19 @@
#include "base_decls.h"
void request_init (request_st *r, connection *con, server *srv);
void request_init_data (request_st *r, connection *con, server *srv);
void request_reset (request_st *r);
void request_reset_ex (request_st *r);
void request_free (request_st *r);
void request_release (request_st *r);
request_st * request_acquire (connection *con);
__attribute_cold__
void request_free_data (request_st *r);
__attribute_cold__
void request_pool_init (uint32_t sz);
__attribute_cold__
void request_pool_free (void);
#endif

4
src/server.c

@ -14,6 +14,7 @@
#include "stat_cache.h"
#include "plugin.h"
#include "network_write.h" /* network_write_show_handlers() */
#include "reqpool.h" /* request_pool_init() request_pool_free() */
#include "response.h" /* strftime_cache_reset() */
#ifdef HAVE_VERSIONSTAMP_H
@ -1356,6 +1357,8 @@ static int server_main_setup (server * const srv, int argc, char **argv) {
srv->max_conns = srv->max_fds/3;
}
request_pool_init(srv->max_conns);
/* libev backend overwrites our SIGCHLD handler and calls waitpid on SIGCHLD; we want our own SIGCHLD handling. */
#ifdef HAVE_SIGACTION
sigaction(SIGCHLD, &act, NULL);
@ -1627,6 +1630,7 @@ int main (int argc, char **argv) {
server_sockets_save(srv);
else
network_close(srv);
request_pool_free();
connections_free(srv);
plugins_free(srv);
server_free(srv);

Loading…
Cancel
Save