Skip to content

Commit

Permalink
refresh fixes
Browse files Browse the repository at this point in the history
- avoid overlapping service refresh cycles
- session_refresh should login if no current session
  • Loading branch information
ekoby committed Dec 22, 2020
1 parent 786ff59 commit bf8dad2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
4 changes: 2 additions & 2 deletions library/model_support.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ jsmntok_t* parse_tokens(jsmn_parser *parser, const char *json, size_t len, size_

int rc = jsmn_parse(parser, json, len, toks, tok_cap);
while (rc == JSMN_ERROR_NOMEM) {
toks = realloc(toks,(tok_cap *= 2) * sizeof(jsmntok_t));
ZITI_LOG(VERBOSE, "reallocating token array, new size = %zd", tok_cap);
toks = realloc(toks, (tok_cap *= 2) * sizeof(jsmntok_t));
ZITI_LOG(TRACE, "reallocating token array, new size = %zd", tok_cap);
rc = jsmn_parse(parser, json, len, toks, tok_cap);
}

Expand Down
19 changes: 16 additions & 3 deletions library/ziti.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ struct ziti_init_req {

int code_to_error(const char *code);

static void services_refresh(uv_timer_t *t);

static void version_cb(ziti_version *v, ziti_error *err, void *ctx);

static void session_cb(ziti_session *session, ziti_error *err, void *ctx);
Expand Down Expand Up @@ -502,8 +504,14 @@ static void session_refresh(uv_timer_t *t) {
struct ziti_init_req *req = calloc(1, sizeof(struct ziti_init_req));
req->ztx = ztx;

ZITI_LOG(DEBUG, "refreshing API session");
ziti_ctrl_current_api_session(&ztx->controller, session_cb, req);
if (ztx->session) {
ZITI_LOG(DEBUG, "refreshing API session");
ziti_ctrl_current_api_session(&ztx->controller, session_cb, req);
}
else {
ZITI_LOG(DEBUG, "requesting new API session");
ziti_ctrl_login(&ztx->controller, ztx->opts->config_types, session_cb, req);
}
}

void ziti_force_session_refresh(ziti_context ztx) {
Expand Down Expand Up @@ -538,6 +546,11 @@ static void update_services(ziti_service_array services, ziti_error *error, ziti
return;
}

// schedule next refresh
if (ztx->opts->refresh_interval > 0) {
ZITI_LOG(VERBOSE, "scheduling service refresh %ld seconds from now", ztx->opts->refresh_interval);
uv_timer_start(&ztx->refresh_timer, services_refresh, ztx->opts->refresh_interval * 1000, 0);
}
ZITI_LOG(VERBOSE, "processing service updates");

model_map updates = {0};
Expand Down Expand Up @@ -644,7 +657,7 @@ static void session_cb(ziti_session *session, ziti_error *err, void *ctx) {

if (ztx->opts->refresh_interval > 0 && !uv_is_active((const uv_handle_t *) &ztx->refresh_timer)) {
ZITI_LOG(DEBUG, "refresh_interval set to %ld seconds", ztx->opts->refresh_interval);
uv_timer_start(&ztx->refresh_timer, services_refresh, 0, ztx->opts->refresh_interval * 1000);
services_refresh(&ztx->refresh_timer);
}
else if (ztx->opts->refresh_interval == 0) {
ZITI_LOG(DEBUG, "refresh_interval not specified");
Expand Down

0 comments on commit bf8dad2

Please sign in to comment.