漫话Redis源码之八十二

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

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

看最后的kqueue, 这是为了兼容不同平台,其实你可以理解为它和select/poll/epoll差不多就行:

#include <sys/types.h>
#include <sys/event.h>
#include <sys/time.h>typedef struct aeApiState {int kqfd;struct kevent *events;/* Events mask for merge read and write event.* To reduce memory consumption, we use 2 bits to store the mask* of an event, so that 1 byte will store the mask of 4 events. */char *eventsMask; 
} aeApiState;#define EVENT_MASK_MALLOC_SIZE(sz) (((sz) + 3) / 4)
#define EVENT_MASK_OFFSET(fd) ((fd) % 4 * 2)
#define EVENT_MASK_ENCODE(fd, mask) (((mask) & 0x3) << EVENT_MASK_OFFSET(fd))static inline int getEventMask(const char *eventsMask, int fd) {return (eventsMask[fd/4] >> EVENT_MASK_OFFSET(fd)) & 0x3;
}static inline void addEventMask(char *eventsMask, int fd, int mask) {eventsMask[fd/4] |= EVENT_MASK_ENCODE(fd, mask);
}static inline void resetEventMask(char *eventsMask, int fd) {eventsMask[fd/4] &= ~EVENT_MASK_ENCODE(fd, 0x3);
}static int aeApiCreate(aeEventLoop *eventLoop) {aeApiState *state = zmalloc(sizeof(aeApiState));if (!state) return -1;state->events = zmalloc(sizeof(struct kevent)*eventLoop->setsize);if (!state->events) {zfree(state);return -1;}state->kqfd = kqueue();if (state->kqfd == -1) {zfree(state->events);zfree(state);return -1;}anetCloexec(state->kqfd);state->eventsMask = zmalloc(EVENT_MASK_MALLOC_SIZE(eventLoop->setsize));memset(state->eventsMask, 0, EVENT_MASK_MALLOC_SIZE(eventLoop->setsize));eventLoop->apidata = state;return 0;
}static int aeApiResize(aeEventLoop *eventLoop, int setsize) {aeApiState *state = eventLoop->apidata;state->events = zrealloc(state->events, sizeof(struct kevent)*setsize);state->eventsMask = zrealloc(state->eventsMask, EVENT_MASK_MALLOC_SIZE(setsize));memset(state->eventsMask, 0, EVENT_MASK_MALLOC_SIZE(setsize));return 0;
}static void aeApiFree(aeEventLoop *eventLoop) {aeApiState *state = eventLoop->apidata;close(state->kqfd);zfree(state->events);zfree(state->eventsMask);zfree(state);
}static int aeApiAddEvent(aeEventLoop *eventLoop, int fd, int mask) {aeApiState *state = eventLoop->apidata;struct kevent ke;if (mask & AE_READABLE) {EV_SET(&ke, fd, EVFILT_READ, EV_ADD, 0, 0, NULL);if (kevent(state->kqfd, &ke, 1, NULL, 0, NULL) == -1) return -1;}if (mask & AE_WRITABLE) {EV_SET(&ke, fd, EVFILT_WRITE, EV_ADD, 0, 0, NULL);if (kevent(state->kqfd, &ke, 1, NULL, 0, NULL) == -1) return -1;}return 0;
}static void aeApiDelEvent(aeEventLoop *eventLoop, int fd, int mask) {aeApiState *state = eventLoop->apidata;struct kevent ke;if (mask & AE_READABLE) {EV_SET(&ke, fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);kevent(state->kqfd, &ke, 1, NULL, 0, NULL);}if (mask & AE_WRITABLE) {EV_SET(&ke, fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);kevent(state->kqfd, &ke, 1, NULL, 0, NULL);}
}static int aeApiPoll(aeEventLoop *eventLoop, struct timeval *tvp) {aeApiState *state = eventLoop->apidata;int retval, numevents = 0;if (tvp != NULL) {struct timespec timeout;timeout.tv_sec = tvp->tv_sec;timeout.tv_nsec = tvp->tv_usec * 1000;retval = kevent(state->kqfd, NULL, 0, state->events, eventLoop->setsize,&timeout);} else {retval = kevent(state->kqfd, NULL, 0, state->events, eventLoop->setsize,NULL);}if (retval > 0) {int j;/* Normally we execute the read event first and then the write event.* When the barrier is set, we will do it reverse.* * However, under kqueue, read and write events would be separate* events, which would make it impossible to control the order of* reads and writes. So we store the event's mask we've got and merge* the same fd events later. */for (j = 0; j < retval; j++) {struct kevent *e = state->events+j;int fd = e->ident;int mask = 0; if (e->filter == EVFILT_READ) mask = AE_READABLE;else if (e->filter == EVFILT_WRITE) mask = AE_WRITABLE;addEventMask(state->eventsMask, fd, mask);}/* Re-traversal to merge read and write events, and set the fd's mask to* 0 so that events are not added again when the fd is encountered again. */numevents = 0;for (j = 0; j < retval; j++) {struct kevent *e = state->events+j;int fd = e->ident;int mask = getEventMask(state->eventsMask, fd);if (mask) {eventLoop->fired[numevents].fd = fd;eventLoop->fired[numevents].mask = mask;resetEventMask(state->eventsMask, fd);numevents++;}}}return numevents;
}static char *aeApiName(void) {return "kqueue";
}

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



http://www.chinasem.cn/article/683880

相关文章

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

Redis分片集群的实现

《Redis分片集群的实现》Redis分片集群是一种将Redis数据库分散到多个节点上的方式,以提供更高的性能和可伸缩性,本文主要介绍了Redis分片集群的实现,具有一定的参考价值,感兴趣的可以了解一... 目录1. Redis Cluster的核心概念哈希槽(Hash Slots)主从复制与故障转移2.

Python实现无痛修改第三方库源码的方法详解

《Python实现无痛修改第三方库源码的方法详解》很多时候,我们下载的第三方库是不会有需求不满足的情况,但也有极少的情况,第三方库没有兼顾到需求,本文将介绍几个修改源码的操作,大家可以根据需求进行选择... 目录需求不符合模拟示例 1. 修改源文件2. 继承修改3. 猴子补丁4. 追踪局部变量需求不符合很

Redis 中的热点键和数据倾斜示例详解

《Redis中的热点键和数据倾斜示例详解》热点键是指在Redis中被频繁访问的特定键,这些键由于其高访问频率,可能导致Redis服务器的性能问题,尤其是在高并发场景下,本文给大家介绍Redis中的热... 目录Redis 中的热点键和数据倾斜热点键(Hot Key)定义特点应对策略示例数据倾斜(Data S

redis+lua实现分布式限流的示例

《redis+lua实现分布式限流的示例》本文主要介绍了redis+lua实现分布式限流的示例,可以实现复杂的限流逻辑,如滑动窗口限流,并且避免了多步操作导致的并发问题,具有一定的参考价值,感兴趣的可... 目录为什么使用Redis+Lua实现分布式限流使用ZSET也可以实现限流,为什么选择lua的方式实现

Redis中管道操作pipeline的实现

《Redis中管道操作pipeline的实现》RedisPipeline是一种优化客户端与服务器通信的技术,通过批量发送和接收命令减少网络往返次数,提高命令执行效率,本文就来介绍一下Redis中管道操... 目录什么是pipeline场景一:我要向Redis新增大批量的数据分批处理事务( MULTI/EXE

Redis中高并发读写性能的深度解析与优化

《Redis中高并发读写性能的深度解析与优化》Redis作为一款高性能的内存数据库,广泛应用于缓存、消息队列、实时统计等场景,本文将深入探讨Redis的读写并发能力,感兴趣的小伙伴可以了解下... 目录引言一、Redis 并发能力概述1.1 Redis 的读写性能1.2 影响 Redis 并发能力的因素二、

Redis中的常用的五种数据类型详解

《Redis中的常用的五种数据类型详解》:本文主要介绍Redis中的常用的五种数据类型详解,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Redis常用的五种数据类型一、字符串(String)简介常用命令应用场景二、哈希(Hash)简介常用命令应用场景三、列表(L

Redis解决缓存击穿问题的两种方法

《Redis解决缓存击穿问题的两种方法》缓存击穿问题也叫热点Key问题,就是⼀个被高并发访问并且缓存重建业务较复杂的key突然失效了,无数的请求访问会在瞬间给数据库带来巨大的冲击,本文给大家介绍了Re... 目录引言解决办法互斥锁(强一致,性能差)逻辑过期(高可用,性能优)设计逻辑过期时间引言缓存击穿:给

Redis中如何实现商品秒杀

《Redis中如何实现商品秒杀》:本文主要介绍Redis中如何实现商品秒杀问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录技术栈功能实现步骤步骤一:准备商品库存数据步骤二:实现商品秒杀步骤三:优化Redis性能技术讲解Redis的List类型Redis的Set