High concurrent servers require some design of pools, such as memory pools, connection pooling, and database connection pooling.
Pool is designed to take into account the frequent application and release of some resources, especially in highly concurrent servers, tens of thousands of or even hundreds of thousands of concurrent per second, which designers have to consider.
For example, the database connection pool (SQL Pool) is communicated through TCP, belongs to IO class, has certain delay, in the high concurrent system frequently creates can seriously affect the system performance.
Memory (MEM) allocation is involved in the lock (mutex), there will be a lock delay, so you can start to request a large chunk of memory, after the allocation and release, to save the lock overhead.
Server connection processing involves not only memory, but also some properties of the assignment, these are to occupy CPU time, if the start of a large number of connections, it is convenient to reuse later.
Let me take the database connection pool as an example to first define the structure of the connection:
typedef struct TST_SQL_S tst_sql_t;
struct tst_sql_s{
MYSQL *sql;
tst_sql_t *next;
tst_sql_t *prev;
};
In real-world development, I've found that some prefer to design pools using (Free-busi) patterns.
struct
tst_sql_pool_s
{
tst_sql_t *free_sql;
tst_sql_t *busi_sql;
...
};
Divide the connections in the pool into two parts, some of which are free, some are in use (busi), and phase functions:
tst_sql_t* Tst_sql_pool_get (tst_sql_pool_t* pool)
{
tst_sql_t *sql;
if (!pool) {return
0;
}
sql = pool->free_sql;
if (!sql) {return
0;
}
Pool->free_sql = sql->next;
Sql->next = pool->busi_sql;
Sql->prev = 0;
if (pool->busi_sql) {
pool->busi_sql->prev = SQL;
}
pool->busi_sql = SQL;
return SQL;
}
int Tst_sql_pool_put (tst_sql_pool_t* pool, tst_sql_t* sql)
{
if (!pool | |!sql) {return
0;
}
if (Sql->prev) {
sql->prev->next = sql->next;
}
else{
pool->busi_sql = sql->next;
}
if (sql->next) {
Sql->next->prev = sql->prev;
}
Sql->next = pool->free_sql;
pool->free_sql = SQL;
return 0;
}