Skip to content

Commit

Permalink
Merge pull request #213 from openziti/optimize-service-refresh
Browse files Browse the repository at this point in the history
check if update needed before service refresh
  • Loading branch information
ekoby authored Jan 21, 2021
2 parents a6a41d3 + ec486e7 commit 090ec43
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
name: C/C++ CI

on:
push:
branches: [ '**' ]
pull_request:
branches: [ master ]

Expand Down
6 changes: 6 additions & 0 deletions inc_internal/internal_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ XX(identity, ziti_identity, ptr, identity, __VA_ARGS__) \
XX(posture_query_set, ziti_posture_query_set, array, postureQueries, __VA_ARGS__)

#define ZITI_ERROR_MODEL(XX, ...) \
XX(http_code, int, none, , __VA_ARGS__) \
XX(code, string, none, code, __VA_ARGS__) \
XX(message, string, none, message, __VA_ARGS__)

Expand Down Expand Up @@ -111,6 +112,9 @@ XX(is_running, bool, none, isRunning, __VA_ARGS__) \
XX(hash, string, none, hash, __VA_ARGS__) \
XX(signers, string, array, signerFingerprints, __VA_ARGS__)

#define ZITI_SERVICE_UPDATE(XX, ...) \
XX(last_change, string, none, lastChangeAt, __VA_ARGS__)

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -148,6 +152,8 @@ DECLARE_MODEL(ziti_pr_process_req, ZITI_PR_PROCESS_REQ)

DECLARE_MODEL(ziti_pr_domain_req, ZITI_PR_DOMAIN_REQ)

DECLARE_MODEL(ziti_service_update, ZITI_SERVICE_UPDATE)

#ifdef __cplusplus
}
#endif
Expand Down
2 changes: 2 additions & 0 deletions inc_internal/ziti_ctrl.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ void ziti_ctrl_current_api_session(ziti_controller *ctrl, void(*cb)(ziti_session

void ziti_ctrl_logout(ziti_controller *ctrl, void(*cb)(void*, ziti_error*, void*), void *ctx);

void ziti_ctrl_get_services_update(ziti_controller *ctrl, void (*cb)(ziti_service_update*, ziti_error*, void*), void *ctx);

void ziti_ctrl_get_services(ziti_controller *ctrl, void (*srv_cb)(ziti_service_array, ziti_error *, void *), void *ctx);

void ziti_ctrl_get_service(ziti_controller *ctrl, const char* service_name, void (*srv_cb)(ziti_service *, ziti_error*, void*), void* ctx);
Expand Down
3 changes: 3 additions & 0 deletions inc_internal/zt_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ struct ziti_ctx {
// map<service_id,ziti_net_session>
model_map sessions;

bool no_service_updates_api; // controller API has no last-update endpoint
char *last_update;

uv_timer_t session_timer;
uv_timer_t refresh_timer;
uv_prepare_t reaper;
Expand Down
2 changes: 2 additions & 0 deletions library/internal_model.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ IMPL_MODEL(ziti_pr_process_req, ZITI_PR_PROCESS_REQ)

IMPL_MODEL(ziti_pr_domain_req, ZITI_PR_DOMAIN_REQ)

IMPL_MODEL(ziti_service_update, ZITI_SERVICE_UPDATE)

const char *ziti_service_get_raw_config(ziti_service *service, const char *cfg_type) {
return (const char *) model_map_get(&service->config, cfg_type);
}
Expand Down
4 changes: 2 additions & 2 deletions library/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ static const char *get_elapsed_time() {
uint64_t now = uv_now(ts_loop);
if (now > last_update) {
last_update = now;
uint64_t elapsed = now - starttime;
snprintf(elapsed_buffer, sizeof(elapsed_buffer), "%9ld.%03ld", elapsed / 1000, elapsed % 1000);
unsigned long long elapsed = now - starttime;
snprintf(elapsed_buffer, sizeof(elapsed_buffer), "%9lld.%03lld", (elapsed / 1000), (elapsed % 1000));
}
return elapsed_buffer;
}
Expand Down
36 changes: 35 additions & 1 deletion library/ziti.c
Original file line number Diff line number Diff line change
Expand Up @@ -617,9 +617,43 @@ static void update_services(ziti_service_array services, ziti_error *error, void
model_map_clear(&updates, NULL);
}

static void check_service_update(ziti_service_update *update, ziti_error *err, void *ctx) {
ziti_context ztx = ctx;
bool need_update = true;

if (err) { // API not supported - do refresh
if (err->http_code == 404) {
ZITI_LOG(INFO, "Controller does not support /current-api-session/service-updates API");
ztx->no_service_updates_api = true;
}
}
else if (ztx->last_update == NULL || strcmp(ztx->last_update, update->last_change) != 0) {
ZITI_LOG(VERBOSE, "ztx last_update = %s", update->last_change);
FREE(ztx->last_update);
ztx->last_update = update->last_change;
} else {
ZITI_LOG(VERBOSE, "not updating: last_update is same previous (%s == %s)", update->last_change,
ztx->last_update);
free_ziti_service_update(update);
need_update = false;

uv_timer_start(&ztx->refresh_timer, services_refresh, ztx->opts->refresh_interval * 1000, 0);
}

if (need_update) {
ziti_ctrl_get_services(&ztx->controller, update_services, ztx);
}
FREE(update);
}

static void services_refresh(uv_timer_t *t) {
ziti_context ztx = t->data;
ziti_ctrl_get_services(&ztx->controller, update_services, ztx);
if (ztx->no_service_updates_api) {
ziti_ctrl_get_services(&ztx->controller, update_services, ztx);
}
else {
ziti_ctrl_get_services_update(&ztx->controller, check_service_update, ztx);
}
}

static void session_cb(ziti_session *session, ziti_error *err, void *ctx) {
Expand Down
15 changes: 15 additions & 0 deletions library/ziti_ctrl.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ static void ctrl_body_cb(um_http_req_t *req, const char *b, ssize_t len) {
}
}

if (cr.error) {
cr.error->http_code = req->resp.code;
}

free_resp_meta(&cr.meta);
FREE(cr.data);
FREE(resp->body);
Expand Down Expand Up @@ -354,6 +358,17 @@ void ziti_ctrl_logout(ziti_controller *ctrl, void(*cb)(void *, ziti_error *, voi
um_http_req(&ctrl->client, "DELETE", "/current-api-session", ctrl_resp_cb, resp);
}

void ziti_ctrl_get_services_update(ziti_controller *ctrl, void (*cb)(ziti_service_update*, ziti_error*, void*), void *ctx) {
struct ctrl_resp *resp = calloc(1, sizeof(struct ctrl_resp));
resp->body_parse_func = (int (*)(void *, const char *, size_t)) parse_ziti_service_update_ptr;
resp->resp_cb = (void (*)(void *, ziti_error *, void *)) cb;
resp->ctx = ctx;
resp->ctrl = ctrl;
resp->ctrl_cb = ctrl_default_cb;

um_http_req(&ctrl->client, "GET", "/current-api-session/service-updates", ctrl_resp_cb, resp);
}

void ziti_ctrl_get_services(ziti_controller *ctrl, void (*cb)(ziti_service_array, ziti_error *, void *), void *ctx) {

struct ctrl_resp *resp = calloc(1, sizeof(struct ctrl_resp));
Expand Down

0 comments on commit 090ec43

Please sign in to comment.