2
0
Fork 0

optimize ram usage by freeing unused connections

This commit is contained in:
Thomas Porzelt 2009-03-28 03:37:41 +01:00
parent 97db5dab4e
commit ab9a616f5d
2 changed files with 21 additions and 1 deletions

View File

@ -62,6 +62,7 @@ struct worker {
* use with atomic, read direct from local worker context
*/
GArray *connections; /** array of (connection*), use only from local worker context */
ev_tstamp connections_gc_ts;
GQueue closing_sockets; /** wait for EOF before shutdown(SHUT_RD) and close() */

View File

@ -470,15 +470,17 @@ static connection* worker_con_get(worker *wrk) {
}
void worker_con_put(connection *con) {
guint threshold;
worker *wrk = con->wrk;
ev_tstamp now = CUR_TS(wrk);
if (con->state == CON_STATE_DEAD)
/* already disconnected */
return;
connection_reset(con);
g_atomic_int_add((gint*) &wrk->connection_load, -1);
g_atomic_int_add((gint*) &wrk->connections_active, -1);
if (con->idx != wrk->connections_active) {
/* Swap [con->idx] and [wrk->connections_active] */
connection *tmp;
@ -489,4 +491,21 @@ void worker_con_put(connection *con) {
g_array_index(wrk->connections, connection*, con->idx) = con;
g_array_index(wrk->connections, connection*, tmp->idx) = tmp;
}
/* realloc wrk->connections if it makes sense (too many allocated, only every 60sec) */
/* if (active < allocated*0.70) { allocated *= 0.85 } */
threshold = (wrk->connections->len * 7) / 10;
if (wrk->connections_active < threshold && (now - wrk->connections_gc_ts) < 60.0 && wrk->connections->len > 10) {
/* realloc */
guint i;
threshold = (wrk->connections->len * 85) / 100;
for (i = wrk->connections->len; i > threshold; i--) {
connection_free(g_array_index(wrk->connections, connection*, i-1));
}
wrk->connections->len = threshold;
wrk->connections_gc_ts = now;
} else {
/* no realloc */
connection_reset(con);
}
}