漫话Redis源码之八十三

2024-02-06 09:38
文章标签 源码 redis 漫话 八十三

本文主要是介绍漫话Redis源码之八十三,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

触发并掩码掉给定的fd:

static int aeApiLookupPending(aeApiState *state, int fd) {uint_t i;for (i = 0; i < state->npending; i++) {if (state->pending_fds[i] == fd)return (i);}return (-1);
}/** Helper function to invoke port_associate for the given fd and mask.*/
static int aeApiAssociate(const char *where, int portfd, int fd, int mask) {int events = 0;int rv, err;if (mask & AE_READABLE)events |= POLLIN;if (mask & AE_WRITABLE)events |= POLLOUT;if (evport_debug)fprintf(stderr, "%s: port_associate(%d, 0x%x) = ", where, fd, events);rv = port_associate(portfd, PORT_SOURCE_FD, fd, events,(void *)(uintptr_t)mask);err = errno;if (evport_debug)fprintf(stderr, "%d (%s)\n", rv, rv == 0 ? "no error" : strerror(err));if (rv == -1) {fprintf(stderr, "%s: port_associate: %s\n", where, strerror(err));if (err == EAGAIN)fprintf(stderr, "aeApiAssociate: event port limit exceeded.");}return rv;
}static int aeApiAddEvent(aeEventLoop *eventLoop, int fd, int mask) {aeApiState *state = eventLoop->apidata;int fullmask, pfd;if (evport_debug)fprintf(stderr, "aeApiAddEvent: fd %d mask 0x%x\n", fd, mask);/** Since port_associate's "events" argument replaces any existing events, we* must be sure to include whatever events are already associated when* we call port_associate() again.*/fullmask = mask | eventLoop->events[fd].mask;pfd = aeApiLookupPending(state, fd);if (pfd != -1) {/** This fd was recently returned from aeApiPoll.  It should be safe to* assume that the consumer has processed that poll event, but we play* it safer by simply updating pending_mask.  The fd will be* re-associated as usual when aeApiPoll is called again.*/if (evport_debug)fprintf(stderr, "aeApiAddEvent: adding to pending fd %d\n", fd);state->pending_masks[pfd] |= fullmask;return 0;}return (aeApiAssociate("aeApiAddEvent", state->portfd, fd, fullmask));
}static void aeApiDelEvent(aeEventLoop *eventLoop, int fd, int mask) {aeApiState *state = eventLoop->apidata;int fullmask, pfd;if (evport_debug)fprintf(stderr, "del fd %d mask 0x%x\n", fd, mask);pfd = aeApiLookupPending(state, fd);if (pfd != -1) {if (evport_debug)fprintf(stderr, "deleting event from pending fd %d\n", fd);/** This fd was just returned from aeApiPoll, so it's not currently* associated with the port.  All we need to do is update* pending_mask appropriately.*/state->pending_masks[pfd] &= ~mask;if (state->pending_masks[pfd] == AE_NONE)state->pending_fds[pfd] = -1;return;}/** The fd is currently associated with the port.  Like with the add case* above, we must look at the full mask for the file descriptor before* updating that association.  We don't have a good way of knowing what the* events are without looking into the eventLoop state directly.  We rely on* the fact that our caller has already updated the mask in the eventLoop.*/fullmask = eventLoop->events[fd].mask;if (fullmask == AE_NONE) {/** We're removing *all* events, so use port_dissociate to remove the* association completely.  Failure here indicates a bug.*/if (evport_debug)fprintf(stderr, "aeApiDelEvent: port_dissociate(%d)\n", fd);if (port_dissociate(state->portfd, PORT_SOURCE_FD, fd) != 0) {perror("aeApiDelEvent: port_dissociate");abort(); /* will not return */}} else if (aeApiAssociate("aeApiDelEvent", state->portfd, fd,fullmask) != 0) {/** ENOMEM is a potentially transient condition, but the kernel won't* generally return it unless things are really bad.  EAGAIN indicates* we've reached a resource limit, for which it doesn't make sense to* retry (counter-intuitively).  All other errors indicate a bug.  In any* of these cases, the best we can do is to abort.*/abort(); /* will not return */}
}static int aeApiPoll(aeEventLoop *eventLoop, struct timeval *tvp) {aeApiState *state = eventLoop->apidata;struct timespec timeout, *tsp;uint_t mask, i;uint_t nevents;port_event_t event[MAX_EVENT_BATCHSZ];/** If we've returned fd events before, we must re-associate them with the* port now, before calling port_get().  See the block comment at the top of* this file for an explanation of why.*/for (i = 0; i < state->npending; i++) {if (state->pending_fds[i] == -1)/* This fd has since been deleted. */continue;if (aeApiAssociate("aeApiPoll", state->portfd,state->pending_fds[i], state->pending_masks[i]) != 0) {/* See aeApiDelEvent for why this case is fatal. */abort();}state->pending_masks[i] = AE_NONE;state->pending_fds[i] = -1;}state->npending = 0;if (tvp != NULL) {timeout.tv_sec = tvp->tv_sec;timeout.tv_nsec = tvp->tv_usec * 1000;tsp = &timeout;} else {tsp = NULL;}/** port_getn can return with errno == ETIME having returned some events (!).* So if we get ETIME, we check nevents, too.*/nevents = 1;if (port_getn(state->portfd, event, MAX_EVENT_BATCHSZ, &nevents,tsp) == -1 && (errno != ETIME || nevents == 0)) {if (errno == ETIME || errno == EINTR)return 0;/* Any other error indicates a bug. */perror("aeApiPoll: port_get");abort();}state->npending = nevents;for (i = 0; i < nevents; i++) {mask = 0;if (event[i].portev_events & POLLIN)mask |= AE_READABLE;if (event[i].portev_events & POLLOUT)mask |= AE_WRITABLE;eventLoop->fired[i].fd = event[i].portev_object;eventLoop->fired[i].mask = mask;if (evport_debug)fprintf(stderr, "aeApiPoll: fd %d mask 0x%x\n",(int)event[i].portev_object, mask);state->pending_fds[i] = event[i].portev_object;state->pending_masks[i] = (uintptr_t)event[i].portev_user;}return nevents;
}static char *aeApiName(void) {return "evport";
}

这篇关于漫话Redis源码之八十三的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/683881

相关文章

Redis指南及6.2.x版本安装过程

《Redis指南及6.2.x版本安装过程》Redis是完全开源免费的,遵守BSD协议,是一个高性能(NOSQL)的key-value数据库,Redis是一个开源的使用ANSIC语言编写、支持网络、... 目录概述Redis特点Redis应用场景缓存缓存分布式会话分布式锁社交网络最新列表Redis各版本介绍旧

Java如何从Redis中批量读取数据

《Java如何从Redis中批量读取数据》:本文主要介绍Java如何从Redis中批量读取数据的情况,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一.背景概述二.分析与实现三.发现问题与屡次改进3.1.QPS过高而且波动很大3.2.程序中断,抛异常3.3.内存消

Redis中的Lettuce使用详解

《Redis中的Lettuce使用详解》Lettuce是一个高级的、线程安全的Redis客户端,用于与Redis数据库交互,Lettuce是一个功能强大、使用方便的Redis客户端,适用于各种规模的J... 目录简介特点连接池连接池特点连接池管理连接池优势连接池配置参数监控常用监控工具通过JMX监控通过Pr

python操作redis基础

《python操作redis基础》Redis(RemoteDictionaryServer)是一个开源的、基于内存的键值对(Key-Value)存储系统,它通常用作数据库、缓存和消息代理,这篇文章... 目录1. Redis 简介2. 前提条件3. 安装 python Redis 客户端库4. 连接到 Re

Redis迷你版微信抢红包实战

《Redis迷你版微信抢红包实战》本文主要介绍了Redis迷你版微信抢红包实战... 目录1 思路分析1.1hCckRX 流程1.2 注意点①拆红包:二倍均值算法②发红包:list③抢红包&记录:hset2 代码实现2.1 拆红包splitRedPacket2.2 发红包sendRedPacket2.3 抢

Golang实现Redis分布式锁(Lua脚本+可重入+自动续期)

《Golang实现Redis分布式锁(Lua脚本+可重入+自动续期)》本文主要介绍了Golang分布式锁实现,采用Redis+Lua脚本确保原子性,持可重入和自动续期,用于防止超卖及重复下单,具有一定... 目录1 概念应用场景分布式锁必备特性2 思路分析宕机与过期防止误删keyLua保证原子性可重入锁自动

8种快速易用的Python Matplotlib数据可视化方法汇总(附源码)

《8种快速易用的PythonMatplotlib数据可视化方法汇总(附源码)》你是否曾经面对一堆复杂的数据,却不知道如何让它们变得直观易懂?别慌,Python的Matplotlib库是你数据可视化的... 目录引言1. 折线图(Line Plot)——趋势分析2. 柱状图(Bar Chart)——对比分析3

Spring Boot 整合 Redis 实现数据缓存案例详解

《SpringBoot整合Redis实现数据缓存案例详解》Springboot缓存,默认使用的是ConcurrentMap的方式来实现的,然而我们在项目中并不会这么使用,本文介绍SpringB... 目录1.添加 Maven 依赖2.配置Redis属性3.创建 redisCacheManager4.使用Sp

redis在spring boot中异常退出的问题解决方案

《redis在springboot中异常退出的问题解决方案》:本文主要介绍redis在springboot中异常退出的问题解决方案,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴... 目录问题:解决 问题根源️ 解决方案1. 异步处理 + 提前ACK(关键步骤)2. 调整Redis消费者组

springboot项目redis缓存异常实战案例详解(提供解决方案)

《springboot项目redis缓存异常实战案例详解(提供解决方案)》redis基本上是高并发场景上会用到的一个高性能的key-value数据库,属于nosql类型,一般用作于缓存,一般是结合数据... 目录缓存异常实践案例缓存穿透问题缓存击穿问题(其中也解决了穿透问题)完整代码缓存异常实践案例Red