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,否则永远不会发送成功,而且这个请求将一直不会结束

相关推荐
LH_R9 小时前
OneTerm开源堡垒机实战(四):访问授权与安全管控
运维·后端·安全
Raymond运维9 小时前
MariaDB源码编译安装(二)
运维·数据库·mariadb
JuiceFS1 天前
从 MLPerf Storage v2.0 看 AI 训练中的存储性能与扩展能力
运维·后端
chen9451 天前
mysql 3节点mgr集群部署
运维·后端
LH_R1 天前
OneTerm开源堡垒机实战(三):功能扩展与效率提升
运维·后端·安全
dessler1 天前
Hadoop HDFS-高可用集群部署
linux·运维·hdfs
少妇的美梦2 天前
logstash教程
运维
chen9452 天前
k8s集群部署vector日志采集器
运维
chen9452 天前
aws ec2部署harbor,使用s3存储
运维
Fanxt_Ja2 天前
【LeetCode】算法详解#15 ---环形链表II
数据结构·算法·leetcode·链表