From 82beb735a6ad0c0effe8125d2b0bdceb40a202a6 Mon Sep 17 00:00:00 2001 From: zhengshuxin Date: Wed, 31 Jan 2024 11:55:59 +0800 Subject: [PATCH] Optimize event module for handling the stream's read_ready for system IO. --- lib_acl/src/event/events.c | 18 +++++++++++++++--- lib_acl/src/master/template/acl_aio_server.c | 3 --- lib_acl/src/stdlib/iostuff/acl_peekfd.c | 8 +++++--- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/lib_acl/src/event/events.c b/lib_acl/src/event/events.c index 19328dad9..8045806dd 100644 --- a/lib_acl/src/event/events.c +++ b/lib_acl/src/event/events.c @@ -29,14 +29,19 @@ void event_check_fds(ACL_EVENT *ev) fdp->event_type |= ACL_EVENT_READ; fdp->fdidx_ready = ev->ready_cnt; ev->ready[ev->ready_cnt++] = fdp; - } else if (fdp->stream->read_ready && !fdp->listener) { + } else if (fdp->stream->read_ready && !fdp->listener + && acl_peekfd(ACL_VSTREAM_SOCK(fdp->stream)) > 0) { fdp->event_type |= ACL_EVENT_READ; fdp->fdidx_ready = ev->ready_cnt; ev->ready[ev->ready_cnt++] = fdp; } else if (fdp->r_ttl > 0 && ev->present > fdp->r_ttl) { + fdp->stream->read_ready = 0; fdp->event_type |= ACL_EVENT_RW_TIMEOUT; fdp->fdidx_ready = ev->ready_cnt; ev->ready[ev->ready_cnt++] = fdp; + } else { + + fdp->stream->read_ready = 0; } } else if ((fdp->flag & EVENT_FDTABLE_FLAG_WRITE)) { if (fdp->w_ttl > 0 && ev->present > fdp->w_ttl) { @@ -68,7 +73,11 @@ int event_prepare(ACL_EVENT *ev) fdp->fdidx_ready = ev->ready_cnt; ev->ready[ev->ready_cnt++] = fdp; } else if ((fdp->flag & EVENT_FDTABLE_FLAG_READ)) { - if (fdp->stream->read_ready && !fdp->listener) { + /* If the read_ready has been set, we should check + * if the fd can be readable first. + */ + if (fdp->stream->read_ready && !fdp->listener + && acl_peekfd(sockfd) > 0) { fdp->event_type |= ACL_EVENT_READ; fdp->fdidx_ready = ev->ready_cnt; ev->ready[ev->ready_cnt++] = fdp; @@ -78,11 +87,14 @@ int event_prepare(ACL_EVENT *ev) fdp->fdidx_ready = ev->ready_cnt; ev->ready[ev->ready_cnt++] = fdp; } else if (fdp->r_ttl > 0 && ev->present > fdp->r_ttl) { + fdp->stream->read_ready = 0; fdp->event_type |= ACL_EVENT_RW_TIMEOUT; fdp->fdidx_ready = ev->ready_cnt; ev->ready[ev->ready_cnt++] = fdp; - } else + } else { + fdp->stream->read_ready = 0; nwait++; + } } else if ((fdp->flag & EVENT_FDTABLE_FLAG_WRITE)) { if (fdp->w_ttl > 0 && ev->present > fdp->w_ttl) { fdp->event_type |= ACL_EVENT_RW_TIMEOUT; diff --git a/lib_acl/src/master/template/acl_aio_server.c b/lib_acl/src/master/template/acl_aio_server.c index 460e4ac1e..2c2c5e377 100644 --- a/lib_acl/src/master/template/acl_aio_server.c +++ b/lib_acl/src/master/template/acl_aio_server.c @@ -923,9 +923,6 @@ static void dispatch_receive(int event_type acl_unused, ACL_EVENT *event, acl_msg_fatal("%s(%d), %s: conn invalid", __FUNCTION__, __LINE__, myname); - /* XXX: Must reset the read_ready flag been set in event trigger */ - conn->read_ready = 0; - ret = acl_read_fd(ACL_VSTREAM_SOCK(conn), buf, sizeof(buf) - 1, &fd); if (ret <= 0 || fd < 0) { acl_msg_warn("%s(%d), %s: read from master_dispatch(%s) error %s", diff --git a/lib_acl/src/stdlib/iostuff/acl_peekfd.c b/lib_acl/src/stdlib/iostuff/acl_peekfd.c index 2185a9703..5ad679af4 100644 --- a/lib_acl/src/stdlib/iostuff/acl_peekfd.c +++ b/lib_acl/src/stdlib/iostuff/acl_peekfd.c @@ -37,9 +37,11 @@ int acl_peekfd(ACL_SOCKET fd) * Anticipate a series of system-dependent code fragments. */ #ifdef ACL_UNIX - return (ioctl(fd, FIONREAD, (char *) &count) < 0 ? -1 : count); + return ioctl(fd, FIONREAD, (char *) &count) < 0 ? -1 : count; #elif defined(ACL_WINDOWS) - return (ioctlsocket(fd, FIONREAD, (unsigned long *) &count) < 0 - ? -1 : count); + return ioctlsocket(fd, FIONREAD, (unsigned long *) &count) < 0 + ? -1 : count; +#else + return 0; #endif }