Background task that renews the sandbox JWT at ~80% of its remaining lifetime. The new token replaces the value in [`TOKEN_SLOT`], so all in-flight and future clients pick it up on their next request. The loop never panics: every failure is logged and re-attempted after a bounded backoff.
(
channel: AuthedChannel,
slot: TokenSlot,
source: TokenSource,
endpoint: String,
plain_channel: Channel,
)
| 322 | /// loop never panics: every failure is logged and re-attempted after a |
| 323 | /// bounded backoff. |
| 324 | async fn refresh_token_loop( |
| 325 | channel: AuthedChannel, |
| 326 | slot: TokenSlot, |
| 327 | source: TokenSource, |
| 328 | endpoint: String, |
| 329 | plain_channel: Channel, |
| 330 | ) { |
| 331 | let mut client = OpenShellClient::new(channel); |
| 332 | loop { |
| 333 | let sleep = compute_refresh_delay(&slot); |
| 334 | tokio::time::sleep(sleep).await; |
| 335 | match client |
| 336 | .refresh_sandbox_token(RefreshSandboxTokenRequest {}) |
| 337 | .await |
| 338 | { |
| 339 | Ok(resp) => { |
| 340 | let new_token = resp.into_inner().token; |
| 341 | match AsciiMetadataValue::try_from(format!("Bearer {new_token}")) { |
| 342 | Ok(value) => { |
| 343 | if let Ok(mut guard) = slot.write() { |
| 344 | *guard = value; |
| 345 | info!("renewed gateway sandbox JWT in-place"); |
| 346 | } |
| 347 | } |
| 348 | Err(e) => warn!(error = %e, "refreshed JWT contained invalid header bytes"), |
| 349 | } |
| 350 | } |
| 351 | Err(status) => { |
| 352 | if status.code() == tonic::Code::Unauthenticated |
| 353 | && source == TokenSource::K8sServiceAccount |
| 354 | { |
| 355 | if let Some(sa_path) = std::env::var(sandbox_env::K8S_SA_TOKEN_FILE) |
| 356 | .ok() |
| 357 | .filter(|p| !p.is_empty()) |
| 358 | { |
| 359 | match acquire_k8s_sandbox_token(&endpoint, &plain_channel, &sa_path).await { |
| 360 | Ok(new_token) => { |
| 361 | match AsciiMetadataValue::try_from(format!("Bearer {new_token}")) { |
| 362 | Ok(value) => { |
| 363 | if let Ok(mut guard) = slot.write() { |
| 364 | *guard = value; |
| 365 | info!( |
| 366 | "rebootstrapped gateway sandbox JWT after refresh authentication failure" |
| 367 | ); |
| 368 | continue; |
| 369 | } |
| 370 | } |
| 371 | Err(e) => warn!( |
| 372 | error = %e, |
| 373 | "rebootstrapped JWT contained invalid header bytes" |
| 374 | ), |
| 375 | } |
| 376 | } |
| 377 | Err(e) => warn!( |
| 378 | error = %e, |
| 379 | "K8s ServiceAccount bootstrap retry failed after refresh authentication failure" |
| 380 | ), |
| 381 | } |
no test coverage detected