Skip to content

Commit

Permalink
Ziti conn improvement (#206)
Browse files Browse the repository at this point in the history
* improve ziti_connection lifecycle

[fixes #154]
- ziti_close() now takes a callback
- avoid crash if we receive close before connect reply
  • Loading branch information
ekoby authored Dec 21, 2020
1 parent 68871fc commit 6ee97bd
Show file tree
Hide file tree
Showing 12 changed files with 282 additions and 144 deletions.
30 changes: 14 additions & 16 deletions inc_internal/zt_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ limitations under the License.
#define UUID_STR_LEN 37
#endif

#define TYPE_DIAL "Dial"
#define TYPE_BIND "Bind"

typedef struct ziti_channel ziti_channel_t;

Expand All @@ -49,18 +51,8 @@ typedef void (*ch_connect_cb)(ziti_channel_t *ch, void *ctx, int status);

typedef void (*ch_notify_state)(ziti_channel_t *ch, ziti_router_status status, void *ctx);

enum conn_state {
Initial,
Connecting,
Connected,
Binding,
Bound,
Accepting,
Timedout,
CloseWrite,
Disconnected,
Closed
};
typedef int ch_state;
typedef int conn_state;

typedef struct ziti_channel {
uv_loop_t *loop;
Expand All @@ -77,7 +69,7 @@ typedef struct ziti_channel {
uint64_t latency;
uv_timer_t latency_timer;

enum conn_state state;
ch_state state;
uint32_t reconnect_count;

struct ch_conn_req **conn_reqs;
Expand Down Expand Up @@ -122,9 +114,11 @@ struct ziti_conn {
ziti_channel_t *channel;
ziti_data_cb data_cb;
ziti_client_cb client_cb;
enum conn_state state;
ziti_close_cb close_cb;
conn_state state;
bool fin_sent;
bool fin_recv;
bool close;
int timeout;

buffer *inbound;
Expand Down Expand Up @@ -205,12 +199,16 @@ struct ziti_ctx {
extern "C" {
#endif

void ziti_invalidate_session(ziti_context ztx, ziti_net_session *session, const char *service_id, const char *type);

void ziti_on_channel_event(ziti_channel_t *ch, ziti_router_status status, ziti_context ztx);

void ziti_force_session_refresh(ziti_context ztx);

int ziti_close_channels(ziti_context ztx);

bool ziti_channel_is_connected(ziti_channel_t *ch);

int ziti_channel_connect(ziti_context ztx, const char *name, const char *url, ch_connect_cb, void *ctx);

int ziti_channel_close(ziti_channel_t *ch);
Expand Down Expand Up @@ -243,12 +241,12 @@ void conn_inbound_data_msg(ziti_connection conn, message *msg);

int ziti_write_req(struct ziti_write_req_s *req);

int ziti_disconnect(struct ziti_conn *conn);

void on_write_completed(struct ziti_conn *conn, struct ziti_write_req_s *req, int status);

int close_conn_internal(struct ziti_conn *conn);

const char* ziti_conn_state(ziti_connection conn);

int establish_crypto(ziti_connection conn, message *msg);

void ziti_fmt_time(char *time_str, size_t time_str_len, uv_timeval64_t *tv);
Expand Down
15 changes: 14 additions & 1 deletion includes/ziti/ziti.h
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,15 @@ typedef void (*ziti_write_cb)(ziti_connection conn, ssize_t status, void *write_
*/
typedef void (*ziti_enroll_cb)(ziti_config *cfg, int status, char *err_message, void *enroll_ctx);

/**
* @brief Callback called after connection was closed.
*
* @param conn connection that was closed
*
* @see ziti_close()
*/
typedef void (*ziti_close_cb)(ziti_connection conn);

/**
* @brief Performs a Ziti enrollment.
*
Expand Down Expand Up @@ -689,12 +698,16 @@ extern int ziti_accept(ziti_connection clt, ziti_conn_cb cb, ziti_data_cb data_c
* When no longer needed a [connection](#ziti_connection) should be closed to gracefully disconnect. This
* function should be invoked after any status is returned which indicates an error situation.
*
* This method initiates the disconnect(if needed) and the release all associated resources.
* After close_cb() is called, the ziti_connection handle is no longer valid.
*
* @param conn the #ziti_connection to be closed
* @param close_cb callback called after connection is closed
*
* @return #ZITI_OK or corresponding #ZITI_ERRORS
*/
ZITI_FUNC
extern int ziti_close(ziti_connection *conn);
extern int ziti_close(ziti_connection conn, ziti_close_cb close_cb);

/**
* @brief Closes the outgoing (write) side of the given ziti connection.
Expand Down
16 changes: 14 additions & 2 deletions library/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ limitations under the License.
#define BACKOFF_TIME 3000 /* 3 seconds */
#define MAX_BACKOFF 5 /* max reconnection timeout: (1 << 5) * BACKOFF_TIME = 96 seconds */

enum ChannelState {
Initial,
Connecting,
Connected,
Disconnected,
Closed,
};

static void reconnect_channel(ziti_channel_t *ch);

static void on_channel_connect_internal(uv_connect_t *req, int status);
Expand Down Expand Up @@ -119,7 +127,7 @@ int ziti_close_channels(struct ziti_ctx *ziti) {
return ZITI_OK;
}

void ziti_close_cb(uv_handle_t *h) {
static void close_handle_cb(uv_handle_t *h) {
uv_mbed_t *mbed = (uv_mbed_t *) h;
ziti_channel_t *ch = mbed->_stream.data;

Expand All @@ -132,7 +140,7 @@ int ziti_channel_close(ziti_channel_t *ch) {
int r = 0;
if (ch->state != Closed) {
ZITI_LOG(INFO, "closing ch[%d](%s)", ch->id, ch->name);
r = uv_mbed_close(&ch->connection, ziti_close_cb);
r = uv_mbed_close(&ch->connection, close_handle_cb);
uv_timer_stop(&ch->latency_timer);
uv_close((uv_handle_t *) &ch->latency_timer, NULL);
ch->state = Closed;
Expand Down Expand Up @@ -160,6 +168,10 @@ void ziti_channel_rem_receiver(ziti_channel_t *ch, int id) {
}
}

bool ziti_channel_is_connected(ziti_channel_t *ch) {
return ch->state == Connected;
}

int ziti_channel_connect(ziti_context ztx, const char *ch_name, const char *url, ch_connect_cb cb, void *cb_ctx) {
ziti_channel_t *ch = model_map_get(&ztx->channels, ch_name);

Expand Down
Loading

0 comments on commit 6ee97bd

Please sign in to comment.