Skip to content
This repository has been archived by the owner on May 6, 2020. It is now read-only.

Commit

Permalink
connect: Try to re-connect to proxy
Browse files Browse the repository at this point in the history
Try to re-connect to proxy if connection is lost (e.g. proxy died)
with RECONNECT_TIMEOUT_S timeout.

This patch is needed to implement high availability of cc-proxy.

Fixes #53.

Signed-off-by: Dmitry Voytik <dmitry.voytik@huawei.com>
  • Loading branch information
Dmitry Voytik committed Aug 2, 2017
1 parent ab14648 commit 595724c
Showing 1 changed file with 54 additions and 4 deletions.
58 changes: 54 additions & 4 deletions src/shim.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <sys/socket.h>
#include <unistd.h>
#include <netdb.h>
#include <time.h>

#include "config.h"
#include "utils.h"
Expand Down Expand Up @@ -359,9 +360,7 @@ bool read_wire_data(int fd, uint8_t *buf, size_t size)
ret = recv(fd, buf+offset, size-offset, 0);
if (ret == 0) {
shim_debug("Received EOF on file descriptor\n");
// TODO: Exit for now, add logic to try to reconnect
// to proxy.
exit(EXIT_FAILURE);
return false;
} else if (ret < 0) {
shim_error("Failed to read from fd: %s\n",
strerror(errno));
Expand Down Expand Up @@ -691,6 +690,8 @@ handle_proxy_notification(struct cc_shim *shim, struct frame *fr)
}
}

bool reconnect_to_proxy(struct cc_shim *shim);

/*!
* Handle message received from proxy
*
Expand All @@ -703,6 +704,9 @@ handle_proxy_message(struct cc_shim *shim)

fr = read_frame(shim);
if ( !fr) {
if (! reconnect_to_proxy(shim)) {
exit(EXIT_FAILURE);
}
return;
}

Expand Down Expand Up @@ -876,7 +880,7 @@ connect_to_proxy(struct cc_shim *shim)

if (connect(sockfd, (struct sockaddr *)&remote,
sizeof(struct sockaddr_un)) == -1) {
shim_error("Error while connecting to proxy "
shim_warning("Error while connecting to proxy "
"with address %s: %s\n", shim->proxy_address,
strerror(errno));
goto out;
Expand Down Expand Up @@ -954,6 +958,52 @@ connect_to_proxy(struct cc_shim *shim)
return false;
}

inline void sleep_ms(int ms)
{
struct timespec ts = { 0, ms * 1000000L };
nanosleep(&ts, NULL);
}

/*
* Try to re-establish tcp/unix connection with proxy in RECONNECT_TIMEOUT_S
* seconds.
*
* \param shim \ref cc_shim
*
* \return true on success, false on failure
*/
bool reconnect_to_proxy(struct cc_shim *shim)
{
// How many seconds shim tries to reconnect to proxy
const int RECONNECT_TIMEOUT_S = 10;
// Period between each attempt in milliseconds
const int RECONNECT_POLL_MS = 333;

shim_warning("Reconnecting to proxy\n");
int time = 0;

try_again:
sleep_ms(RECONNECT_POLL_MS);
time += RECONNECT_POLL_MS;
if (time >= RECONNECT_TIMEOUT_S * 1000) {
shim_error("Failed to reconnect to proxy in %d sec\n",
RECONNECT_TIMEOUT_S);
return false;
}
if (! connect_to_proxy(shim)) {
goto try_again;
}

shim_debug("Sending connect command\n");
if (! send_connect_command(shim)) {
shim_error("Could not send connect command to proxy\n");
goto try_again;
}
add_pollfd(poll_fds, PROXY_SOCK_INDEX, shim->proxy_sock_fd,
POLLIN | POLLPRI);
return true;
}

/*
* Print version information.
*/
Expand Down

0 comments on commit 595724c

Please sign in to comment.