From b1a2deb7ee1a81f2cab5861c828a4510b5b12c2d Mon Sep 17 00:00:00 2001 From: lesismal Date: Tue, 21 Sep 2021 12:32:46 +0800 Subject: [PATCH] fix epoll mod op --- poller_epoll.go | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/poller_epoll.go b/poller_epoll.go index b6ee2b8a..c5c96f86 100644 --- a/poller_epoll.go +++ b/poller_epoll.go @@ -24,10 +24,15 @@ const ( ) const ( - epoollEventsRead = syscall.EPOLLPRI | syscall.EPOLLIN - epoollEventsWrite = syscall.EPOLLOUT - epoollEventsReadWrite = syscall.EPOLLPRI | syscall.EPOLLIN | syscall.EPOLLOUT - epoollEventsError = syscall.EPOLLERR | syscall.EPOLLHUP | syscall.EPOLLRDHUP + epollEventsRead = syscall.EPOLLPRI | syscall.EPOLLIN + epollEventsWrite = syscall.EPOLLOUT + epollEventsReadWrite = syscall.EPOLLPRI | syscall.EPOLLIN | syscall.EPOLLOUT + epollEventsError = syscall.EPOLLERR | syscall.EPOLLHUP | syscall.EPOLLRDHUP + + epollEventsReadET = syscall.EPOLLPRI | syscall.EPOLLIN | EPOLLET + epollEventsWriteET = syscall.EPOLLOUT | EPOLLET + epollEventsReadWriteET = syscall.EPOLLPRI | syscall.EPOLLIN | syscall.EPOLLOUT | EPOLLET + epollEventsErrorET = syscall.EPOLLERR | syscall.EPOLLHUP | syscall.EPOLLRDHUP | EPOLLET ) type poller struct { @@ -160,16 +165,16 @@ func (p *poller) readWriteLoop() { default: c := p.getConn(fd) if c != nil { - if ev.Events&epoollEventsError != 0 { + if ev.Events&epollEventsError != 0 { c.closeWithError(io.EOF) continue } - if ev.Events&epoollEventsWrite != 0 { + if ev.Events&epollEventsWrite != 0 { c.flush() } - if ev.Events&epoollEventsRead != 0 { + if ev.Events&epollEventsRead != 0 { if p.g.onRead == nil { for i := 0; i < p.g.maxReadTimesPerEventLoop; i++ { buffer := p.g.borrow(c) @@ -216,8 +221,12 @@ func (p *poller) stop() { } func (p *poller) addRead(fd int) error { - // return syscall.EpollCtl(p.epfd, syscall.EPOLL_CTL_ADD, fd, &syscall.EpollEvent{Fd: int32(fd), Events: epoollEventsRead}) - return syscall.EpollCtl(p.epfd, syscall.EPOLL_CTL_ADD, fd, &syscall.EpollEvent{Fd: int32(fd), Events: uint32(epoollEventsRead | p.g.epollMod)}) + switch p.g.epollMod { + case EPOLLET: + return syscall.EpollCtl(p.epfd, syscall.EPOLL_CTL_ADD, fd, &syscall.EpollEvent{Fd: int32(fd), Events: epollEventsReadET}) + default: + return syscall.EpollCtl(p.epfd, syscall.EPOLL_CTL_ADD, fd, &syscall.EpollEvent{Fd: int32(fd), Events: epollEventsRead}) + } } // func (p *poller) addWrite(fd int) error { @@ -225,8 +234,12 @@ func (p *poller) addRead(fd int) error { // } func (p *poller) modWrite(fd int) error { - // return syscall.EpollCtl(p.epfd, syscall.EPOLL_CTL_MOD, fd, &syscall.EpollEvent{Fd: int32(fd), Events: epoollEventsReadWrite}) - return syscall.EpollCtl(p.epfd, syscall.EPOLL_CTL_ADD, fd, &syscall.EpollEvent{Fd: int32(fd), Events: uint32(epoollEventsReadWrite | p.g.epollMod)}) + switch p.g.epollMod { + case EPOLLET: + return syscall.EpollCtl(p.epfd, syscall.EPOLL_CTL_ADD, fd, &syscall.EpollEvent{Fd: int32(fd), Events: epollEventsReadWriteET}) + default: + return syscall.EpollCtl(p.epfd, syscall.EPOLL_CTL_MOD, fd, &syscall.EpollEvent{Fd: int32(fd), Events: epollEventsReadWrite}) + } } func (p *poller) deleteEvent(fd int) error {