Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keepalive usr default value #701

Merged
merged 3 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion documentation/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,11 @@ TCP keep-alive probes to send before giving up and killing the connection i

#### keepalive_usr_timeout *integer*
When the value is greater than 0, it specifies the maximum amount of time in milliseconds that transmitted data may remain unacknowledged before TCP will forcibly close the
corresponding connection
corresponding connection.

When the value is negaive, the default system user timeout will be used.

When no value or 0 is provided `floor(keepalive + keepalive_keep_interval * keepalive_probes - 0.5)` is used.

`keepalive_usr_timeout 7`

Expand Down
15 changes: 15 additions & 0 deletions sources/config_reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -2366,6 +2366,19 @@ static int od_config_reader_hba_import(od_config_reader_t *config_reader)
return rc;
}

static void od_config_setup_default_tcp_usr_timeout(od_config_t *config)
{
if (config->keepalive_usr_timeout == 0) {
config->keepalive_usr_timeout =
machine_advice_keepalive_usr_timeout(
config->keepalive,
config->keepalive_keep_interval,
config->keepalive_probes);
} else if (config->keepalive_usr_timeout < 0) {
config->keepalive_usr_timeout = 0;
}
}

static int od_config_reader_parse(od_config_reader_t *reader,
od_extention_t *extentions)
{
Expand Down Expand Up @@ -2815,6 +2828,8 @@ static int od_config_reader_parse(od_config_reader_t *reader,
config->client_max_routing = config->workers * 16;
}

od_config_setup_default_tcp_usr_timeout(config);

return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion stress/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ if(THREADS_HAVE_PTHREAD_ARG)
set_property(TARGET ${od_stress_binary} PROPERTY INTERFACE_COMPILE_OPTIONS "-pthread")
endif()

target_link_libraries(${od_stress_binary} ${od_libraries} ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries(${od_stress_binary} ${od_libraries} ${CMAKE_THREAD_LIBS_INIT} m)

if (BUILD_COMPRESSION)
target_link_libraries(${od_stress_binary} ${compression_libraries})
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ set(od_test_src
machinarium/test_connect_cancel1.c
machinarium/test_accept_timeout.c
machinarium/test_accept_cancel.c
machinarium/test_advice_keepalive_usr_timeout.c
machinarium/test_getaddrinfo0.c
machinarium/test_getaddrinfo1.c
machinarium/test_getaddrinfo2.c
Expand Down
8 changes: 8 additions & 0 deletions test/machinarium/test_advice_keepalive_usr_timeout.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <machinarium.h>
#include <odyssey_test.h>

void machinarium_test_advice_keepalive_usr_timeout(void)
{
test(machine_advice_keepalive_usr_timeout(100, 10, 3) == 129500);
test(machine_advice_keepalive_usr_timeout(15, 10, 5) == 64500);
}
2 changes: 2 additions & 0 deletions test/odyssey_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ extern void machinarium_test_connect_cancel0(void);
extern void machinarium_test_connect_cancel1(void);
extern void machinarium_test_accept_timeout(void);
extern void machinarium_test_accept_cancel(void);
extern void machinarium_test_advice_keepalive_usr_timeout(void);
extern void machinarium_test_getaddrinfo0(void);
extern void machinarium_test_getaddrinfo1(void);
extern void machinarium_test_getaddrinfo2(void);
Expand Down Expand Up @@ -134,6 +135,7 @@ int main(int argc, char *argv[])
odyssey_test(machinarium_test_connect_cancel1);
odyssey_test(machinarium_test_accept_timeout);
odyssey_test(machinarium_test_accept_cancel);
odyssey_test(machinarium_test_advice_keepalive_usr_timeout);
odyssey_test(machinarium_test_getaddrinfo0);
odyssey_test(machinarium_test_getaddrinfo1);
odyssey_test(machinarium_test_getaddrinfo2);
Expand Down
6 changes: 6 additions & 0 deletions third_party/machinarium/sources/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,12 @@ MACHINE_API int machine_set_keepalive(machine_io_t *obj, int enable, int delay,
return 0;
}

MACHINE_API int machine_advice_keepalive_usr_timeout(int delay, int interval,
int probes)
{
return mm_socket_advice_keepalive_usr_timeout(delay, interval, probes);
}

MACHINE_API int machine_io_attach(machine_io_t *obj)
{
mm_io_t *io = mm_cast(mm_io_t *, obj);
Expand Down
3 changes: 3 additions & 0 deletions third_party/machinarium/sources/machinarium.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ MACHINE_API int machine_set_keepalive(machine_io_t *, int enable, int delay,
int interval, int probes,
int usr_timeout);

MACHINE_API int machine_advice_keepalive_usr_timeout(int delay, int interval,
int probes);

MACHINE_API int machine_set_tls(machine_io_t *, machine_tls_t *, uint32_t);
MACHINE_API int machine_set_compression(machine_io_t *, char algorithm);

Expand Down
1 change: 1 addition & 0 deletions third_party/machinarium/sources/machinarium_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <time.h>
#include <assert.h>
#include <signal.h>
#include <math.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
Expand Down
10 changes: 10 additions & 0 deletions third_party/machinarium/sources/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ int mm_socket_set_keepalive(int fd, int enable, int delay, int interval,
return MM_OK_RETCODE;
}

int mm_socket_advice_keepalive_usr_timeout(int delay, int interval,
int keep_count)
{
// https://habr.com/ru/articles/700470/
// delay, interval are in seconds
// usr timeout in milliseconds
// see man 7 tcp
return 1000 * (delay + interval * keep_count) - 500;
}

int mm_socket_set_nosigpipe(int fd, int enable)
{
#if defined(SO_NOSIGPIPE)
Expand Down
2 changes: 2 additions & 0 deletions third_party/machinarium/sources/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ int mm_socket_set_nonblock(int, int);
int mm_socket_set_nodelay(int, int);
int mm_socket_set_keepalive(int fd, int enable, int delay, int interval,
int keep_count, int usr_timeout);
int mm_socket_advice_keepalive_usr_timeout(int delay, int interval,
int keep_count);
int mm_socket_set_nosigpipe(int, int);
int mm_socket_set_reuseaddr(int, int);
int mm_socket_set_reuseport(int, int);
Expand Down
Loading