redis实现的发送订阅系统,即pub-sub,这部分的的代码比较少,也方便分析。在这只将会分析下普通的pub-sub(会忽略掉Pattern-matching subscriptions),以此来简述一个pubsub系统是如何实现的。

在redis主要有介绍redis的pub-sub,在开始之前, 需要知道redis的pubsub的几个命令:

SUBSCRIBE first second //订阅两个channel,分别是first和second

PUBLISH secondHello   //发送方向channel是second的发送"hello"消息

UNSUBSCRIBE      //取消之前订阅的所有channel

下面来看看在redis的Pubsub.c代码中对这些命令的实现。

首先看看subscribe的实现:

voidsubscribeCommand(redisClient *c) {
int j; for (j = 1; j < c->argc; j++)
//客户端订阅多个channel,对于需要订阅的每个channel都执行该函数
pubsubSubscribeChannel(c,c->argv[j]);
} /* Subscribe aclient to a channel. Returns 1 if the operation succeeded, or
* 0 if the client was already subscribed tothat channel. */
intpubsubSubscribeChannel(redisClient *c, robj *channel) {
struct dictEntry *de;
list *clients = NULL;
int retval = 0; //c->pubsub_channels是一个redis自己实现的hash表,这不是我们的重点,就不展开说了
//dictAdd向hash表中增加一个key为channel,value为null的键值对
/* Add the channel to the client ->channels hash table */
if(dictAdd(c->pubsub_channels,channel,NULL) == DICT_OK) {
retval = 1;
incrRefCount(channel);
//从服务端查找当前需要订阅的channel是否已经在服务端登记过
//没有登记过,则创建一个列表作为channel的值,并记录到服务端
//也就是意味着服务端是通过这个列表来得知这个channel是被哪些客户端订阅过了
//登记过,则取出在服务端,channel所对应的客户端列表
/* Add the client to the channel ->list of clients hash table */
de =dictFind(server.pubsub_channels,channel);
if (de == NULL) {
clients = listCreate();
dictAdd(server.pubsub_channels,channel,clients);
incrRefCount(channel);
} else {
clients = dictGetVal(de);
}
//向这个channel所对应的记录客户端的列表的尾部插入当前客户端,
//这样服务端就当前客户端订阅了这个channel
listAddNodeTail(clients,c);
}
/* Notify the client */
addReply(c,shared.mbulkhdr[3]);
addReply(c,shared.subscribebulk);
addReplyBulk(c,channel);
addReplyLongLong(c,dictSize(c->pubsub_channels)+listLength(c->pubsub_patterns));
return retval;
} //在看看pub的实现:
voidpublishCommand(redisClient *c) {
//发送则向某个channel发送消息,如 PUBLISH second Hello
//c->args[1]就是channel second,第二个参数就是要发送的消息Hello
int receivers =pubsubPublishMessage(c->argv[1],c->argv[2]);
addReplyLongLong(c,receivers);
} /*Publish a message */
intpubsubPublishMessage(robj *channel, robj *message) {
int receivers = 0;
struct dictEntry *de;
listNode *ln;
listIter li; //从服务端找到订阅了这个channel的客户端列表
//遍历这个列表,将消息发送给每个客户端
/* Send to clients listening for that channel */
de = dictFind(server.pubsub_channels,channel);
if (de) {
list *list = dictGetVal(de);
listNode *ln;
listIter li; listRewind(list,&li);
while ((ln = listNext(&li)) != NULL) {
redisClient *c = ln->value; addReply(c,shared.mbulkhdr[3]);
addReply(c,shared.messagebulk);
addReplyBulk(c,channel);
addReplyBulk(c,message);
receivers++;
}
}
/* Send to clients listening to matching channels */
发送到模式匹配的订阅方的处理... return receivers;
} //最后看看unsubscribe的处理:
void unsubscribeCommand(redisClient *c) {
if (c->argc == 1) {
//取消这个客户端的所有订阅
pubsubUnsubscribeAllChannels(c,1);
} else {
int j; for (j = 1; j < c->argc; j++)
//取消这个客户端的关于某个channel的订阅
pubsubUnsubscribeChannel(c,c->argv[j],1);
}
}
/* Unsubscribe from all the channels. Return the number of channels the
* client was subscribed from. */
int pubsubUnsubscribeAllChannels(redisClient *c, int notify) {
//取得这个客户端的所有订阅的channel的迭代器
dictIterator *di =dictGetSafeIterator(c->pubsub_channels);
dictEntry *de;
int count = 0;
//通过遍历迭代器来获取在客户端记录的每个channel记录,并对每个记录取消订阅
while((de = dictNext(di)) != NULL) {
robj *channel = dictGetKey(de); count +=pubsubUnsubscribeChannel(c,channel,notify);
}
/* We were subscribed to nothing? Still reply to the client.*/
if (notify && count == 0) {
addReply(c,shared.mbulkhdr[3]);
addReply(c,shared.unsubscribebulk);
addReply(c,shared.nullbulk);
addReplyLongLong(c,dictSize(c->pubsub_channels)+
listLength(c->pubsub_patterns));
}
dictReleaseIterator(di);
return count;
} * Unsubscribe a client from a channel. Returns 1 if the operationsucceeded, or
* 0 if the client was not subscribed to the specified channel. */
int pubsubUnsubscribeChannel(redisClient *c, robj *channel, int notify) {
struct dictEntry *de;
list *clients;
listNode *ln;
int retval = 0; //从客户端记录的hash表中删除这个channel记录,以此来取消客户对这个channel的订阅
//从服务端查找channel所对应客户端列表,从这个列表中删除这个客户端的记录
//这样就完成了取消订阅
/* Remove the channel from the client -> channels hashtable */
incrRefCount(channel); /* channel may be just a pointer tothe same object
we have in the hash tables. Protect it... */
if (dictDelete(c->pubsub_channels,channel) == DICT_OK) {
retval = 1;
/* Remove the client from the channel ->clients list hash table */
de = dictFind(server.pubsub_channels,channel);
redisAssertWithInfo(c,NULL,de != NULL);
clients = dictGetVal(de);
ln = listSearchKey(clients,c);
redisAssertWithInfo(c,NULL,ln != NULL);
listDelNode(clients,ln);
if (listLength(clients) == 0) {
/* Free the list and associatedhash entry at all if this was
* the latest client, sothat it will be possible to abuse
* Redis PUBSUB creatingmillions of channels. */
dictDelete(server.pubsub_channels,channel);
}
}
/* Notify the client */
if (notify) {
addReply(c,shared.mbulkhdr[3]);
addReply(c,shared.unsubscribebulk);
addReplyBulk(c,channel);
addReplyLongLong(c,dictSize(c->pubsub_channels)+
listLength(c->pubsub_patterns)); }
decrRefCount(channel); /* it is finally safe to release it*/
return retval;
}

redis-2.6.16源码分析之pub-sub系统的更多相关文章

  1. 鸿蒙内核源码分析(中断切换篇) | 系统因中断活力四射 | 百篇博客分析OpenHarmony源码 | v42.02

    百篇博客系列篇.本篇为: v42.xx 鸿蒙内核源码分析(中断切换篇) | 系统因中断活力四射 | 51.c.h .o 硬件架构相关篇为: v22.xx 鸿蒙内核源码分析(汇编基础篇) | CPU在哪 ...

  2. Redis学习——ae事件处理源码分析

    0. 前言 Redis在封装事件的处理采用了Reactor模式,添加了定时事件的处理.Redis处理事件是单进程单线程的,而经典Reator模式对事件是串行处理的.即如果有一个事件阻塞过久的话会导致整 ...

  3. 【Redis】事件驱动框架源码分析

    aeEventLoop初始化 在server.c文件的initServer函数中,对aeEventLoop进行了初始化: 调用aeCreateEventLoop函数创建aeEventLoop结构体,对 ...

  4. 【Redis】事件驱动框架源码分析(单线程)

    aeEventLoop初始化 在server.c文件的initServer函数中,对aeEventLoop进行了初始化: 调用aeCreateEventLoop函数创建aeEventLoop结构体,对 ...

  5. Android源码分析(三)-----系统框架设计思想

    一 : 术在内而道在外 Android系统的精髓在源码之外,而不在源码之内,代码只是一种实现人类思想的工具,仅此而已...... 近来发现很多关于Android文章都是以源码的方向入手分析Androi ...

  6. [Abp vNext 源码分析] - 23. 二进制大对象系统(BLOB)

    一.简介 ABP vNext 在 v 2.9.x 版本当中添加了 BLOB 系统,主要用于存储大型二进制文件.ABP 抽象了一套通用的 BLOB 体系,开发人员在存储或读取二进制文件时,可以忽略具体实 ...

  7. JNI-从jvm源码分析Thread.interrupt的系统级别线程打断原理

    前言 在java编程中,我们经常会调用Thread.sleep()方法使得线程停止运行一段时间,而Thread类中也提供了interrupt方法供我们去主动打断一个线程.那么线程挂起和打断的本质究竟是 ...

  8. 【Redis】事件驱动框架源码分析(多线程)

    IO线程初始化 Redis在6.0版本中引入了多线程,提高IO请求处理效率. 在Redis Server启动函数main(server.c文件)中初始化服务之后,又调用了InitServerLast函 ...

  9. 认识 Redis client-output-buffer-limit 参数与源码分析

    概述 Redis 的 client-output-buffer-limit 可以用来强制断开无法足够快从 redis 服务器端读取数据的客户端.保护机制规则如下: [hard limit] 大小限制, ...

  10. Redis学习之SDS源码分析

    一.SDS的简单介绍 SDS:简单动态字符串(simple dynamic string) 1)SDS是Redis默认的字符表示,比如包含字符串值的键值对都是在底层由SDS实现的 2)SDS用来保存数 ...

随机推荐

  1. 64位操作系统下IIS报“试图加载格式不正确的程序”错误

    缘由:在64位操作系统下IIS发布32位的项目,报“项目依赖的dll无法读取,试图加载格式不正确的程序”错误. 原因:程序集之间的通讯要么全是64位环境下的,要么全是32位环境下的.不能混编访问.不然 ...

  2. JS屏蔽右键菜单,复制,粘帖xxxxx........

    //屏蔽右键菜单 document.oncontextmenu = function (event) { if (window.event) { event = window.event; } try ...

  3. linux变量心得

    前一段时间学习了一下linux的变量,现在总结有3点需要特别注意: linux变量和C/C++变量的区别 linux变量的引用 linux变量特有的命令替换 先说第一点,linux变量更像是宏定义,只 ...

  4. SQL SAVE TRANSACTION

    --创建存储过程 create procedure qiantaoProc @asd nchar(10) as begin begin try begin transaction innerTrans ...

  5. angularJS学习笔记一

    AngularJS是为了克服HTML在构建应用上的不足而设计的.HTML是一门很好的为静态文本展示设计的声明式语言,但要构建WEB应用的话它就显得乏力了.所以我做了一些工作(你也可以觉得是小花招)来让 ...

  6. 面向对象设计模式之Interpreter解释器模式(行为型)

    动机:在软件构建过程中 ,如果某一特定领域的问题比较复杂,类似的模式不断重复出现,如果使用普通的编程方式来实现将面临非常频繁的变化.在这种情况下,将特定领域的问题表达为某种语法规则的句子,然后构建一个 ...

  7. TP开发小技巧

    TP开发小技巧原文地址http://wp.chenyuanzhao.com/wp/2016/07/23/tp%E5%BC%80%E5%8F%91%E5%B0%8F%E6%8A%80%E5%B7%A7/ ...

  8. php中字符串编码

    php中抓取网页拼接url的时候经常需要进行编码,这时候就用到两个函数 mb_detect_encoding — 检测字符的编码. mb_convert_encoding — 转换字符的编码 < ...

  9. Django - staticfiles,STATIC_ROOT, STATIC_URL,STATICFILES_DIRS

    staticfiles:这是一个静态资源管理的app,django.contrib.staticfiles.老的版本中,静态资源管理一直是一个问题,部分app发布的时候需要带上静态资源,在部署的时候必 ...

  10. vector list array deque

    因此在实际使用时,如何选择这三个容器中哪一个,应根据你的需要而定,一般应遵循下面 的原则:   1.如果你需要高效的随即存取,而不在乎插入和删除的效率,使用vector   2.如果你需要大量的插入和 ...