refactor(session manager): replace dep/list/list.h with sys/queue.h

This commit is contained in:
luwenpeng
2024-09-13 15:44:46 +08:00
parent 244cc88ace
commit e0ecee73b3
5 changed files with 42 additions and 50 deletions

View File

@@ -26,7 +26,7 @@ struct snowflake
struct session_manager struct session_manager
{ {
struct list_head evicte_queue; struct session_list evicte_list;
struct session_pool *sess_pool; struct session_pool *sess_pool;
struct session_timer *sess_timer; struct session_timer *sess_timer;
struct session_table *tcp_sess_table; struct session_table *tcp_sess_table;
@@ -550,7 +550,7 @@ static void session_manager_evicte_session(struct session_manager *mgr, struct s
} }
} }
session_timer_del(mgr->sess_timer, sess); session_timer_del(mgr->sess_timer, sess);
list_add_tail(&sess->evicte, &mgr->evicte_queue); TAILQ_INSERT_TAIL(&mgr->evicte_list, sess, evicte_tqe);
switch (session_get_type(sess)) switch (session_get_type(sess))
{ {
@@ -977,7 +977,7 @@ struct session_manager *session_manager_new(const struct session_manager_config
goto error; goto error;
} }
INIT_LIST_HEAD(&mgr->evicte_queue); TAILQ_INIT(&mgr->evicte_list);
session_transition_init(); session_transition_init();
mgr->now_ms = now_ms; mgr->now_ms = now_ms;
mgr->last_clean_expired_sess_ts = now_ms; mgr->last_clean_expired_sess_ts = now_ms;
@@ -995,10 +995,9 @@ void session_manager_free(struct session_manager *mgr)
if (mgr) if (mgr)
{ {
// free all evicted session // free all evicted session
while (!list_empty(&mgr->evicte_queue)) while ((sess = TAILQ_FIRST(&mgr->evicte_list)))
{ {
sess = list_first_entry(&mgr->evicte_queue, struct session, evicte); TAILQ_REMOVE(&mgr->evicte_list, sess, evicte_tqe);
list_del(&sess->evicte);
session_manager_free_session(mgr, sess); session_manager_free_session(mgr, sess);
} }
// free all udp session // free all udp session
@@ -1234,17 +1233,13 @@ struct session *session_manager_get_expired_session(struct session_manager *mgr,
struct session *session_manager_get_evicted_session(struct session_manager *mgr) struct session *session_manager_get_evicted_session(struct session_manager *mgr)
{ {
if (list_empty(&mgr->evicte_queue)) struct session *sess = TAILQ_FIRST(&mgr->evicte_list);
if (sess)
{ {
return NULL; TAILQ_REMOVE(&mgr->evicte_list, sess, evicte_tqe);
} }
else
{
struct session *sess = list_first_entry(&mgr->evicte_queue, struct session, evicte);
list_del(&sess->evicte);
return sess; return sess;
} }
}
// array_size at least EVICTE_SESSION_BURST, suggest 2 * EVICTE_SESSION_BURST // array_size at least EVICTE_SESSION_BURST, suggest 2 * EVICTE_SESSION_BURST
uint64_t session_manager_clean_session(struct session_manager *mgr, uint64_t now_ms, struct session *cleaned_sess[], uint64_t array_size) uint64_t session_manager_clean_session(struct session_manager *mgr, uint64_t now_ms, struct session *cleaned_sess[], uint64_t array_size)

View File

@@ -7,7 +7,7 @@ struct session_pool
{ {
uint64_t capacity; uint64_t capacity;
uint64_t available; uint64_t available;
struct list_head free_queue; struct session_list free_list;
}; };
struct session_pool *session_pool_new(uint64_t capacity) struct session_pool *session_pool_new(uint64_t capacity)
@@ -19,13 +19,13 @@ struct session_pool *session_pool_new(uint64_t capacity)
} }
pool->available = 0; pool->available = 0;
pool->capacity = capacity; pool->capacity = capacity;
INIT_LIST_HEAD(&pool->free_queue); TAILQ_INIT(&pool->free_list);
struct session *array = (struct session *)(pool + 1); struct session *array = (struct session *)(pool + 1);
for (uint64_t i = 0; i < capacity; i++) for (uint64_t i = 0; i < capacity; i++)
{ {
struct session *sess = &array[i]; struct session *sess = &array[i];
list_add_tail(&sess->free, &pool->free_queue); TAILQ_INSERT_TAIL(&pool->free_list, sess, free_tqe);
pool->available++; pool->available++;
} }
@@ -36,10 +36,10 @@ void session_pool_free(struct session_pool *pool)
{ {
if (pool) if (pool)
{ {
while (!list_empty(&pool->free_queue)) struct session *sess;
while ((sess = TAILQ_FIRST(&pool->free_list)))
{ {
struct session *sess = list_first_entry(&pool->free_queue, struct session, free); TAILQ_REMOVE(&pool->free_list, sess, free_tqe);
list_del(&sess->free);
pool->available--; pool->available--;
} }
@@ -55,14 +55,12 @@ struct session *session_pool_pop(struct session_pool *pool)
return NULL; return NULL;
} }
if (list_empty(&pool->free_queue)) struct session *sess = TAILQ_FIRST(&pool->free_list);
if (sess)
{ {
return NULL; TAILQ_REMOVE(&pool->free_list, sess, free_tqe);
}
struct session *sess = list_first_entry(&pool->free_queue, struct session, free);
list_del(&sess->free);
pool->available--; pool->available--;
}
return sess; return sess;
} }
@@ -74,7 +72,7 @@ void session_pool_push(struct session_pool *pool, struct session *sess)
return; return;
} }
list_add_tail(&sess->free, &pool->free_queue); TAILQ_INSERT_TAIL(&pool->free_list, sess, free_tqe);
pool->available++; pool->available++;
} }

View File

@@ -7,7 +7,6 @@ extern "C"
#include <stdbool.h> #include <stdbool.h>
#include "list.h"
#include "tuple.h" #include "tuple.h"
#include "uthash.h" #include "uthash.h"
#include "timeout.h" #include "timeout.h"
@@ -51,9 +50,9 @@ struct session
uint64_t timestamps[MAX_TIMESTAMP]; // realtime msec uint64_t timestamps[MAX_TIMESTAMP]; // realtime msec
struct tcp_half tcp_halfs[MAX_FLOW_TYPE]; struct tcp_half tcp_halfs[MAX_FLOW_TYPE];
struct timeout timeout; struct timeout timeout;
struct list_head lru; TAILQ_ENTRY(session) lru_tqe;
struct list_head free; TAILQ_ENTRY(session) free_tqe;
struct list_head evicte; TAILQ_ENTRY(session) evicte_tqe;
UT_hash_handle hh1; UT_hash_handle hh1;
UT_hash_handle hh2; UT_hash_handle hh2;
UT_hash_handle hh3; UT_hash_handle hh3;
@@ -76,6 +75,8 @@ struct session
struct session_manager_stat *mgr_stat; struct session_manager_stat *mgr_stat;
}; };
TAILQ_HEAD(session_list, session);
void session_init(struct session *sess); void session_init(struct session *sess);
void session_set_id(struct session *sess, uint64_t id); void session_set_id(struct session *sess, uint64_t id);

View File

@@ -15,7 +15,7 @@ struct session_table
void *arg; void *arg;
uint64_t count; uint64_t count;
struct list_head lru_queue; struct session_list lru_list;
}; };
/****************************************************************************** /******************************************************************************
@@ -93,7 +93,7 @@ struct session_table *session_table_new()
} }
table->count = 0; table->count = 0;
INIT_LIST_HEAD(&table->lru_queue); TAILQ_INIT(&table->lru_list);
return table; return table;
} }
@@ -106,7 +106,7 @@ void session_table_free(struct session_table *table)
struct session *tmp = NULL; struct session *tmp = NULL;
HASH_ITER(hh1, table->root_tuple6, node, tmp) HASH_ITER(hh1, table->root_tuple6, node, tmp)
{ {
list_del(&node->lru); TAILQ_REMOVE(&table->lru_list, node, lru_tqe);
HASH_DELETE(hh1, table->root_tuple6, node); HASH_DELETE(hh1, table->root_tuple6, node);
HASH_DELETE(hh2, table->root_tuple4, node); HASH_DELETE(hh2, table->root_tuple4, node);
HASH_DELETE(hh3, table->root_sessid, node); HASH_DELETE(hh3, table->root_sessid, node);
@@ -151,7 +151,7 @@ void session_table_add(struct session_table *table, struct session *sess)
HASH_ADD(hh1, table->root_tuple6, tuple, sizeof(struct tuple6), sess); HASH_ADD(hh1, table->root_tuple6, tuple, sizeof(struct tuple6), sess);
HASH_ADD(hh2, table->root_tuple4, tuple, sizeof(struct tuple4), sess); HASH_ADD(hh2, table->root_tuple4, tuple, sizeof(struct tuple4), sess);
HASH_ADD(hh3, table->root_sessid, id, sizeof(uint64_t), sess); HASH_ADD(hh3, table->root_sessid, id, sizeof(uint64_t), sess);
list_add_tail(&sess->lru, &table->lru_queue); TAILQ_INSERT_TAIL(&table->lru_list, sess, lru_tqe);
table->count++; table->count++;
} }
@@ -162,10 +162,10 @@ void session_table_del(struct session_table *table, struct session *sess)
return; return;
} }
list_del(&sess->lru);
HASH_DELETE(hh1, table->root_tuple6, sess); HASH_DELETE(hh1, table->root_tuple6, sess);
HASH_DELETE(hh2, table->root_tuple4, sess); HASH_DELETE(hh2, table->root_tuple4, sess);
HASH_DELETE(hh3, table->root_sessid, sess); HASH_DELETE(hh3, table->root_sessid, sess);
TAILQ_REMOVE(&table->lru_list, sess, lru_tqe);
if (table->free_cb) if (table->free_cb)
{ {
table->free_cb(sess, table->arg); table->free_cb(sess, table->arg);
@@ -184,8 +184,8 @@ struct session *session_table_find_sessid(struct session_table *table, uint64_t
HASH_FIND(hh3, table->root_sessid, &id, sizeof(uint64_t), sess); HASH_FIND(hh3, table->root_sessid, &id, sizeof(uint64_t), sess);
if (sess && !quiet) if (sess && !quiet)
{ {
list_del(&sess->lru); TAILQ_REMOVE(&table->lru_list, sess, lru_tqe);
list_add_tail(&sess->lru, &table->lru_queue); TAILQ_INSERT_TAIL(&table->lru_list, sess, lru_tqe);
} }
return sess; return sess;
@@ -202,8 +202,8 @@ struct session *session_table_find_tuple6(struct session_table *table, const str
HASH_FIND(hh1, table->root_tuple6, tuple, sizeof(struct tuple6), sess); HASH_FIND(hh1, table->root_tuple6, tuple, sizeof(struct tuple6), sess);
if (sess && !quiet) if (sess && !quiet)
{ {
list_del(&sess->lru); TAILQ_REMOVE(&table->lru_list, sess, lru_tqe);
list_add_tail(&sess->lru, &table->lru_queue); TAILQ_INSERT_TAIL(&table->lru_list, sess, lru_tqe);
} }
return sess; return sess;
@@ -220,8 +220,8 @@ struct session *session_table_find_tuple4(struct session_table *table, const str
HASH_FIND(hh2, table->root_tuple4, tuple, sizeof(struct tuple4), sess); HASH_FIND(hh2, table->root_tuple4, tuple, sizeof(struct tuple4), sess);
if (sess && !quiet) if (sess && !quiet)
{ {
list_del(&sess->lru); TAILQ_REMOVE(&table->lru_list, sess, lru_tqe);
list_add_tail(&sess->lru, &table->lru_queue); TAILQ_INSERT_TAIL(&table->lru_list, sess, lru_tqe);
} }
return sess; return sess;
@@ -234,12 +234,5 @@ struct session *session_table_find_lru(struct session_table *table)
return NULL; return NULL;
} }
if (list_empty(&table->lru_queue)) return TAILQ_FIRST(&table->lru_list);
{
return NULL;
}
else
{
return list_first_entry(&table->lru_queue, struct session, lru);
}
} }

View File

@@ -1,6 +1,11 @@
#include "session_private.h" #include "session_private.h"
#include "session_timer.h" #include "session_timer.h"
#ifndef container_of
#define container_of(ptr, type, member) \
(type *)((char *)(ptr) - (char *)&((type *)0)->member)
#endif
struct session_timer struct session_timer
{ {
struct timeouts *timeouts; struct timeouts *timeouts;