Skip to content

Commit

Permalink
io: support timeout on synchronous calls
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew Fala <[email protected]>
  • Loading branch information
matthewfala committed Dec 1, 2021
1 parent 31d26a2 commit e8d798d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
3 changes: 3 additions & 0 deletions include/fluent-bit/flb_network.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ struct flb_net_setup {
/* max time in seconds to wait for a established connection */
int connect_timeout;

/* max time in seconds to wait for blocking io calls */
int io_timeout;

/* network interface to bind and use to send data */
flb_sds_t source_address;

Expand Down
8 changes: 8 additions & 0 deletions src/flb_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,14 @@ static ssize_t net_io_read(struct flb_upstream_conn *u_conn,

ret = recv(u_conn->fd, buf, len, 0);
if (ret == -1) {
ret = FLB_WOULDBLOCK();
if (ret) {
/* timeout caused error */
flb_warn("[net] sync io_read #%i timeout after %i seconds from: "
"%s:%i",
u_conn->fd, u_conn->u->net.io_timeout,
u_conn->u->tcp_host, u_conn->u->tcp_port);
}
return -1;
}

Expand Down
25 changes: 25 additions & 0 deletions src/flb_network.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ void flb_net_setup_init(struct flb_net_setup *net)
net->keepalive_idle_timeout = 30;
net->keepalive_max_recycle = 0;
net->connect_timeout = 10;
net->io_timeout = 0; /* Infinite time */
net->source_address = NULL;
}

Expand Down Expand Up @@ -224,6 +225,27 @@ int flb_net_socket_blocking(flb_sockfd_t fd)
return 0;
}

int flb_net_socket_set_rcvtimeout(flb_sockfd_t fd, int timeout_in_seconds)
{
#ifdef FLB_SYSTEM_WINDOWS
/* WINDOWS */
DWORD timeout = timeout_in_seconds * 1000;
if (setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout, sizeof timeout)
== -1) {
#else
/* LINUX and MAC OS X */
struct timeval tv;
tv.tv_sec = timeout_in_seconds;
tv.tv_usec = 0;
if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv) == -1) {
#endif
flb_errno();
return -1;
}

return 0;
}

/*
* Enable the TCP_FASTOPEN feature for server side implemented in Linux Kernel >= 3.7,
* for more details read here:
Expand Down Expand Up @@ -1167,6 +1189,9 @@ flb_sockfd_t flb_net_tcp_connect(const char *host, unsigned long port,
/* Disable Nagle's algorithm */
flb_net_socket_tcp_nodelay(fd);

/* Set receive timeout */
flb_net_socket_set_rcvtimeout(fd, u_conn->u->net.io_timeout);

if (u_conn) {
u_conn->fd = fd;
u_conn->event.fd = fd;
Expand Down

0 comments on commit e8d798d

Please sign in to comment.