nginx连接池:
每个进程有自己独立的连接池,连接数配置:worker_connections(当然这个连接数受限于进程ulimit open files ;连接数还包括upstream建立的连接)
连接池实际上是一个数组单链表,初始化是在ngx_event_process_init中进行的,包括读写事件链表。
连接池(cycle->connections)是分配在连续的n个ngx_connection_t空间里数组,每个ngx_connection_t的
data指向相邻的下一个ngx_connection_t。cycle->free_connections初始化指向了cycle->connections的链表头元素。
ngx_reusable_connection
ngx_drain_connections
每个进程有自己独立的连接池,连接数配置:worker_connections(当然这个连接数受限于进程ulimit open files ;连接数还包括upstream建立的连接)
连接池实际上是一个数组单链表,初始化是在ngx_event_process_init中进行的,包括读写事件链表。
连接池(cycle->connections)是分配在连续的n个ngx_connection_t空间里数组,每个ngx_connection_t的
data指向相邻的下一个ngx_connection_t。cycle->free_connections初始化指向了cycle->connections的链表头元素。
实现思考点:一是单链表足够用了,不需要队列或者栈,每次操作都是链表的首元素;二是这是一个数组,支持
ngx_get_connection
从free_connections获取一个connection,然后初始化
ngx_close_connection
主要关闭一个connection,包括“善后”以及调用ngx_free_connection来将连接放回free_connections
ngx_free_connection
将使用的连接放回free_connections
长连接:
长链接使用一个queue保存的,先进先出。在连接不够用的时候,要释放一定数量(32个)的长连接
ngx_reusable_connection
ngx_reusable_connection(ngx_connection_t *c, ngx_uint_t reusable)
reusable=1 ,放进queue中
reusable=0 ,从queue中出来
ngx_drain_connections
当ngx_get_connection获取不到连接时(即并发比较高的时候,连接都用完了),那么使用
ngx_drain_connections来释放长连接,将长连接从queue拿出来,放回到free_connections,然后再获取