feature: session pool support forearch session by index
This commit is contained in:
@@ -5,25 +5,28 @@
|
|||||||
|
|
||||||
struct session_pool
|
struct session_pool
|
||||||
{
|
{
|
||||||
uint64_t count;
|
uint64_t capacity;
|
||||||
|
uint64_t available;
|
||||||
struct list_head free_queue;
|
struct list_head free_queue;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct session_pool *session_pool_new(uint64_t count)
|
struct session_pool *session_pool_new(uint64_t capacity)
|
||||||
{
|
{
|
||||||
struct session_pool *pool = (struct session_pool *)calloc(1, sizeof(struct session_pool) + count * sizeof(struct session));
|
struct session_pool *pool = (struct session_pool *)calloc(1, sizeof(struct session_pool) + capacity * sizeof(struct session));
|
||||||
if (pool == NULL)
|
if (pool == NULL)
|
||||||
{
|
{
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
pool->available = 0;
|
||||||
|
pool->capacity = capacity;
|
||||||
INIT_LIST_HEAD(&pool->free_queue);
|
INIT_LIST_HEAD(&pool->free_queue);
|
||||||
|
|
||||||
struct session *array = (struct session *)(pool + 1);
|
struct session *array = (struct session *)(pool + 1);
|
||||||
for (uint64_t i = 0; i < count; 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);
|
list_add_tail(&sess->free, &pool->free_queue);
|
||||||
pool->count++;
|
pool->available++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return pool;
|
return pool;
|
||||||
@@ -37,7 +40,7 @@ void session_pool_free(struct session_pool *pool)
|
|||||||
{
|
{
|
||||||
struct session *sess = list_first_entry(&pool->free_queue, struct session, free);
|
struct session *sess = list_first_entry(&pool->free_queue, struct session, free);
|
||||||
list_del(&sess->free);
|
list_del(&sess->free);
|
||||||
pool->count--;
|
pool->available--;
|
||||||
}
|
}
|
||||||
|
|
||||||
free(pool);
|
free(pool);
|
||||||
@@ -59,7 +62,7 @@ struct session *session_pool_pop(struct session_pool *pool)
|
|||||||
|
|
||||||
struct session *sess = list_first_entry(&pool->free_queue, struct session, free);
|
struct session *sess = list_first_entry(&pool->free_queue, struct session, free);
|
||||||
list_del(&sess->free);
|
list_del(&sess->free);
|
||||||
pool->count--;
|
pool->available--;
|
||||||
|
|
||||||
return sess;
|
return sess;
|
||||||
}
|
}
|
||||||
@@ -72,15 +75,36 @@ void session_pool_push(struct session_pool *pool, struct session *sess)
|
|||||||
}
|
}
|
||||||
|
|
||||||
list_add_tail(&sess->free, &pool->free_queue);
|
list_add_tail(&sess->free, &pool->free_queue);
|
||||||
pool->count++;
|
pool->available++;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t session_pool_get_count(struct session_pool *pool)
|
const struct session *session_pool_get0(const struct session_pool *pool, uint64_t idx)
|
||||||
|
{
|
||||||
|
if (pool == NULL || idx >= pool->capacity)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct session *array = (struct session *)(pool + 1);
|
||||||
|
return &array[idx];
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t session_pool_available_num(const struct session_pool *pool)
|
||||||
{
|
{
|
||||||
if (pool == NULL)
|
if (pool == NULL)
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return pool->count;
|
return pool->available;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint64_t session_pool_capacity_size(const struct session_pool *pool)
|
||||||
|
{
|
||||||
|
if (pool == NULL)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return pool->capacity;
|
||||||
|
}
|
||||||
|
|||||||
@@ -8,12 +8,15 @@ extern "C"
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
struct session_pool;
|
struct session_pool;
|
||||||
struct session_pool *session_pool_new(uint64_t count);
|
struct session_pool *session_pool_new(uint64_t capacity);
|
||||||
void session_pool_free(struct session_pool *pool);
|
void session_pool_free(struct session_pool *pool);
|
||||||
|
|
||||||
struct session *session_pool_pop(struct session_pool *pool);
|
struct session *session_pool_pop(struct session_pool *pool);
|
||||||
void session_pool_push(struct session_pool *pool, struct session *sess);
|
void session_pool_push(struct session_pool *pool, struct session *sess);
|
||||||
uint64_t session_pool_get_count(struct session_pool *pool);
|
const struct session *session_pool_get0(const struct session_pool *pool, uint64_t idx);
|
||||||
|
|
||||||
|
uint64_t session_pool_available_num(const struct session_pool *pool);
|
||||||
|
uint64_t session_pool_capacity_size(const struct session_pool *pool);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,45 +12,46 @@ TEST(SESSION_POOL, POP_PUSH)
|
|||||||
|
|
||||||
sess_pool = session_pool_new(3);
|
sess_pool = session_pool_new(3);
|
||||||
EXPECT_TRUE(sess_pool != NULL);
|
EXPECT_TRUE(sess_pool != NULL);
|
||||||
EXPECT_TRUE(session_pool_get_count(sess_pool) == 3);
|
EXPECT_TRUE(session_pool_available_num(sess_pool) == 3);
|
||||||
|
EXPECT_TRUE(session_pool_capacity_size(sess_pool) == 3);
|
||||||
|
|
||||||
sess1 = session_pool_pop(sess_pool);
|
sess1 = session_pool_pop(sess_pool);
|
||||||
EXPECT_TRUE(sess1 != NULL);
|
EXPECT_TRUE(sess1 != NULL);
|
||||||
EXPECT_TRUE(session_pool_get_count(sess_pool) == 2);
|
EXPECT_TRUE(session_pool_available_num(sess_pool) == 2);
|
||||||
sess2 = session_pool_pop(sess_pool);
|
sess2 = session_pool_pop(sess_pool);
|
||||||
EXPECT_TRUE(sess2 != NULL);
|
EXPECT_TRUE(sess2 != NULL);
|
||||||
EXPECT_TRUE(session_pool_get_count(sess_pool) == 1);
|
EXPECT_TRUE(session_pool_available_num(sess_pool) == 1);
|
||||||
sess3 = session_pool_pop(sess_pool);
|
sess3 = session_pool_pop(sess_pool);
|
||||||
EXPECT_TRUE(sess3 != NULL);
|
EXPECT_TRUE(sess3 != NULL);
|
||||||
EXPECT_TRUE(session_pool_get_count(sess_pool) == 0);
|
EXPECT_TRUE(session_pool_available_num(sess_pool) == 0);
|
||||||
sess4 = session_pool_pop(sess_pool);
|
sess4 = session_pool_pop(sess_pool);
|
||||||
EXPECT_TRUE(sess4 == NULL);
|
EXPECT_TRUE(sess4 == NULL);
|
||||||
|
|
||||||
session_pool_push(sess_pool, sess1);
|
session_pool_push(sess_pool, sess1);
|
||||||
EXPECT_TRUE(session_pool_get_count(sess_pool) == 1);
|
EXPECT_TRUE(session_pool_available_num(sess_pool) == 1);
|
||||||
session_pool_push(sess_pool, sess2);
|
session_pool_push(sess_pool, sess2);
|
||||||
EXPECT_TRUE(session_pool_get_count(sess_pool) == 2);
|
EXPECT_TRUE(session_pool_available_num(sess_pool) == 2);
|
||||||
session_pool_push(sess_pool, sess3);
|
session_pool_push(sess_pool, sess3);
|
||||||
EXPECT_TRUE(session_pool_get_count(sess_pool) == 3);
|
EXPECT_TRUE(session_pool_available_num(sess_pool) == 3);
|
||||||
|
|
||||||
sess1 = session_pool_pop(sess_pool);
|
sess1 = session_pool_pop(sess_pool);
|
||||||
EXPECT_TRUE(sess1 != NULL);
|
EXPECT_TRUE(sess1 != NULL);
|
||||||
EXPECT_TRUE(session_pool_get_count(sess_pool) == 2);
|
EXPECT_TRUE(session_pool_available_num(sess_pool) == 2);
|
||||||
sess2 = session_pool_pop(sess_pool);
|
sess2 = session_pool_pop(sess_pool);
|
||||||
EXPECT_TRUE(sess2 != NULL);
|
EXPECT_TRUE(sess2 != NULL);
|
||||||
EXPECT_TRUE(session_pool_get_count(sess_pool) == 1);
|
EXPECT_TRUE(session_pool_available_num(sess_pool) == 1);
|
||||||
sess3 = session_pool_pop(sess_pool);
|
sess3 = session_pool_pop(sess_pool);
|
||||||
EXPECT_TRUE(sess3 != NULL);
|
EXPECT_TRUE(sess3 != NULL);
|
||||||
EXPECT_TRUE(session_pool_get_count(sess_pool) == 0);
|
EXPECT_TRUE(session_pool_available_num(sess_pool) == 0);
|
||||||
sess4 = session_pool_pop(sess_pool);
|
sess4 = session_pool_pop(sess_pool);
|
||||||
EXPECT_TRUE(sess4 == NULL);
|
EXPECT_TRUE(sess4 == NULL);
|
||||||
|
|
||||||
session_pool_push(sess_pool, sess1);
|
session_pool_push(sess_pool, sess1);
|
||||||
EXPECT_TRUE(session_pool_get_count(sess_pool) == 1);
|
EXPECT_TRUE(session_pool_available_num(sess_pool) == 1);
|
||||||
session_pool_push(sess_pool, sess2);
|
session_pool_push(sess_pool, sess2);
|
||||||
EXPECT_TRUE(session_pool_get_count(sess_pool) == 2);
|
EXPECT_TRUE(session_pool_available_num(sess_pool) == 2);
|
||||||
session_pool_push(sess_pool, sess3);
|
session_pool_push(sess_pool, sess3);
|
||||||
EXPECT_TRUE(session_pool_get_count(sess_pool) == 3);
|
EXPECT_TRUE(session_pool_available_num(sess_pool) == 3);
|
||||||
|
|
||||||
session_pool_free(sess_pool);
|
session_pool_free(sess_pool);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user