Nginx中封装的数据结构

Nginx中封装的数据结构

Nginx中封装的数据结构

整型

复制代码
typedef intptr_t        ngx_int_t;
typedef uintptr_t       ngx_uint_t;

ngx_str_t【字符串】

c 复制代码
typedef struct {
    size_t      len; // 表示字符串的有效长度
    u_char     *data;// 表示字符串起始地址
} ngx_str_t;

ngx_list_t【链表】

c 复制代码
typedef struct ngx_list_part_s  ngx_list_part_t;// 表示链表中的一个元素
struct ngx_list_part_s {
    void             *elts; // 指向数组的起始位置
    ngx_uint_t        nelts;//  表示数组中已经使用了多少个元素
    ngx_list_part_t  *next;// 下一个链表元素的地址
};

// 存储数组的链表
typedef struct {
    ngx_list_part_t  *last; // 指向链表的最后一个元素
    ngx_list_part_t   part; // 指向链表的第一个元素
    size_t            size; // 限制存储值的大小
    ngx_uint_t        nalloc; // 每个ngx_list_part_t数组的容量
    ngx_pool_t       *pool; // 链表中管理内存分配的内存池对象
} ngx_list_t;

图解如下:

ngx_table_elt_t【key/value】

c 复制代码
typedef struct ngx_table_elt_s  ngx_table_elt_t;

struct ngx_table_elt_s {
    ngx_uint_t        hash; // 用于快速检索头部
    ngx_str_t         key; // 名字
    ngx_str_t         value; // 值
    u_char           *lowcase_key; // key全是小写
    ngx_table_elt_t  *next; // 
};

用于存储HTTP头部

ngx_buf_t

c 复制代码
typedef void *            ngx_buf_tag_t;

typedef struct ngx_buf_s  ngx_buf_t;

    u_char          *pos;  // 从这个位置开始处理内存中的数据
    u_char          *last; // 表示有效的内容的结束位置
    off_t            file_pos;  //  要处理文件的位置
    off_t            file_last; //  要处理文件的截止位置

    u_char          *start;         /* start of buffer */  // 要处理内存的起始地址
    u_char          *end;           /* end of buffer */    // 要处理内存的末尾地址
    ngx_buf_tag_t    tag;  // 缓冲区的类型,例如由哪个模块使用就指向这个模块变量的地址
    ngx_file_t      *file;  // 引用的文件类型
    ngx_buf_t       *shadow;


    /* the buf's content could be changed */
    unsigned         temporary:1; // 临时内存标记位,为1时表示数据在内存中且这段内存可以修改

    /*
     * the buf's content is in a memory cache or in a read only memory
     * and must not be changed
     */
    unsigned         memory:1; // 标志位,为1时表示数据在内存中且这段内存不可以被修改

    /* the buf's content is mmap()ed and must not be changed */
    unsigned         mmap:1; // 标志位,为1时表示这段内存是用mmap系统调用映射过来的,不可以被修改

    unsigned         recycled:1; // 标志位, 为1表示可回收
    unsigned         in_file:1; // 标志位,为1表示这段缓冲区处理的是文件而不是内存
    unsigned         flush:1; // 标志位,为1时表示需要执行flush操作
    unsigned         sync:1; // 标志位,对操作这块缓冲区是否使用同步方式,
    unsigned         last_buf:1; // 标志位,表示是否是最后一块缓冲区,因为ngx_buf_t可以由ngx_chain_t链表串联起来
    unsigned         last_in_chain:1; // 标志位,表示是否是ngx_chain_t中的最后一块缓冲区

    unsigned         last_shadow:1; // 标志位,表示是否为最后一个影子缓冲区
    unsigned         temp_file:1; // 标志位,表示当前缓冲区是否属于临时文件

    /* STUB */ int   num;
};

缓冲区,处理大数据,既用于存储内存数据,也用于存储磁盘数据。

ngx_chain_t

复制代码
typedef struct ngx_chain_s           ngx_chain_t;

struct ngx_chain_s {
    ngx_buf_t    *buf; // 指向当前的ngx_buf_t缓冲区
    ngx_chain_t  *next; // 指向下一个ngx_chain_t,如果是最后一个,则为NULL
};

在向用户发送HTTP包体时,就要传入ngx_chain_t链表对象,注意,如果是最后一个ngx_chain_t,那么必须将next置为NULL,否则永远不会发送成功,而且这个请求将一直不会结束

相关推荐
_殊途1 小时前
《Java HashMap底层原理全解析(源码+性能+面试)》
java·数据结构·算法
core5124 小时前
prometheus+grafana接入nginx实战
nginx·grafana·prometheus·监控·接入·vts·vtx
saynaihe4 小时前
ubuntu 22.04 anaconda comfyui安装
linux·运维·服务器·ubuntu
企鹅与蟒蛇4 小时前
Ubuntu-25.04 Wayland桌面环境安装Anaconda3之后无法启动anaconda-navigator问题解决
linux·运维·python·ubuntu·anaconda
小蜜蜂爱编程4 小时前
ubuntu透网方案
运维·服务器·ubuntu
AI视觉网奇4 小时前
git 访问 github
运维·开发语言·docker
头发那是一根不剩了5 小时前
nginx:SSL_CTX_use_PrivateKey failed
运维·服务器
秋说5 小时前
【PTA数据结构 | C语言版】顺序队列的3个操作
c语言·数据结构·算法
七夜zippoe5 小时前
破解 VMware 迁移难题:跨平台迁移常见问题及自动化解决方案
运维·自动化·vmware
lifallen6 小时前
Kafka 时间轮深度解析:如何O(1)处理定时任务
java·数据结构·分布式·后端·算法·kafka