Skip to content

Commit

Permalink
Ensure event cache handle properly (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
baigao-X authored Jan 4, 2024
1 parent 0dbf6c8 commit e6a32b7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
26 changes: 25 additions & 1 deletion src/Poller/EventPoller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ int EventPoller::addEvent(int fd, int event, PollEventCB cb) {
}
#endif
auto record = std::make_shared<Poll_Record>();
record->fd = fd;

This comment has been minimized.

Copy link
@xia-chu

xia-chu Jan 4, 2024

Member

这个新增的fd变量 没用 何不删除?

This comment has been minimized.

Copy link
@baigao-X

baigao-X Jan 4, 2024

Author Contributor

添加这个的目的是为了 作为_event_cache_expired_map的key值。

record->event = event;
record->call_back = std::move(cb);
_event_map.emplace(fd, record);
Expand All @@ -146,10 +147,12 @@ int EventPoller::delEvent(int fd, PollCompleteCB cb) {
if (isCurrentThread()) {
#if defined(HAS_EPOLL)
bool success = epoll_ctl(_epoll_fd, EPOLL_CTL_DEL, fd, nullptr) == 0 && _event_map.erase(fd) > 0;
refreshEventCache(fd);
cb(success);
return success ? 0 : -1;
#else
cb(_event_map.erase(fd));
refreshEventCache(fd);

This comment has been minimized.

Copy link
@xia-chu

xia-chu Jan 4, 2024

Member

感觉 改成这样比较好

bool success = _event_map.erase(fd);
if (success) {
   refreshEventCache(fd);
}
cb(success);
return 0;
#endif //HAS_EPOLL

Expand Down Expand Up @@ -303,9 +306,17 @@ void EventPoller::runLoop(bool blocked, bool ref_self) {
//超时或被打断
continue;
}

_event_cache_expired_map.clear();

for (int i = 0; i < ret; ++i) {
struct epoll_event &ev = events[i];
int fd = ev.data.fd;
if (_event_cache_expired_map[fd]) {

This comment has been minimized.

Copy link
@xia-chu

xia-chu Jan 4, 2024

Member

这里还是感觉欠妥 如果事件比较多 会导致_event_cache_expired_map频繁插入
可能会导致hash桶不停的扩容拷贝, 改成这样比较合理:

if (_event_cache_expired_map.find(fd) == _event_cache_expired_map.end()) 
//event cache refresh
continue;
}

auto it = _event_map.find(fd);
if (it == _event_map.end()) {
epoll_ctl(_epoll_fd, EPOLL_CTL_DEL, fd, nullptr);
Expand Down Expand Up @@ -357,6 +368,9 @@ void EventPoller::runLoop(bool blocked, bool ref_self) {
//超时或被打断
continue;
}

_event_cache_expired_map.clear();

//收集select事件类型
for (auto &pr : _event_map) {
int event = 0;
Expand All @@ -375,7 +389,12 @@ void EventPoller::runLoop(bool blocked, bool ref_self) {
}
}

callback_list.for_each([](Poll_Record::Ptr &record) {
callback_list.for_each([this](Poll_Record::Ptr &record) {
if (this->_event_cache_expired_map[record->fd]) {

This comment has been minimized.

Copy link
@xia-chu

xia-chu Jan 4, 2024

Member

这里同理

//event cache refresh
return;
}

try {
record->call_back(record->attach);
} catch (std::exception &ex) {
Expand Down Expand Up @@ -445,6 +464,11 @@ EventPoller::DelayTask::Ptr EventPoller::doDelayTask(uint64_t delay_ms, function
return ret;
}

void EventPoller::refreshEventCache(int fd) {
_event_cache_expired_map[fd] = true;
return;
}

///////////////////////////////////////////////

static size_t s_pool_size = 0;
Expand Down
9 changes: 8 additions & 1 deletion src/Poller/EventPoller.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ class EventPoller : public TaskExecutor, public AnyStorage, public std::enable_s
*/
void addEventPipe();

/**
* 删除过时的事件缓存
*/
void refreshEventCache(int fd);

private:
class ExitException : public std::exception {};

Expand Down Expand Up @@ -210,12 +215,14 @@ class EventPoller : public TaskExecutor, public AnyStorage, public std::enable_s
//select相关
struct Poll_Record {
using Ptr = std::shared_ptr<Poll_Record>;
int fd;
int event;
int attach;
PollEventCB call_back;
};
unordered_map<int, Poll_Record::Ptr> _event_map;
#endif //HAS_EPOLL
unordered_map<int, bool> _event_cache_expired_map;

//定时器相关
std::multimap<uint64_t, DelayTask::Ptr> _delay_task_map;
Expand Down Expand Up @@ -277,4 +284,4 @@ class EventPollerPool : public std::enable_shared_from_this<EventPollerPool>, pu
};

} // namespace toolkit
#endif /* EventPoller_h */
#endif /* EventPoller_h */

0 comments on commit e6a32b7

Please sign in to comment.