标签:需要 进程 关于 data 表示 listen 调用 结构体 结构
本文介绍在nginx中连接资源(即ngx_connection_t)的管理与使用。
在ngx_cycle_t结构体中维护了几个和连接相关的数据,具体如下
struct ngx_cycle_s {
....
ngx_connection_t *free_connections;
ngx_uint_t free_connection_n;
ngx_uint_t connection_n;
ngx_connection_t *connections;
ngx_event_t *read_events;
ngx_event_t *write_events;
}
逐一说明一下
关于free链结构可以参考ngx_event_process_init()中的代码
c = cycle->connections;
i = cycle->connection_n;
next = NULL;
do {
i--;
c[i].data = next;
c[i].read = &cycle->read_events[i];
c[i].write = &cycle->write_events[i];
c[i].fd = (ngx_socket_t) -1;
next = &c[i];
} while (i);
cycle->free_connections = next;
cycle->free_connection_n = cycle->connection_n;
连接的申请与释放就是对cycle->free_connections的操作,相关的函数有2个ngx_get_connection()与ngx_free_connection().核心的逻辑可以参考代码
ngx_connection_t *
ngx_get_connection(ngx_socket_t s, ngx_log_t *log)
{
...
c = ngx_cycle->free_connections;
ngx_cycle->free_connections = c->data;
ngx_cycle->free_connection_n--;
...
}
void
ngx_free_connection(ngx_connection_t *c)
{
c->data = ngx_cycle->free_connections;
ngx_cycle->free_connections = c;
ngx_cycle->free_connection_n++;
...
}
通过查看ngx_get_connection()与的引用,可以看到连接的主要使用场景
标签:需要 进程 关于 data 表示 listen 调用 结构体 结构
原文地址:https://www.cnblogs.com/atskyline/p/11221968.html