Skip to content

Commit

Permalink
Allocate connections structs lazily.
Browse files Browse the repository at this point in the history
Keep memory use down when there are few simultaneous connections.
Allocate connection structs on demand, but save them in a free list
and reuse them for faster operation under load. Also remove the hard
limit on number of simultaneous connections.
  • Loading branch information
Keith Rarick committed Sep 25, 2007
1 parent c7669c9 commit 2da6bae
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 18 deletions.
1 change: 0 additions & 1 deletion beanstalkd.c
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,6 @@ main(int argc, char **argv)

drop_root();
prot_init();
conn_init();
daemonize();
event_init();
set_sig_handlers();
Expand Down
17 changes: 3 additions & 14 deletions conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
#include "util.h"
#include "prot.h"

static struct conn pool[MAX_CONNECTIONS];

/* linked list of free connections. See struct conn's next field in conn.h */
static conn pool_front, pool_rear;
static conn pool_front = NULL, pool_rear = NULL;

static int
pool_conn_p()
Expand All @@ -24,7 +22,7 @@ conn_alloc()
{
conn c;

if (!pool_conn_p()) return NULL;
if (!pool_conn_p()) return malloc(sizeof(struct conn));

/* remove it from the list */
c = pool_front;
Expand All @@ -47,22 +45,13 @@ conn_free(conn c)
pool_rear = c;
}

void
conn_init()
{
int i;

pool_front = pool_rear = NULL;
for (i = 0; i < MAX_CONNECTIONS; i++) conn_free(&pool[i]);
}

conn
make_conn(int fd, char start_state)
{
conn c;

c = conn_alloc();
if (!c) return NULL;
if (!c) return warn("OOM"), NULL;

c->fd = fd;
c->state = start_state;
Expand Down
3 changes: 0 additions & 3 deletions conn.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
#include "event.h"
#include "job.h"

/* We will stop accepting new connections at 8 simultaneous Ki-connections. */
#define MAX_CONNECTIONS (8 * 1024)

#define STATE_WANTCOMMAND 0
#define STATE_WANTDATA 1
#define STATE_SENDJOB 2
Expand Down

0 comments on commit 2da6bae

Please sign in to comment.