Add timeout to incoming connections
All checks were successful
Run Unit Tests / build-and-run-unit-tests (push) Successful in 20s
All checks were successful
Run Unit Tests / build-and-run-unit-tests (push) Successful in 20s
This commit is contained in:
parent
76ec53dfb0
commit
1b7c73f12f
2 changed files with 43 additions and 5 deletions
|
@ -18,6 +18,8 @@
|
|||
#define SEODISPARATE_COM_C_SIMPLE_HTTP_CONSTANTS_H_
|
||||
|
||||
#define C_SIMPLE_HTTP_SLEEP_NANOS 1000000
|
||||
#define C_SIMPLE_HTTP_NONBLOCK_SLEEP_NANOS 1000000
|
||||
#define C_SIMPLE_HTTP_MAX_NONBLOCK_WAIT_NANOS 3500000000
|
||||
#define C_SIMPLE_HTTP_TRY_CONFIG_RELOAD_NANOS 4000000000
|
||||
#define C_SIMPLE_HTTP_TRY_CONFIG_RELOAD_TICKS \
|
||||
(C_SIMPLE_HTTP_TRY_CONFIG_RELOAD_NANOS / C_SIMPLE_HTTP_SLEEP_NANOS)
|
||||
|
|
36
src/main.c
36
src/main.c
|
@ -29,6 +29,7 @@
|
|||
#include <errno.h>
|
||||
#include <sys/inotify.h>
|
||||
#include <linux/limits.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
// Third party includes.
|
||||
#include <SimpleArchiver/src/helpers.h>
|
||||
|
@ -352,14 +353,49 @@ int main(int argc, char **argv) {
|
|||
printf("Peer connected.\n");
|
||||
}
|
||||
int connection_fd = ret;
|
||||
|
||||
// Set non-blocking.
|
||||
ret = fcntl(connection_fd, F_SETFL, O_NONBLOCK);
|
||||
if (ret < 0) {
|
||||
fprintf(stderr, "ERROR Failed to set non-blocking on connection fd!\n");
|
||||
close(connection_fd);
|
||||
continue;
|
||||
}
|
||||
|
||||
{
|
||||
const struct timespec non_block_wait_time =
|
||||
(struct timespec){.tv_sec = 0,
|
||||
.tv_nsec = C_SIMPLE_HTTP_NONBLOCK_SLEEP_NANOS};
|
||||
uint64_t nanoseconds_waited = 0;
|
||||
int_fast8_t continue_after = 0;
|
||||
while (1) {
|
||||
read_ret = read(connection_fd, recv_buf, C_SIMPLE_HTTP_RECV_BUF_SIZE);
|
||||
if (read_ret < 0) {
|
||||
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
||||
nanosleep(&non_block_wait_time, NULL);
|
||||
nanoseconds_waited += (uint64_t)non_block_wait_time.tv_nsec;
|
||||
if (nanoseconds_waited >= C_SIMPLE_HTTP_MAX_NONBLOCK_WAIT_NANOS) {
|
||||
fprintf(stderr, "WARNING Connection timed out!\n");
|
||||
close(connection_fd);
|
||||
continue_after = 1;
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
close(connection_fd);
|
||||
fprintf(
|
||||
stderr,
|
||||
"WARNING Failed to read from new connection, closing...\n");
|
||||
continue_after = 1;
|
||||
break;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (continue_after) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
#ifndef NDEBUG
|
||||
// DEBUG print received buf.
|
||||
for (uint32_t idx = 0;
|
||||
|
|
Loading…
Reference in a new issue