From 1900bda15257516af0e46fd49798e26d4e9d5a76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?shuxin=20=E3=80=80=E3=80=80zheng?= Date: Sat, 23 Mar 2024 16:50:26 +0800 Subject: [PATCH] Optimize IO timeout for hook API setsockopt(). --- lib_fiber/c/src/fiber_io.c | 22 ++++++++++++++++++++-- lib_fiber/c/src/hook/socket.c | 12 ++++++++++++ lib_fiber/samples/fiber_stack/main.c | 5 ++++- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/lib_fiber/c/src/fiber_io.c b/lib_fiber/c/src/fiber_io.c index 192ae9d93..9a8991585 100644 --- a/lib_fiber/c/src/fiber_io.c +++ b/lib_fiber/c/src/fiber_io.c @@ -495,11 +495,21 @@ int fiber_wait_read(FILE_EVENT *fe) } if (acl_fiber_canceled(curr)) { - acl_fiber_set_error(curr->errnum); // If the IO has been canceled, we should try to remove the // IO read event, because the wakeup process wasn't from // read_callback normally. event_del_read(__thread_fiber->event, fe); + acl_fiber_set_error(curr->errnum); + return -1; + } else if (curr->flag & FIBER_F_TIMER) { + // If the IO reading timeout set in setsockopt. + curr->flag &= ~FIBER_F_TIMER; + event_del_read(__thread_fiber->event, fe); + acl_fiber_set_errno(curr, FIBER_ETIME); + acl_fiber_set_error(FIBER_ETIME); + + acl_fiber_set_errno(curr, FIBER_EAGAIN); + acl_fiber_set_error(FIBER_EAGAIN); return -1; } // else: the IO read event should has been removed in read_callback. @@ -557,8 +567,16 @@ int fiber_wait_write(FILE_EVENT *fe) } if (acl_fiber_canceled(curr)) { - acl_fiber_set_error(curr->errnum); event_del_write(__thread_fiber->event, fe); + acl_fiber_set_error(curr->errnum); + return -1; + } else if (curr->flag & FIBER_F_TIMER) { + curr->flag &= ~FIBER_F_TIMER; + event_del_write(__thread_fiber->event, fe); + //acl_fiber_set_errno(curr, FIBER_EAGAIN); + //acl_fiber_set_error(FIBER_EAGAIN); + acl_fiber_set_errno(curr, FIBER_ETIME); + acl_fiber_set_error(FIBER_ETIME); return -1; } diff --git a/lib_fiber/c/src/hook/socket.c b/lib_fiber/c/src/hook/socket.c index 2e42b5a19..6e3b0932d 100644 --- a/lib_fiber/c/src/hook/socket.c +++ b/lib_fiber/c/src/hook/socket.c @@ -484,6 +484,8 @@ int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) return acl_fiber_connect(sockfd, addr, addrlen); } +#ifdef CREAT_TIMER_FIBER + typedef struct TIMEOUT_CTX { ACL_FIBER *fiber; int sockfd; @@ -536,11 +538,15 @@ static void send_timeout(ACL_FIBER *fiber UNUSED, void *ctx) mem_free(ctx); } +#endif // CREAT_TIMER_FIBER + int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen) { size_t val; +#ifdef CREAT_TIMER_FIBER TIMEOUT_CTX *ctx; +#endif const struct timeval *tm; if (sys_setsockopt == NULL) { @@ -583,6 +589,7 @@ int setsockopt(int sockfd, int level, int optname, return -1; } +#ifdef CREAT_TIMER_FIBER ctx = (TIMEOUT_CTX*) mem_malloc(sizeof(TIMEOUT_CTX)); ctx->fiber = acl_fiber_running(); ctx->sockfd = sockfd; @@ -599,6 +606,11 @@ int setsockopt(int sockfd, int level, int optname, msg_error("Invalid optname=%d", optname); return -1; } +#else + val *= 1000; + fiber_timer_add(acl_fiber_running(), val); + return 0; +#endif } #endif diff --git a/lib_fiber/samples/fiber_stack/main.c b/lib_fiber/samples/fiber_stack/main.c index 3c9ee68e1..fcb9ebef4 100644 --- a/lib_fiber/samples/fiber_stack/main.c +++ b/lib_fiber/samples/fiber_stack/main.c @@ -6,7 +6,7 @@ #include "fiber/libfiber.h" static int __stack_size = 320000; -static int __rw_timeout = 0; +static int __rw_timeout = 5; static int __echo_data = 0; static int __setsockopt_timeout = 0; @@ -78,6 +78,7 @@ static void fiber_accept(ACL_FIBER *fiber acl_unused, void *ctx) break; } +#if 0 ret = acl_vstream_gets(cstream, buf, sizeof(buf) - 1); if (ret == ACL_VSTREAM_EOF) { printf("get first line error\r\n"); @@ -88,6 +89,7 @@ static void fiber_accept(ACL_FIBER *fiber acl_unused, void *ctx) acl_vstream_close(cstream); continue; } +#endif //printf("accept one, fd: %d\r\n", ACL_VSTREAM_SOCK(cstream)); acl_fiber_create(echo_client, cstream, __stack_size); @@ -183,6 +185,7 @@ int main(int argc, char *argv[]) __listen_fiber = acl_fiber_create(fiber_accept, sstream, 327680); + if (0) acl_fiber_create(fiber_sleep, NULL, 327680); printf("call fiber_schedule\r\n");