refresh fetches the keys and updates the cache.
()
| 301 | |
| 302 | // refresh fetches the keys and updates the cache. |
| 303 | func (c *cache) refresh() { |
| 304 | now := c.clock.Now("CryptoKeyCache", "refresh") |
| 305 | c.mu.Lock() |
| 306 | |
| 307 | if c.closed { |
| 308 | c.mu.Unlock() |
| 309 | return |
| 310 | } |
| 311 | |
| 312 | // If something's already fetching, we don't need to do anything. |
| 313 | if c.fetching { |
| 314 | c.mu.Unlock() |
| 315 | return |
| 316 | } |
| 317 | |
| 318 | // There's a window we must account for where the timer fires while a fetch |
| 319 | // is ongoing but prior to the timer getting reset. In this case we want to |
| 320 | // avoid double fetching. |
| 321 | if now.Sub(c.lastFetch) < refreshInterval { |
| 322 | c.mu.Unlock() |
| 323 | return |
| 324 | } |
| 325 | |
| 326 | c.fetching = true |
| 327 | |
| 328 | c.mu.Unlock() |
| 329 | keys, err := c.cryptoKeys(c.ctx) |
| 330 | if err != nil { |
| 331 | c.logger.Error(c.ctx, "fetch crypto keys", slog.Error(err)) |
| 332 | return |
| 333 | } |
| 334 | |
| 335 | c.mu.Lock() |
| 336 | defer c.mu.Unlock() |
| 337 | |
| 338 | c.lastFetch = c.clock.Now() |
| 339 | c.refresher.Reset(refreshInterval) |
| 340 | c.keys = keys |
| 341 | c.fetching = false |
| 342 | c.cond.Broadcast() |
| 343 | } |
| 344 | |
| 345 | // cryptoKeys queries the control plane for the crypto keys. |
| 346 | // Outside of initialization, this should only be called by fetch. |
no test coverage detected