mirror of
https://github.com/openziti/ziti-sdk-c.git
synced 2026-11-15 20:53:50 +00:00
Use exponential backoff for OIDC refresh retries.
Replace fixed 5s retry interval and 60-failure threshold with randomized exponential backoff (5s-60s) that utilizes the full token refresh window. Escalate to full re-auth only when the token actually expires, not after a fixed number of attempts.
This commit is contained in:
committed by
eugene
parent
64097b6c89
commit
ec0555b551
@@ -2,3 +2,4 @@
|
||||
Nic Fragale <nic.fragale@netfoundry.io>
|
||||
Tom Carroll <Thomas.Carroll@pnnl.gov>
|
||||
Mario Trangoni <mario.trangoni@gmail.com>
|
||||
Dominik Münsterer <dominik.muensterer@deltasecure.de>
|
||||
|
||||
@@ -62,6 +62,7 @@ struct oidc_client_s {
|
||||
tlsuv_http_req_t *refresh_req;
|
||||
int refresh_failures;
|
||||
int total_refresh_failures;
|
||||
uint64_t token_expiry;
|
||||
};
|
||||
|
||||
// init
|
||||
|
||||
+25
-5
@@ -108,6 +108,7 @@ int oidc_client_init(uv_loop_t *loop, oidc_client_t *clt,
|
||||
tlsuv_http_idle_keepalive(&clt->http, 0);
|
||||
tlsuv_http_header(&clt->http, HTTP_ACCEPT, APPLICATION_JSON);
|
||||
|
||||
clt->token_expiry = 0;
|
||||
clt->timer = calloc(1, sizeof(*clt->timer));
|
||||
uv_timer_init(loop, clt->timer);
|
||||
clt->timer->data = clt;
|
||||
@@ -576,6 +577,7 @@ static void oidc_client_set_tokens(oidc_client_t *clt, json_object *tok_json) {
|
||||
}
|
||||
if (ttl) {
|
||||
int32_t t = json_object_get_int(ttl);
|
||||
clt->token_expiry = uv_now(clt->timer->loop) + (uint64_t)t * 1000;
|
||||
t = t / 2; // refresh at half the token lifetime
|
||||
OIDC_LOG(DEBUG, "scheduling token refresh in %d seconds", t);
|
||||
uv_timer_start(clt->timer, refresh_time_cb, t * 1000, 0);
|
||||
@@ -606,13 +608,15 @@ static void refresh_cb(tlsuv_http_resp_t *http_resp, const char *err, json_objec
|
||||
OIDC_LOG(WARN, "OIDC token refresh failed (%d/%s), attempt %d (total: %d)",
|
||||
http_resp->code, err, clt->refresh_failures, clt->total_refresh_failures);
|
||||
|
||||
// after sustained failure (5 min at 5s intervals), give up on refresh and restart full auth
|
||||
if (clt->total_refresh_failures >= 60) {
|
||||
OIDC_LOG(WARN, "OIDC token refresh has failed %d times, restarting full authentication",
|
||||
// token expired, give up on refresh and restart full auth
|
||||
uint64_t now = uv_now(clt->timer->loop);
|
||||
if (clt->token_expiry > 0 && now >= clt->token_expiry) {
|
||||
OIDC_LOG(WARN, "OIDC token has expired after %d refresh attempts, restarting full authentication",
|
||||
clt->total_refresh_failures);
|
||||
tlsuv_http_cancel_all(&clt->http);
|
||||
clt->refresh_failures = 0;
|
||||
clt->total_refresh_failures = 0;
|
||||
clt->token_expiry = 0;
|
||||
json_object_put(clt->tokens);
|
||||
clt->tokens = NULL;
|
||||
oidc_client_start(clt, clt->token_cb);
|
||||
@@ -624,11 +628,26 @@ static void refresh_cb(tlsuv_http_resp_t *http_resp, const char *err, json_objec
|
||||
tlsuv_http_cancel_all(&clt->http);
|
||||
clt->refresh_failures = 0;
|
||||
}
|
||||
uv_timer_start(clt->timer, refresh_time_cb, 5 * 1000, 0);
|
||||
|
||||
// exponential backoff with jitter to avoid stampeding controller after outage
|
||||
int shift = clt->total_refresh_failures - 1;
|
||||
if (shift > 4) shift = 4; // cap at 80s base
|
||||
uint64_t delay = 5000ULL << shift;
|
||||
if (delay > 60000) delay = 60000;
|
||||
// jitter: [delay/2, delay]
|
||||
uint32_t half = (uint32_t)(delay / 2);
|
||||
if (half > 0) delay = half + randombytes_uniform(half + 1);
|
||||
// clamp to remaining token lifetime
|
||||
if (clt->token_expiry > 0 && now + delay > clt->token_expiry) {
|
||||
delay = clt->token_expiry - now;
|
||||
}
|
||||
|
||||
OIDC_LOG(DEBUG, "scheduling token refresh retry in %llu ms", (unsigned long long)delay);
|
||||
uv_timer_start(clt->timer, refresh_time_cb, delay, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
// http_resp->code >= 0 but not 200: server-side rejection (e.g. EOF, 401, etc.)
|
||||
// http_resp->code > 0 but not 200: server-side rejection (e.g. 401, 403, etc.)
|
||||
OIDC_LOG(WARN, "OIDC token refresh failed: %d %s [%s]",
|
||||
http_resp->code, http_resp->status, err);
|
||||
if (resp) {
|
||||
@@ -636,6 +655,7 @@ static void refresh_cb(tlsuv_http_resp_t *http_resp, const char *err, json_objec
|
||||
}
|
||||
clt->refresh_failures = 0;
|
||||
clt->total_refresh_failures = 0;
|
||||
clt->token_expiry = 0;
|
||||
json_object_put(clt->tokens);
|
||||
clt->tokens = NULL;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user