[sheepdog] [PATCH 1/2] sheep: set keepalive in all client socket fd
Yunkai Zhang
yunkai.me at gmail.com
Wed Jun 13 05:38:32 CEST 2012
From: Yunkai Zhang <qiushu.zyk at taobao.com>
We have set keepalive in cached socket fd, but uncached fd should also
need to set this option. So I move set_keepalive() from get_sheep_fd()
to connect_to().
Signed-off-by: Yunkai Zhang <qiushu.zyk at taobao.com>
---
include/net.h | 1 +
lib/net.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++----
sheep/sdnet.c | 47 +----------------------------------------------
3 files changed, 51 insertions(+), 50 deletions(-)
diff --git a/include/net.h b/include/net.h
index 83da12a..581da66 100644
--- a/include/net.h
+++ b/include/net.h
@@ -52,6 +52,7 @@ char *addr_to_str(char *str, int size, uint8_t *addr, uint16_t port);
uint8_t *str_to_addr(int af, const char *ipstr, uint8_t *addr);
int set_nonblocking(int fd);
int set_nodelay(int fd);
+int set_keepalive(int fd);
int set_timeout(int fd);
int get_local_addr(uint8_t *bytes);
diff --git a/lib/net.c b/lib/net.c
index c4a96ac..a7deb2a 100644
--- a/lib/net.c
+++ b/lib/net.c
@@ -229,13 +229,26 @@ int connect_to(const char *name, int port)
}
ret = connect(fd, res->ai_addr, res->ai_addrlen);
- if (ret)
+ if (ret) {
eprintf("failed to connect to %s:%d: %m\n",
name, port);
- else
- goto success;
+ close(fd);
+ continue;
+ }
- close(fd);
+ ret = set_keepalive(fd);
+ if (ret) {
+ close(fd);
+ break;
+ }
+
+ ret = set_nodelay(fd);
+ if (ret) {
+ eprintf("%m\n");
+ close(fd);
+ break;
+ } else
+ goto success;
}
fd = -1;
success:
@@ -420,6 +433,38 @@ int set_nodelay(int fd)
return ret;
}
+/*
+ * Timeout after request is issued after 5s.
+ *
+ * Heart-beat message will be sent periodically with 1s interval.
+ * If the node of the other end of fd fails, we'll detect it in 3s
+ */
+int set_keepalive(int fd)
+{
+ int val = 1;
+
+ if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val)) < 0) {
+ dprintf("%m\n");
+ return -1;
+ }
+ val = 5;
+ if (setsockopt(fd, SOL_TCP, TCP_KEEPIDLE, &val, sizeof(val)) < 0) {
+ dprintf("%m\n");
+ return -1;
+ }
+ val = 1;
+ if (setsockopt(fd, SOL_TCP, TCP_KEEPINTVL, &val, sizeof(val)) < 0) {
+ dprintf("%m\n");
+ return -1;
+ }
+ val = 3;
+ if (setsockopt(fd, SOL_TCP, TCP_KEEPCNT, &val, sizeof(val)) < 0) {
+ dprintf("%m\n");
+ return -1;
+ }
+ return 0;
+}
+
int get_local_addr(uint8_t *bytes)
{
struct ifaddrs *ifaddr, *ifa;
diff --git a/sheep/sdnet.c b/sheep/sdnet.c
index 8479a74..3ae260e 100644
--- a/sheep/sdnet.c
+++ b/sheep/sdnet.c
@@ -828,41 +828,9 @@ void del_sheep_fd(int fd)
}
}
-/*
- * Timeout after request is issued after 5s.
- *
- * Heart-beat message will be sent periodically with 1s interval.
- * If the node of the other end of fd fails, we'll detect it in 3s
- */
-static int set_keepalive(int fd)
-{
- int val = 1;
-
- if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val)) < 0) {
- dprintf("%m\n");
- return -1;
- }
- val = 5;
- if (setsockopt(fd, SOL_TCP, TCP_KEEPIDLE, &val, sizeof(val)) < 0) {
- dprintf("%m\n");
- return -1;
- }
- val = 1;
- if (setsockopt(fd, SOL_TCP, TCP_KEEPINTVL, &val, sizeof(val)) < 0) {
- dprintf("%m\n");
- return -1;
- }
- val = 3;
- if (setsockopt(fd, SOL_TCP, TCP_KEEPCNT, &val, sizeof(val)) < 0) {
- dprintf("%m\n");
- return -1;
- }
- return 0;
-}
-
int get_sheep_fd(uint8_t *addr, uint16_t port, int node_idx, uint32_t epoch)
{
- int i, fd, ret;
+ int i, fd;
char name[INET6_ADDRSTRLEN];
if (cached_epoch == 0) {
@@ -902,19 +870,6 @@ int get_sheep_fd(uint8_t *addr, uint16_t port, int node_idx, uint32_t epoch)
if (fd < 0)
return -1;
- ret = set_keepalive(fd);
- if (ret) {
- close(fd);
- return -1;
- }
-
- ret = set_nodelay(fd);
- if (ret) {
- eprintf("%m\n");
- close(fd);
- return -1;
- }
-
cached_fds[node_idx] = fd;
return fd;
--
1.7.10.2
More information about the sheepdog
mailing list