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

相关推荐
龙龙博客2 小时前
LVS+Keepalived 实现高可用负载均衡
运维·负载均衡·lvs
职略2 小时前
负载均衡类型和算法解析
java·运维·分布式·算法·负载均衡
阿里巴巴P8资深技术专家3 小时前
Java常用算法&集合扩容机制分析
java·数据结构·算法
Iᴛ's ᴅᴇsᴛɪɴʏ²⁸.3 小时前
Nginx反向代理和负载均衡
运维·nginx·负载均衡
cui_win5 小时前
nginx-限制客户端并发数
运维·nginx·限流·limit_conn·限制并发
King's King6 小时前
自动化立体仓库出入库能力及堆垛机节拍
运维·自动化
一颗星的征途6 小时前
宝塔-Linux模板常用命令-centos7
linux·运维·服务器
逸群不凡6 小时前
C++|哈希应用->布隆过滤器
开发语言·数据结构·c++·算法·哈希算法
yumuing blog6 小时前
【论文解读】AGENTLESS:揭开基于LLM的软件工程代理的神秘面纱,重塑软件工程自动化新基线
运维·自动化·软件工程·llama
Elastic 中国社区官方博客7 小时前
Elasticsearch:Painless scripting 语言(一)
大数据·运维·elasticsearch·搜索引擎·全文检索