nginx-push-stream模块源码学习(三)——发布
一、概述
发布:发布者将MSG post到某一特定通道上,channel将信息缓存
在说明发布流程之前有必要说明下channel和msg的数据结构。
二、数据结构
2.1 MSG
发布时,模块先将消息转化为ngx_http_push_stream_msg_t的数据结构进行存储。
- // message queue
- typedef struct {
- ngx_queue_t queue; // this MUST be first
- time_t expires;//消息过期时间
- time_t time;//消息创建时间
- ngx_flag_t deleted;//是否已删除
- ngx_int_t id;
- ngx_str_t *raw;//纯文本
- ngx_int_t tag;
- ngx_str_t *event_id;//支持event source
- ngx_str_t *event_id_message;
- ngx_str_t *formatted_messages;//格式化后消息
- ngx_int_t workers_ref_count;//待发送该消息ngx worker计数
- } ngx_http_push_stream_msg_t;
@queue:每个channel会维护一个消息链表,每向channel发布一条消息,channel将其添加到自身的消息链表中。
@expires,@deleted:消息的过期时间,待介绍过订阅流程后,我会整理出一条消息的生产周期,到时会详细阐述该字段的意义。
@raw,@formatted_messages:该模块允许自定义三种消息模板——header模板:当收到订阅请求后发送模板消息;message模板:对消息体格式化;footer模板:断开连接时发送该模板。raw为消息原始内容,后者为应用message模板格式化后的信息
2.2 channel
channel作为发布订阅的中间载体,理解理解它的存储至关重要。
- typedef struct {
- ngx_rbtree_node_t node; // this MUST be first
- ngx_str_t id;
- ngx_uint_t last_message_id;
- time_t last_message_time;
- ngx_int_t last_message_tag;
- ngx_uint_t stored_messages;//# of messages
- ngx_uint_t subscribers;//# of subscribers
- ngx_http_push_stream_pid_queue_t workers_with_subscribers;//处理该channel上订阅者的ngx worker进程链表
- ngx_http_push_stream_msg_t message_queue;//消息链表
- time_t expires;//过期时间
- ngx_flag_t deleted;//是否已删除
- ngx_flag_t broadcast;//是否为广播通道
- ngx_http_push_stream_msg_t *channel_deleted_message;//已删除消息链表
- } ngx_http_push_stream_channel_t;
2.3 worker msg
- // messages to worker processes
- typedef struct {
- ngx_queue_t queue;
- ngx_http_push_stream_msg_t *msg; // ->shared memory
- ngx_pid_t pid;
- ngx_http_push_stream_channel_t *channel; // ->shared memory
- ngx_http_push_stream_queue_elem_t *subscribers_sentinel; // ->a worker's local pool
- } ngx_http_push_stream_worker_msg_t;
模块初始化时为每个ngx worker分配一片独立的工作区,工作区中维护一份消息链表。
三、流程
发布流程总的流程图如图所示:
对于删除channel和获取channel info的流程比较简单,不做阐述,具体说明下发布消息流程,流程图如图所示:

需要说明的是“向所有订阅者发送MSG”的过程:
- 向每个有该channel订阅者的worker(workers_with_subscriber)的消息链表中插入一条消息
- 向上述worker发送CHECK_MESSAGES指令,触发msg发送流程(ngx_http_push_stream_process_worker_message)
MSG发送(ngx_http_push_stream_process_worker_message):
- // now let's respond to some requests!
- //对于该channel上的所有订阅者
- while ((cur = (ngx_http_push_stream_queue_elem_t *) ngx_queue_next(&cur->queue)) != subscribers_sentinel) {
- ngx_http_push_stream_subscriber_t *subscriber = (ngx_http_push_stream_subscriber_t *) cur->value;
- //如果订阅者为longpolling模式
- if (subscriber->longpolling) {
- ngx_http_push_stream_queue_elem_t *prev = (ngx_http_push_stream_queue_elem_t *) ngx_queue_prev(&cur-
- >queue);
- //发送longpolling头(last Modified/Etag)
- ngx_http_push_stream_add_polling_headers(subscriber->request, msg->time, msg->tag, subscriber->reque
- st->pool);
- ngx_http_send_header(subscriber->request);
- //发送模块配置header模板
- ngx_http_push_stream_send_response_content_header(subscriber->request, ngx_http_get_module_loc_conf(
- subscriber->request, ngx_http_push_stream_module));
- //发送响应MSG
- ngx_http_push_stream_send_response_message(subscriber->request, channel, msg);
- //发送footer模板,last chunck("\0"CRLF CRLF)
- ngx_http_push_stream_send_response_finalize(subscriber->request);
- cur = prev;
- } else {//stream或polling模式
- if (ngx_http_push_stream_send_response_message(subscriber->request, channel, msg) == NGX_ERROR) {
- ngx_http_push_stream_queue_elem_t *prev = (ngx_http_push_stream_queue_elem_t *) ngx_queue_prev(&
- cur->queue);
- ngx_http_push_stream_send_response_finalize(subscriber->request);
- cur = prev;
- }
- }
说明:
可以看出push stream模块在发布过程中针对longpolling和stream两种模式的不同:
- Longpolling模式下,每次发布消息时会发送longpolling头:last modified和etag,使得客户端下次请求时可据此判断服务端是否有更新的消息待发布。
- longpolling模式下,订阅者每次请求都会在获得数据后断开重连,所以每次发布时都会发送header模板
- ngx_http_push_stream_send_response_finalize同时会清理订阅者
nginx-push-stream模块源码学习(三)——发布的更多相关文章
- ngx-push-stream模块源码学习(四)——订阅
一.概述 push stream模块允许三种模式的订阅者: longpolling:每收到服务端响应数据即断开连接然后迅速重连,连接耗时可以忽略 stream:与服务端保持长连接,持续不断的请求-&g ...
- nginx-push-stream模块源码学习(二)——模块初始化
本文重点介绍push stream模块的构成,至于nginx如何启动.维护该模块不会详细阐述,以后有时间会做详细阐述. 一.模块定义 1.1. 模块配置 通用nginx模块的配置struct有三种, ...
- nginx健康检查模块源码分析
nginx健康检查模块 本文所说的nginx健康检查模块是指nginx_upstream_check_module模块.nginx_upstream_check_module模块是Taobao定制的用 ...
- mybatis源码学习(三)-一级缓存二级缓存
本文主要是个人学习mybatis缓存的学习笔记,主要有以下几个知识点 1.一级缓存配置信息 2.一级缓存源码学习笔记 3.二级缓存配置信息 4.二级缓存源码 5.一级缓存.二级缓存总结 1.一级缓存配 ...
- Vue源码学习三 ———— Vue构造函数包装
Vue源码学习二 是对Vue的原型对象的包装,最后从Vue的出生文件导出了 Vue这个构造函数 来到 src/core/index.js 代码是: import Vue from './instanc ...
- 05.ElementUI源码学习:项目发布配置(github pages&npm package)
0x00.前言 书接上文.项目第一个组件已经封装好,说明文档也已编写好.下面需要将说明文档发布到外网上,以此来展示和推广项目,使用 Github Pages功能实现.同时将组件发布之 npm 上,方便 ...
- ngx-push-stream模块源码学习(五)——内存清理
1.定时器 采用nginx自身的定时器管理机制,具体细节待学习过nginx源码后加以补充 2.channel的生成周期 (0).初始(诞生) 发布.订阅均有可能产生ch ...
- spring源码学习(三)--spring循环引用源码学习
在spring中,是支持单实例bean的循环引用(循环依赖)的,循环依赖,简单而言,就是A类中注入了B类,B类中注入了A类,首先贴出我的代码示例 @Component public class Add ...
- [spring源码学习]三、IOC源码——自定义配置文件读取
一.环境准备 在文件读取的时候,第9步我们发现spring会根据标签的namespace来选择读取方式,联想spring里提供的各种标签,比如<aop:xxx>等应该会有不同的读取和解析方 ...
随机推荐
- 教你一步一步部署.net免费空间OpenShift系列之四------绑定域名、使用CDN加速
很抱歉这几天没有时间,有人问我怎么绑定域名的问题也没有答复,下面进入正题,惊闻ASP.Net要开源了,难道.Net春天要来了?不废话,上回书说,部署完毕ASP.Net网站后,直接访问不能访问(嗯,众所 ...
- 汽车之家购买价格PC真正的原因阿拉丁
网行业风起云涌,先是6月3号汽车之家天价竞购百度PC阿拉丁.接着今天又有消息说易车拿下百度移动阿拉丁.易车拿下百度移动阿拉丁能够想象.但PC阿拉丁被向来不屑流量购买,以自主流量自居的汽车之家拿 ...
- cfs
转自:http://www.cnblogs.com/openix/p/3254394.html 下文中对于红黑树或链表组织的就绪队列,统称为用队列组织的就绪队列. ...
- iOS开展 - 中国 iOS/Mac 开发博客列表
博客地址 RSS地址 OneV's Den http://onevcat.com/atom.xml 破船之家 http://beyondvincent.com/atom.xml NSHipster h ...
- springbatch操作CSV文件
一.需求分析 使用Spring Batch对CSV文件进行读写操作: 读取一个含有四个字段的CSV文件(id, name, age, score), 对文件做简单的处理, 然后输出到还有一个csv文件 ...
- ASP.NET 5 Hello World
ASP.NET 5系列教程 (二):Hello World 本篇文章内容比较基础,主要是向大家展示如何创建一个 ASP.NET 5 工程,主要包含内容如下: 创建ASP.NET 5 工程 添加 T ...
- flex 用footerdatagrid做列的汇总合计
之前用flex+c#做的一个项目中,有涉及到列的汇总计算.可以用到的方法很多,这里列举了一种,在前台flash中用footerdatagrid结合labelfunction的用法即可实现.当然,下面的 ...
- 专业MySQL数据库管理专家SQL Manager for MySQL发布5.4版本
SQL Manager for MySQL是一款针对MySQL数据库服务器系统的管理工具.深受数据库管理员的喜欢,其富有艺术感的图形用户界面,即使新手也可以不会为如何使用而感到困扰.近日EMSSoft ...
- Inno Setup技巧[界面]欢迎页面上添加文字
原文:Inno Setup技巧[界面]欢迎页面上添加文字 本文介绍在"欢迎页面添加文字"的两种方法. 界面预览: Setup技巧[界面]欢迎页面上添加文字" title= ...
- iOS画面模糊
增加 CoreImage.framework CoreGraphic.framework 等库 在使用时引入:#import <Accelerate/Accelerate.h> ,支持 ...