Skip to content

Commit

Permalink
Merge pull request #233 from openziti/hosting-re-bind
Browse files Browse the repository at this point in the history
rebind hosting connections after channel failure
  • Loading branch information
ekoby authored Feb 9, 2021
2 parents aff3dde + 558a620 commit a48c51a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
46 changes: 39 additions & 7 deletions library/connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ static int ziti_channel_start_connection(struct ziti_conn *conn);

static int ziti_disconnect(ziti_connection conn);

static void restart_connect(struct ziti_conn *conn);

static void free_handle(uv_handle_t *h) {
free(h);
}
Expand Down Expand Up @@ -402,13 +404,14 @@ static void connect_get_net_session_cb(ziti_net_session * s, ziti_error *err, vo
struct ziti_ctx *ztx = conn->ziti_ctx;

if (err != NULL) {
CONN_LOG(ERROR, "failed to get session for service[%s]: %s(%s)", conn->service, err->code, err->message);
if (err->err == ZITI_NOT_AUTHORIZED) {
ziti_force_session_refresh(ztx);
restart_connect(conn);
}
else {
CONN_LOG(ERROR, "failed to get session for service[%s]: %s(%s)", conn->service, err->code, err->message);
complete_conn_req(conn, ZITI_SERVICE_UNAVAILABLE);
}
}
if (s == NULL) {
complete_conn_req(conn, ZITI_SERVICE_UNAVAILABLE);
uv_close((uv_handle_t *) ar, free_handle);
}
else {
Expand Down Expand Up @@ -1056,6 +1059,32 @@ int ziti_bind(ziti_connection conn, const char *service, ziti_listen_opts *liste
return uv_async_send(async_cr);
}

static void rebind_cb(ziti_connection conn, int status) {
if (status == ZITI_OK) {
CONN_LOG(DEBUG, "re-bound successfully");
} else {
CONN_LOG(DEBUG, "failed to re-bind [%d/%s]", status, ziti_errorstr(status));
conn->client_cb(conn, NULL, status, NULL);
}
}

// reset connection and Bind again with same options
static void ziti_rebind(ziti_connection conn) {
const char *service = conn->service;
struct ziti_conn_req *req = conn->conn_req;
ziti_listen_opts *opts = req->listen_opts;

conn->channel = NULL;

CONN_LOG(DEBUG, "rebinding to service[%s]", service);

ziti_bind(conn, service, opts, rebind_cb, conn->client_cb);

FREE(service);
free_conn_req(req);

}

int ziti_accept(ziti_connection conn, ziti_conn_cb cb, ziti_data_cb data_cb) {

ziti_channel_t *ch = conn->parent->channel;
Expand Down Expand Up @@ -1176,6 +1205,12 @@ int ziti_close_write(ziti_connection conn) {
static void process_edge_message(struct ziti_conn *conn, message *msg, int code) {

if (msg == NULL) {
if (conn->state == Bound) {
CONN_LOG(DEBUG, "binding lost due to failed channel [%d/%s]", code, ziti_errorstr(code));
ziti_rebind(conn);
return;
}

CONN_LOG(DEBUG, "closed due to err[%d](%s)", code, ziti_errorstr(code));
conn_state st = conn->state;
conn_set_state(conn, Disconnected);
Expand All @@ -1185,9 +1220,6 @@ static void process_edge_message(struct ziti_conn *conn, message *msg, int code)
case Accepting:
complete_conn_req(conn, code);
break;
case Bound:
conn->client_cb(conn, NULL, code, NULL);
break;
case Connected:
case CloseWrite:
conn->data_cb(conn, NULL, code);
Expand Down
2 changes: 2 additions & 0 deletions programs/host-proxy/host-proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ static void on_client(ziti_connection server, ziti_connection conn, int status,

uv_freeaddrinfo(resolve.addrinfo);
ziti_conn_set_data(hc->ziti_conn, hc);
} else {
fprintf(stderr, "hosting error: %d(%s)\n", status, ziti_errorstr(status));
}
}

Expand Down

0 comments on commit a48c51a

Please sign in to comment.