Skip to content

Commit

Permalink
Fixes on connection timeout code.
Browse files Browse the repository at this point in the history
  • Loading branch information
mariya-rightscale committed Jul 16, 2010
1 parent 4c24620 commit d976b21
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 36 deletions.
99 changes: 67 additions & 32 deletions src/rrd_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
static int sd = -1;
static FILE *sh = NULL;
static char *sd_path = NULL; /* cache the path for sd */
static int conn_to; /*bool. Connect wit user specified timeout?*/
static int conn_timeout; /* User specified timeout */

/* get_path: Return a path name appropriate to be sent to the daemon.
Expand Down Expand Up @@ -543,8 +542,11 @@ static int rrdc_connect_network (const char *addr_orig) /* {{{ */
char *port;
/* Connect with timeout. (Inspired by <http://www.developerweb.net/forum/showthread.php?p=13486>) */
long arg;
fd_set myset;
fd_set myset;
int valopt;
socklen_t lon;
struct timeval tv;
int res;

assert (addr_orig != NULL);
assert (sd == -1);
Expand Down Expand Up @@ -609,8 +611,8 @@ static int rrdc_connect_network (const char *addr_orig) /* {{{ */
return (-1);
}

if(conn_to)
{
if(conn_timeout > 0)
{
for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
{
// Create sockets
Expand All @@ -621,39 +623,73 @@ static int rrdc_connect_network (const char *addr_orig) /* {{{ */
sd = -1;
continue;
}

// Set non-blocking
arg = fcntl(sd, F_GETFL, NULL);
if(((arg = fcntl(sd, F_GETFL, NULL))) < 0) {
rrd_set_error("Error fcntl(..., F_GETFL) (%s)\n", strerror(errno));
return -1;
}
arg |= O_NONBLOCK;
fcntl(sd, F_SETFL, arg);
if( fcntl(sd, F_SETFL, arg) < 0) {
rrd_set_error("Error fcntl(..., F_SETFL) (%s)\n", strerror(errno));
return -1;
}

// Trying to connect with timeout
status = connect (sd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
tv.tv_sec = conn_timeout;
tv.tv_usec = 0;
FD_ZERO(&myset);
FD_SET(sd, &myset);
int cd; // connect descriptor
cd = select(sd+1, NULL, &myset, NULL, &tv);
if(cd > 0)
status = 0;
else if(status <= 0)
status = -1;

// Set to blocking mode again...
arg = fcntl(sd, F_GETFL, NULL);
arg &= (~O_NONBLOCK);
fcntl(sd, F_SETFL, arg);

sh = fdopen (sd, "r+");
if (sh == NULL)
{
status = errno;
close_connection ();
if (status < 0) {
if (errno == EINPROGRESS) {
tv.tv_sec = conn_timeout;
tv.tv_usec = 0;
FD_ZERO(&myset);
FD_SET(sd, &myset);
res = select(sd+1, NULL, &myset, NULL, &tv);
if (res <= 0 && errno != EINTR) {
status = -1;
close_connection();
rrd_set_error( "Error in connecting %s\n", strerror(errno));
continue;
}
else if (res > 0) {
// Socket selected for write
lon = sizeof(int);
if (getsockopt(sd, SOL_SOCKET, SO_ERROR, (void*)(&valopt), &lon) < 0) { // getsockopt returns -1 in case of error
status = -1;
close_connection();
rrd_set_error( "Error in getsockopt() %s\n", strerror(errno));
continue;
}else{ // getsockopt returns 0 upon successful completion
status = 0;
// Set to blocking mode again...
if( (arg = fcntl(sd, F_GETFL, NULL)) < 0) {
rrd_set_error("Error fcntl(..., F_GETFL) (%s)\n", strerror(errno));
}
arg &= (~O_NONBLOCK);
if( fcntl(sd, F_SETFL, arg) < 0) {
rrd_set_error("Error fcntl(..., F_SETFL) (%s)\n", strerror(errno));
}

sh = fdopen (sd, "r+");
if (sh == NULL)
{
status = errno;
close_connection ();
continue;
}

break;
}
// Check the value returned...
}
else {
status = -1;
close_connection();
rrd_set_error("Error connecting (%s)\n", strerror(errno));
continue;
}

break;
}
}
} /* for (ai_ptr) */
}
}else{
for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
{
Expand Down Expand Up @@ -1078,10 +1114,9 @@ int rrdc_fetch (const char *filename, /* {{{ */
#undef BAIL_OUT
} /* }}} int rrdc_flush */

int get_conn_to(int c_to,
int set_conn_to(
int c_timeout)
{
conn_to = c_to;
conn_timeout = c_timeout;
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion src/rrd_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ int rrdc_fetch (const char *filename,
char ***ret_ds_names,
rrd_value_t **ret_data);

int get_conn_to(int c_to,
int set_conn_to(
int c_timeout);

#else
Expand Down
4 changes: 1 addition & 3 deletions src/rrd_graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ int data_fetch(
int i, ii;
int skip;

get_conn_to(im->conn_to, im->c_timeout);
set_conn_to(im->c_timeout);

/* pull the data from the rrd files ... */
for (i = 0; i < (int) im->gdes_c; i++) {
Expand Down Expand Up @@ -4296,7 +4296,6 @@ void rrd_graph_init(
im->zoom = 1;
im->heat_gap = 0.0;
im->heat_base = 0.0;
im->conn_to = 0;
im->c_timeout = 0;

im->surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 10, 10);
Expand Down Expand Up @@ -4697,7 +4696,6 @@ void rrd_graph_options(
break;
case 'O':
im->c_timeout = atoi(optarg);
im->conn_to = 1;
break;
case 'c':
if (sscanf(optarg,
Expand Down

0 comments on commit d976b21

Please sign in to comment.