Design of the connection pool for high concurrent server design

Source: Internet
Author: User

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;  
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.