summaryrefslogtreecommitdiff
path: root/vio/viosocket.c
diff options
context:
space:
mode:
authorVladislav Vaintroub <wlad@mariadb.com>2017-11-17 19:36:47 +0000
committerVladislav Vaintroub <wlad@mariadb.com>2017-11-17 21:40:20 +0000
commite0a00c5a2f276a4e314785a89c84f58d033b46b3 (patch)
treea746ae593da78aa53f8aa2bce342e4099d667a31 /vio/viosocket.c
parentfaee08c10c767375aca26d126fff1a832330fc43 (diff)
downloadmariadb-git-e0a00c5a2f276a4e314785a89c84f58d033b46b3.tar.gz
MDEV-14412 Support TCP keepalive options
Based on pull request by Oleg Obleukhov https://github.com/MariaDB/server/pull/400
Diffstat (limited to 'vio/viosocket.c')
-rw-r--r--vio/viosocket.c58
1 files changed, 58 insertions, 0 deletions
diff --git a/vio/viosocket.c b/vio/viosocket.c
index f983fe20fe7..6be830fee9e 100644
--- a/vio/viosocket.c
+++ b/vio/viosocket.c
@@ -28,6 +28,7 @@
#ifdef __WIN__
#include <winsock2.h>
#include <MSWSock.h>
+ #include <mstcpip.h>
#pragma comment(lib, "ws2_32.lib")
#endif
#include "my_context.h"
@@ -522,6 +523,63 @@ int vio_keepalive(Vio* vio, my_bool set_keep_alive)
DBUG_RETURN(r);
}
+/*
+ Set socket options for keepalive e.g., TCP_KEEPCNT, TCP_KEEPIDLE/TCP_KEEPALIVE, TCP_KEEPINTVL
+*/
+int vio_set_keepalive_options(Vio* vio, const struct vio_keepalive_opts *opts)
+{
+#if defined _WIN32
+ struct tcp_keepalive s;
+ DWORD nbytes;
+
+ if (vio->type == VIO_TYPE_NAMEDPIPE || vio->type == VIO_TYPE_SHARED_MEMORY)
+ return 0;
+
+ if (!opts->idle && !opts->interval)
+ return 0;
+
+ s.onoff= 1;
+ s.keepalivetime= opts->idle? opts->idle * 1000 : 7200;
+ s.keepaliveinterval= opts->interval?opts->interval * 1000 : 1;
+
+ return WSAIoctl(vio->mysql_socket.fd, SIO_KEEPALIVE_VALS, (LPVOID) &s, sizeof(s),
+ NULL, 0, &nbytes, NULL, NULL);
+
+#elif defined (TCP_KEEPIDLE) || defined (TCP_KEEPALIVE)
+
+ int ret= 0;
+ if (opts->idle)
+ {
+#ifdef TCP_KEEPIDLE // Linux only
+ ret= mysql_socket_setsockopt(vio->mysql_socket, IPPROTO_TCP, TCP_KEEPIDLE, (char *)&opts->idle, sizeof(opts->idle));
+#elif defined (TCP_KEEPALIVE)
+ ret= mysql_socket_setsockopt(vio->mysql_socket, IPPROTO_TCP, TCP_KEEPALIVE, (char *)&opts->idle, sizeof(opts->idle));
+#endif
+ if(ret)
+ return ret;
+ }
+
+#ifdef TCP_KEEPCNT // Linux only
+ if(opts->probes)
+ {
+ ret= mysql_socket_setsockopt(vio->mysql_socket, IPPROTO_TCP, TCP_KEEPCNT, (char *)&opts->probes, sizeof(opts->probes));
+ if(ret)
+ return ret;
+ }
+#endif
+
+#ifdef TCP_KEEPINTVL // Linux only
+ if(opts->interval)
+ {
+ ret= mysql_socket_setsockopt(vio->mysql_socket, IPPROTO_TCP, TCP_KEEPINTVL, (char *)&opts->interval, sizeof(opts->interval));
+ }
+#endif
+ return ret;
+#else /*TCP_KEEPIDLE || TCP_KEEPALIVE */
+ return -1;
+#endif
+}
+
/**
Indicate whether a I/O operation must be retried later.