Redis 笔记七 监视器

Redis 客户端通过执行 MONITOR命令可以将自己变为一个监视器,实时地接受并打印出服务器当前处理的命令请求的相关信息。
此时,当其他客户端向服务器发送一条命令请求时,服务器除了会处理这条命令请求之外,还会将这条 命令请求的信息发送给所有监视器
实现监视器
redisServer 维护一个 monitors 的链表,记录自己的监视器,每次收到 MONITOR 命令之后,将客户 端追加到链表尾。
void monitorCommand(redisClient *c) { 
    /* ignore MONITOR if already slave or in monitor mode */ 
    if (c->flags & REDIS_SLAVE) return;
        c->flags |= (REDIS_SLAVE|REDIS_MONITOR); 
        listAddNodeTail(server.monitors,c); 
        addReply(c,shared.ok); //回复OK 
}
向监视器发送命令信息
利用 call 函数实现向监视器发送命令
// call() 函数是执行命令的核心函数,这里只看监视器部分 
/*src/redis.c/call*/ 
/* Call() is the core of Redis execution of a command */ 
void call(redisClient *c, int flags) { 
    long long dirty, start = ustime(), duration; 
    int client_old_flags = c->flags; 
    /* Sent the command to clients in MONITOR mode, only if the commands are * not generated from reading an AOF. */ 
    if (listLength(server.monitors) && !server.loading && !(c->cmd->flags & REDIS_CMD_SKIP_MONITOR)) {
        replicationFeedMonitors(c,server.monitors,c->db->id,c->argv,c->argc); 
    }
    ...... 
}
call 主要调用了 replicationFeedMonitors ,这个函数的作用就是将命令打包为协议,发送给监视 器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值