Skip to content

Commit 24c81e5

Browse files
author
wangshaoyi
committed
fix connections overflow when command processing takes too long and client close connection
1 parent 7cd72f8 commit 24c81e5

3 files changed

Lines changed: 20 additions & 3 deletions

File tree

src/net/include/net_define.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ enum EventStatus {
4848
kNone = 0,
4949
kReadable = 0x1,
5050
kWritable = 0x1 << 1,
51-
kErrorEvent = 0x1 << 2,
51+
kPeerClose = 0x1 << 2,
52+
kErrorEvent = 0x1 << 3,
5253
};
5354

5455
enum ConnStatus {

src/net/src/net_epoll.cc

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
#include "net/include/net_define.h"
1515
#include "pstd/include/xdebug.h"
1616

17+
#ifndef EPOLLRDHUP
18+
#define EPOLLRDHUP 0x2000
19+
#endif
20+
1721
namespace net {
1822

1923
NetMultiplexer* CreateNetMultiplexer(int limit) { return new NetEpoll(limit); }
@@ -45,7 +49,7 @@ int NetEpoll::NetAddEvent(int fd, int mask) {
4549
}
4650
if (mask & kWritable) {
4751
ee.events |= EPOLLOUT;
48-
}
52+
}
4953

5054
return epoll_ctl(multiplexer_, EPOLL_CTL_ADD, fd, &ee);
5155
}
@@ -62,6 +66,10 @@ int NetEpoll::NetModEvent(int fd, int old_mask, int mask) {
6266
if ((old_mask | mask) & kWritable) {
6367
ee.events |= EPOLLOUT;
6468
}
69+
70+
if ((old_mask | mask) & kPeerClose) {
71+
ee.events |= EPOLLRDHUP;
72+
}
6573
return epoll_ctl(multiplexer_, EPOLL_CTL_MOD, fd, &ee);
6674
}
6775

@@ -93,6 +101,10 @@ int NetEpoll::NetPoll(int timeout) {
93101
ev.mask |= kWritable;
94102
}
95103

104+
if (events_[i].events & EPOLLRDHUP) {
105+
ev.mask |= kPeerClose;
106+
}
107+
96108
if (events_[i].events & (EPOLLERR | EPOLLHUP)) {
97109
ev.mask |= kErrorEvent;
98110
}

src/net/src/worker_thread.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ void* WorkerThread::ThreadMain() {
189189
ReadStatus read_status = in_conn->GetRequest();
190190
in_conn->set_last_interaction(now);
191191
if (read_status == kReadAll) {
192-
net_multiplexer_->NetModEvent(pfe->fd, 0, 0);
192+
net_multiplexer_->NetModEvent(pfe->fd, 0, kPeerClose);
193193
// Wait for the conn complete asynchronous task and
194194
// Mod Event to kWritable
195195
} else if (read_status == kReadHalf) {
@@ -199,6 +199,10 @@ void* WorkerThread::ThreadMain() {
199199
}
200200
}
201201

202+
if ((should_close == 0) && ((pfe->mask & kPeerClose) != 0)) {
203+
should_close = 1;
204+
}
205+
202206
if (((pfe->mask & kErrorEvent) != 0) || (should_close != 0)) {
203207
net_multiplexer_->NetDelEvent(pfe->fd, 0);
204208
CloseFd(in_conn);

0 commit comments

Comments
 (0)