Commit 580b7dce authored by Geoff Garside's avatar Geoff Garside Committed by antirez
Browse files

Add anetSetReuseAddr(err, fd) static function.

Extract setting SO_REUSEADDR socket option into separate function
so the same code can be more easily used by anetCreateSocket and
other functions.
parent 071963c8
...@@ -186,8 +186,19 @@ int anetResolve(char *err, char *host, char *ipbuf, size_t ipbuf_len) ...@@ -186,8 +186,19 @@ int anetResolve(char *err, char *host, char *ipbuf, size_t ipbuf_len)
return ANET_OK; return ANET_OK;
} }
static int anetSetReuseAddr(char *err, int fd) {
int yes = 1;
/* Make sure connection-intensive things like the redis benckmark
* will be able to close/open sockets a zillion of times */
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1) {
anetSetError(err, "setsockopt SO_REUSEADDR: %s", strerror(errno));
return ANET_ERR;
}
return ANET_OK;
}
static int anetCreateSocket(char *err, int domain) { static int anetCreateSocket(char *err, int domain) {
int s, on = 1; int s;
if ((s = socket(domain, SOCK_STREAM, 0)) == -1) { if ((s = socket(domain, SOCK_STREAM, 0)) == -1) {
anetSetError(err, "creating socket: %s", strerror(errno)); anetSetError(err, "creating socket: %s", strerror(errno));
return ANET_ERR; return ANET_ERR;
...@@ -195,8 +206,8 @@ static int anetCreateSocket(char *err, int domain) { ...@@ -195,8 +206,8 @@ static int anetCreateSocket(char *err, int domain) {
/* Make sure connection-intensive things like the redis benchmark /* Make sure connection-intensive things like the redis benchmark
* will be able to close/open sockets a zillion of times */ * will be able to close/open sockets a zillion of times */
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) { if (anetSetReuseAddr(err,s) == ANET_ERR) {
anetSetError(err, "setsockopt SO_REUSEADDR: %s", strerror(errno)); close(s);
return ANET_ERR; return ANET_ERR;
} }
return s; return s;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment