Optimize IO timeout for hook API setsockopt().

This commit is contained in:
shuxin   zheng 2024-03-23 16:50:26 +08:00
parent 904dfc103f
commit 1900bda152
3 changed files with 36 additions and 3 deletions

View File

@ -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;
}

View File

@ -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

View File

@ -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");