From eb7299748d55b28703a3f53753b3064789d656da Mon Sep 17 00:00:00 2001 From: Andrey Borodin Date: Sun, 1 Dec 2019 23:21:23 +0500 Subject: [PATCH] Implement login timeout --- odyssey.conf | 4 ++++ sources/config.c | 1 + sources/config.h | 1 + sources/config_reader.c | 7 +++++++ sources/frontend.c | 4 ++-- sources/tls.c | 4 ++-- test/machinarium/test_tls0.c | 4 ++-- test/machinarium/test_tls_read_10mb0.c | 4 ++-- test/machinarium/test_tls_read_10mb1.c | 4 ++-- test/machinarium/test_tls_read_10mb2.c | 4 ++-- test/machinarium/test_tls_read_multithread.c | 4 ++-- test/machinarium/test_tls_read_var.c | 4 ++-- test/machinarium/test_tls_unix_socket.c | 4 ++-- third_party/machinarium/sources/io.c | 4 ++-- third_party/machinarium/sources/machinarium.h | 2 +- third_party/machinarium/sources/tls.c | 4 ++-- third_party/machinarium/sources/tls.h | 2 +- 17 files changed, 37 insertions(+), 24 deletions(-) diff --git a/odyssey.conf b/odyssey.conf index dec1d06ba..9342bac82 100644 --- a/odyssey.conf +++ b/odyssey.conf @@ -292,6 +292,10 @@ listen { # tls_key_file "" # tls_cert_file "" # tls_protocols "" + +# client_login_timeout +# Prevent client stall during routing for more that client_login_timeout milliseconds. +# Defaults to 15000. } ### diff --git a/sources/config.c b/sources/config.c index 314c6741a..22f1d324d 100644 --- a/sources/config.c +++ b/sources/config.c @@ -95,6 +95,7 @@ od_config_listen_add(od_config_t *config) memset(listen, 0, sizeof(*listen)); listen->port = 6432; listen->backlog = 128; + listen->client_login_timeout = 15000; od_list_init(&listen->link); od_list_append(&config->listen, &listen->link); return listen; diff --git a/sources/config.h b/sources/config.h index 15223c65f..8b92d3157 100644 --- a/sources/config.h +++ b/sources/config.h @@ -30,6 +30,7 @@ struct od_config_listen char *tls_key_file; char *tls_cert_file; char *tls_protocols; + int client_login_timeout; od_list_t link; }; diff --git a/sources/config_reader.c b/sources/config_reader.c index b01ab079e..aa4fa7889 100644 --- a/sources/config_reader.c +++ b/sources/config_reader.c @@ -65,6 +65,7 @@ enum OD_LCOROUTINE_STACK_SIZE, OD_LCLIENT_MAX, OD_LCLIENT_MAX_ROUTING, + OD_LCLIENT_LOGIN_TIMEOUT, OD_LCLIENT_FWD_ERROR, OD_LTLS, OD_LTLS_CA_FILE, @@ -150,6 +151,7 @@ od_config_keywords[] = od_keyword("coroutine_stack_size", OD_LCOROUTINE_STACK_SIZE), od_keyword("client_max", OD_LCLIENT_MAX), od_keyword("client_max_routing", OD_LCLIENT_MAX_ROUTING), + od_keyword("client_login_timeout", OD_LCLIENT_LOGIN_TIMEOUT), od_keyword("client_fwd_error", OD_LCLIENT_FWD_ERROR), od_keyword("tls", OD_LTLS), od_keyword("tls_ca_file", OD_LTLS_CA_FILE), @@ -414,6 +416,11 @@ od_config_reader_listen(od_config_reader_t *reader) if (! od_config_reader_number(reader, &listen->port)) return -1; continue; + /* client_login_timeout */ + case OD_LCLIENT_LOGIN_TIMEOUT: + if (! od_config_reader_number(reader, &listen->client_login_timeout)) + return -1; + continue; /* backlog */ case OD_LBACKLOG: if (! od_config_reader_number(reader, &listen->backlog)) diff --git a/sources/frontend.c b/sources/frontend.c index 9c392cc84..361dd4d3b 100644 --- a/sources/frontend.c +++ b/sources/frontend.c @@ -107,7 +107,7 @@ od_frontend_startup(od_client_t *client) od_instance_t *instance = client->global->instance; machine_msg_t *msg; - msg = od_read_startup(&client->io, UINT32_MAX); + msg = od_read_startup(&client->io, client->config_listen->client_login_timeout); if (msg == NULL) goto error; @@ -132,7 +132,7 @@ od_frontend_startup(od_client_t *client) /* read startup-cancel message followed after ssl * negotiation */ assert(client->startup.is_ssl_request); - msg = od_read_startup(&client->io, UINT32_MAX); + msg = od_read_startup(&client->io, client->config_listen->client_login_timeout); if (msg == NULL) return -1; rc = kiwi_be_read_startup(machine_msg_data(msg), diff --git a/sources/tls.c b/sources/tls.c index 7dc68adf1..add33bd58 100644 --- a/sources/tls.c +++ b/sources/tls.c @@ -104,7 +104,7 @@ od_tls_frontend_accept(od_client_t *client, od_io_error(&client->io)); return -1; } - rc = machine_set_tls(client->io.io, tls); + rc = machine_set_tls(client->io.io, tls, config->client_login_timeout); if (rc == -1) { od_error(logger, "tls", client, NULL, "error: %s", od_io_error(&client->io)); @@ -203,7 +203,7 @@ od_tls_backend_connect(od_server_t *server, case 'S': /* supported */ od_debug(logger, "tls", NULL, server, "supported"); - rc = machine_set_tls(server->io.io, server->tls); + rc = machine_set_tls(server->io.io, server->tls, UINT32_MAX); if (rc == -1) { od_error(logger, "tls", NULL, server, "error: %s", od_io_error(&server->io)); diff --git a/test/machinarium/test_tls0.c b/test/machinarium/test_tls0.c index e25fc2112..e3429c935 100644 --- a/test/machinarium/test_tls0.c +++ b/test/machinarium/test_tls0.c @@ -37,7 +37,7 @@ server(void *arg) test(rc == 0); rc = machine_tls_create_context(tls,0); test(rc == 0); - rc = machine_set_tls(client, tls); + rc = machine_set_tls(client, tls, UINT32_MAX); if (rc == -1) { printf("%s\n", machine_error(client)); test(rc == 0); @@ -91,7 +91,7 @@ client(void *arg) test(rc == 0); rc = machine_tls_create_context(tls,1); test(rc == 0); - rc = machine_set_tls(client, tls); + rc = machine_set_tls(client, tls, UINT32_MAX); if (rc == -1) { printf("%s\n", machine_error(client)); test(rc == 0); diff --git a/test/machinarium/test_tls_read_10mb0.c b/test/machinarium/test_tls_read_10mb0.c index 512f09f83..2f012125c 100644 --- a/test/machinarium/test_tls_read_10mb0.c +++ b/test/machinarium/test_tls_read_10mb0.c @@ -36,7 +36,7 @@ server(void *arg) test(rc == 0); rc = machine_tls_create_context(tls,0); test(rc == 0); - rc = machine_set_tls(client, tls); + rc = machine_set_tls(client, tls, UINT32_MAX); if (rc == -1) { printf("%s\n", machine_error(client)); test(rc == 0); @@ -96,7 +96,7 @@ client(void *arg) test(rc == 0); rc = machine_tls_create_context(tls,1); test(rc == 0); - rc = machine_set_tls(client, tls); + rc = machine_set_tls(client, tls, UINT32_MAX); if (rc == -1) { printf("%s\n", machine_error(client)); test(rc == 0); diff --git a/test/machinarium/test_tls_read_10mb1.c b/test/machinarium/test_tls_read_10mb1.c index 2fe8c84db..33f7525d1 100644 --- a/test/machinarium/test_tls_read_10mb1.c +++ b/test/machinarium/test_tls_read_10mb1.c @@ -36,7 +36,7 @@ server(void *arg) test(rc == 0); rc = machine_tls_create_context(tls,0); test(rc == 0); - rc = machine_set_tls(client, tls); + rc = machine_set_tls(client, tls, UINT32_MAX); if (rc == -1) { printf("%s\n", machine_error(client)); test(rc == 0); @@ -96,7 +96,7 @@ client(void *arg) test(rc == 0); rc = machine_tls_create_context(tls,1); test(rc == 0); - rc = machine_set_tls(client, tls); + rc = machine_set_tls(client, tls, UINT32_MAX); if (rc == -1) { printf("%s\n", machine_error(client)); test(rc == 0); diff --git a/test/machinarium/test_tls_read_10mb2.c b/test/machinarium/test_tls_read_10mb2.c index 72039316d..60a7b10a4 100644 --- a/test/machinarium/test_tls_read_10mb2.c +++ b/test/machinarium/test_tls_read_10mb2.c @@ -36,7 +36,7 @@ server(void *arg) test(rc == 0); rc = machine_tls_create_context(tls,0); test(rc == 0); - rc = machine_set_tls(client, tls); + rc = machine_set_tls(client, tls, UINT32_MAX); if (rc == -1) { printf("%s\n", machine_error(client)); test(rc == 0); @@ -90,7 +90,7 @@ client(void *arg) test(rc == 0); rc = machine_tls_create_context(tls,1); test(rc == 0); - rc = machine_set_tls(client, tls); + rc = machine_set_tls(client, tls, UINT32_MAX); if (rc == -1) { printf("%s\n", machine_error(client)); test(rc == 0); diff --git a/test/machinarium/test_tls_read_multithread.c b/test/machinarium/test_tls_read_multithread.c index 1f2760678..e905416fb 100644 --- a/test/machinarium/test_tls_read_multithread.c +++ b/test/machinarium/test_tls_read_multithread.c @@ -35,7 +35,7 @@ server(void *arg) test(rc == 0); rc = machine_tls_create_context(tls,0); test(rc == 0); - rc = machine_set_tls(client, tls); + rc = machine_set_tls(client, tls, UINT32_MAX); if (rc == -1) { printf("%s\n", machine_error(client)); test(rc == 0); @@ -94,7 +94,7 @@ client(void *arg) test(rc == 0); rc = machine_tls_create_context(tls,1); test(rc == 0); - rc = machine_set_tls(client, tls); + rc = machine_set_tls(client, tls, UINT32_MAX); if (rc == -1) { printf("%s\n", machine_error(client)); test(rc == 0); diff --git a/test/machinarium/test_tls_read_var.c b/test/machinarium/test_tls_read_var.c index 08450c2f5..0fd85553c 100644 --- a/test/machinarium/test_tls_read_var.c +++ b/test/machinarium/test_tls_read_var.c @@ -37,7 +37,7 @@ server(void *arg) test(rc == 0); rc = machine_tls_create_context(tls,0); test(rc == 0); - rc = machine_set_tls(client, tls); + rc = machine_set_tls(client, tls, UINT32_MAX); if (rc == -1) { printf("%s\n", machine_error(client)); test(rc == 0); @@ -103,7 +103,7 @@ client(void *arg) test(rc == 0); rc = machine_tls_create_context(tls,1); test(rc == 0); - rc = machine_set_tls(client, tls); + rc = machine_set_tls(client, tls, UINT32_MAX); if (rc == -1) { printf("%s\n", machine_error(client)); test(rc == 0); diff --git a/test/machinarium/test_tls_unix_socket.c b/test/machinarium/test_tls_unix_socket.c index 1794719f4..ea2cb9fd9 100644 --- a/test/machinarium/test_tls_unix_socket.c +++ b/test/machinarium/test_tls_unix_socket.c @@ -39,7 +39,7 @@ server(void *arg) test(rc == 0); rc = machine_tls_create_context(tls,0); test(rc == 0); - rc = machine_set_tls(client, tls); + rc = machine_set_tls(client, tls, UINT32_MAX); if (rc == -1) { printf("%s\n", machine_error(client)); test(rc == 0); @@ -94,7 +94,7 @@ client(void *arg) test(rc == 0); rc = machine_tls_create_context(tls,1); test(rc == 0); - rc = machine_set_tls(client, tls); + rc = machine_set_tls(client, tls, UINT32_MAX); if (rc == -1) { printf("%s\n", machine_error(client)); test(rc == 0); diff --git a/third_party/machinarium/sources/io.c b/third_party/machinarium/sources/io.c index 8ae27886e..89ea8188f 100644 --- a/third_party/machinarium/sources/io.c +++ b/third_party/machinarium/sources/io.c @@ -257,7 +257,7 @@ machine_tls_set_key_file(machine_tls_t *obj, char *path) } MACHINE_API int -machine_set_tls(machine_io_t *obj, machine_tls_t *tls) +machine_set_tls(machine_io_t *obj, machine_tls_t *tls, uint32_t timeout) { mm_io_t *io = mm_cast(mm_io_t*, obj); if (io->tls) { @@ -265,7 +265,7 @@ machine_set_tls(machine_io_t *obj, machine_tls_t *tls) return -1; } io->tls = mm_cast(mm_tls_t*, tls); - return mm_tls_handshake(io); + return mm_tls_handshake(io, timeout); } MACHINE_API machine_io_t* diff --git a/third_party/machinarium/sources/machinarium.h b/third_party/machinarium/sources/machinarium.h index 0a793726d..b33cb3b96 100644 --- a/third_party/machinarium/sources/machinarium.h +++ b/third_party/machinarium/sources/machinarium.h @@ -242,7 +242,7 @@ MACHINE_API int machine_set_keepalive(machine_io_t*, int enable, int delay); MACHINE_API int -machine_set_tls(machine_io_t*, machine_tls_t*); +machine_set_tls(machine_io_t*, machine_tls_t*, uint32_t); MACHINE_API int machine_io_verify(machine_io_t*, char *common_name); diff --git a/third_party/machinarium/sources/tls.c b/third_party/machinarium/sources/tls.c index eb7d225af..dfb955a14 100644 --- a/third_party/machinarium/sources/tls.c +++ b/third_party/machinarium/sources/tls.c @@ -354,7 +354,7 @@ mm_tls_handshake_cb(mm_fd_t *handle) } int -mm_tls_handshake(mm_io_t *io) +mm_tls_handshake(mm_io_t *io, uint32_t timeout) { mm_machine_t *machine = mm_self; mm_tls_error_reset(io); @@ -373,7 +373,7 @@ mm_tls_handshake(mm_io_t *io) } /* wait for completion */ - mm_call(&io->call, MM_CALL_HANDSHAKE, UINT32_MAX); + mm_call(&io->call, MM_CALL_HANDSHAKE, timeout); rc = mm_loop_read_write_stop(&machine->loop, &io->handle); if (rc == -1) { diff --git a/third_party/machinarium/sources/tls.h b/third_party/machinarium/sources/tls.h index 5f6ae75c5..b889fae41 100644 --- a/third_party/machinarium/sources/tls.h +++ b/third_party/machinarium/sources/tls.h @@ -18,7 +18,7 @@ mm_tls_is_active(mm_io_t *io) { void mm_tls_init(mm_io_t*); void mm_tls_free(mm_io_t*); void mm_tls_error_reset(mm_io_t*); -int mm_tls_handshake(mm_io_t*); +int mm_tls_handshake(mm_io_t*, uint32_t); int mm_tls_write(mm_io_t*, char*, int); int mm_tls_writev(mm_io_t*, struct iovec*, int); int mm_tls_read_pending(mm_io_t*);