设为首页 收藏本站
查看: 1067|回复: 0

[经验分享] nginx-push-stream模块源码学习(二)——模块初始化

[复制链接]

尚未签到

发表于 2016-12-27 10:32:05 | 显示全部楼层 |阅读模式
本文重点介绍push stream模块的构成,至于nginx如何启动、维护该模块不会详细阐述,以后有时间会做详细阐述。
一、模块定义
1.1.  模块配置

        通用nginx模块的配置struct有三种,分别是main,server和location。本模块会涉及到main和location两个域的配置。名称分别为:ngx_http_push_stream_main_conf_t和ngx_http_push_stream_loc_conf_t.  
       具体模块配置请参考nginx官网:http://wiki.nginx.org/HttpPushStreamModule
1.2.  模块指令
  模块的指令是定义在一个叫做ngx_command_t的静态数组中的,或用于在nginx配置文件中设定模块的相关参数或处理相应请求,先简单来说下ngx_command_t的定义:

struct ngx_command_t {
ngx_str_t             name;//指令名称
ngx_uint_t            type;//指令类型——该指令可用于ngx conf的哪个域——main?server?location?
/*命令所对应的处理函数指针,参数
@指向结构体 ngx_conf_t 的指针, 这个结构体里包含需要传递给指令的参数;
@指向结构体 ngx_command_t 的指针;
@指向模块自定义配置结构体的指针
*/
char               *(*set)(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
ngx_uint_t            conf;//指定参数存储区域(main?server?loc?)
ngx_uint_t            offset;//指定数据保存位置
void                 *post;//指向模块在读配置的时候需要的一些零碎变量
};

比如:

{ ngx_string("push_stream_publisher"),
NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS|NGX_CONF_TAKE1,//指令用在location域,没有参数或1个参数
ngx_http_push_stream_publisher,//发布处理函数
NGX_HTTP_LOC_CONF_OFFSET,//参数存储在location域
offsetof(ngx_http_push_stream_loc_conf_t, location_type),
NULL },
{ ngx_string("push_stream_subscriber"),
NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS|NGX_CONF_TAKE1,
ngx_http_push_stream_subscriber,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_push_stream_loc_conf_t, location_type),
NULL },

1.3.  模块上下文
       静态的ngx_http_module_t结构体,包含一大坨函数引用,用来创建和合并三段配置 (main,server,location):

static ngx_http_module_t    ngx_http_push_stream_module_ctx = {
NULL,                                       /* preconfiguration */
ngx_http_push_stream_postconfig,            /* postconfiguration */
ngx_http_push_stream_create_main_conf,      /* create main configuration */
ngx_http_push_stream_init_main_conf,        /* init main configuration */
NULL,                                       /* create server configuration */
NULL,                                       /* merge server configuration */
ngx_http_push_stream_create_loc_conf,       /* create location configuration */
ngx_http_push_stream_merge_loc_conf,        /* merge location configuration */
};


1.4.  模块定义

ngx_module_t    ngx_http_push_stream_module = {
NGX_MODULE_V1,
&ngx_http_push_stream_module_ctx,           /* module context */
ngx_http_push_stream_commands,              /* module directives */
NGX_HTTP_MODULE,                            /* module type */
NULL,                                       /* init master */
ngx_http_push_stream_init_module,           /* init module */
ngx_http_push_stream_init_worker,           /* init process */
NULL,                                       /* init thread */
NULL,                                       /* exit thread */
ngx_http_push_stream_exit_worker,           /* exit process */
ngx_http_push_stream_exit_master,           /* exit master */
NGX_MODULE_V1_PADDING
};

二、初始化
        由上文可以看出,nginx初始化时会调用ngx_http_push_stream_init_module和ngx_http_push_stream_init_worker两个函数,下面看下它们都干了什么
2.1.  模块初始化(init_module)

static ngx_int_t
ngx_http_push_stream_init_module(ngx_cycle_t *cycle)
{
ngx_core_conf_t                         *ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module
);
if ((ngx_http_push_stream_module_main_conf == NULL) || !ngx_http_push_stream_module_main_conf->enabled) {
ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "ngx_http_push_stream_module will not be used with this configu
ration.");
return NGX_OK;
}
// initialize our little IPC
return ngx_http_push_stream_init_ipc(cycle, ccf->worker_processes);
}

代码很简单

  • 获取conf
  • 初始化IPC

2.1.1 IPC
       该模块的IPC是对nginx的master与worker间IPC的扩展,有关nginx的进程间通信请参见http://simohayha.iyunv.com/blog/467940
       简单来讲,master与worker直接的通信模型为:

  • master每次创建worker之前,创建一个channel(socketpair),fork后worker继承该socketpair
  • master保留所有worker的socketpair从而可以向所有worker发送控制指令。
  • worker继承socketpair时,仅保留master为自己创建的socketpair的读端以及master为其他worker创建的socketpair的写端,从而可实现worker间的进程间通信
  • 由于worker创建时序的不同,先创建的worker无法获悉后创建worker的chaneel信息,为此master每次创建channel后以将新创建的channel信息以指令的形式通知先前创建的worker

       由于master为worker创建的channel(socketpair)只处理特定的指令,push stream模块创建了供自己使用的socketpair:

  • 模块初始化时为所有worker创建一个socketpair
  • master创建worker时,worker会完全继承由模块创建的socketpair读端与写端。
  • worker可借助由push stream模块创建的socketpair实现全双工通信

2.  ngx worker初始化(ngx_http_push_stream_init_worker)
DSC0000.png

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-320118-1-1.html 上篇帖子: 一台nginx带多个域名多个tomcat情况的配置 下篇帖子: MongoDB GridFS介绍与nginx-gridfs编译问题
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表