Skip to content

chore: refactor keycache implementation to reduce duplication #15100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 16, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
pr comments
  • Loading branch information
sreya committed Oct 16, 2024
commit e11e2d216fe264f98a10cc769fb20da8cfe0869b
16 changes: 8 additions & 8 deletions coderd/cryptokeys/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,18 @@ func WithCacheClock(clock quartz.Clock) CacheOption {
}
}

// NewSigningCache instantiates a cache. Close should be called to
// release resources associated with its internal timer.
func NewSigningCache(ctx context.Context, logger slog.Logger, fetcher Fetcher, feature codersdk.CryptoKeyFeature, opts ...func(*cache)) (SigningKeycache, error) {
// NewSigningCache instantiates a cache. Close should be called to release resources
// associated with its internal timer.
func NewSigningCache(ctx context.Context, logger slog.Logger, fetcher Fetcher,
feature codersdk.CryptoKeyFeature, opts ...func(*cache)) (SigningKeycache, error) {
if !isSigningKeyFeature(feature) {
return nil, xerrors.Errorf("invalid feature: %s", feature)
}
return newCache(ctx, logger, fetcher, feature, opts...)
}

func NewEncryptionCache(ctx context.Context, logger slog.Logger, fetcher Fetcher, feature codersdk.CryptoKeyFeature, opts ...func(*cache)) (EncryptionKeycache, error) {
func NewEncryptionCache(ctx context.Context, logger slog.Logger, fetcher Fetcher,
feature codersdk.CryptoKeyFeature, opts ...func(*cache)) (EncryptionKeycache, error) {
if !isEncryptionKeyFeature(feature) {
return nil, xerrors.Errorf("invalid feature: %s", feature)
}
Expand Down Expand Up @@ -288,23 +290,21 @@ func checkKey(key codersdk.CryptoKey, sequence int32, now time.Time) (string, []
func (c *cache) refresh() {
now := c.clock.Now("CryptoKeyCache", "refresh")
c.mu.Lock()
defer c.mu.Unlock()

if c.closed {
c.mu.Unlock()
return
}

// If something's already fetching, we don't need to do anything.
if c.fetching {
c.mu.Unlock()
return
}

// There's a window we must account for where the timer fires while a fetch
// is ongoing but prior to the timer getting reset. In this case we want to
// avoid double fetching.
if now.Sub(c.lastFetch) < refreshInterval {
c.mu.Unlock()
return
}

Expand All @@ -317,8 +317,8 @@ func (c *cache) refresh() {
return
}

// We don't defer an unlock here due to the deferred unlock at the top of the function.
c.mu.Lock()
defer c.mu.Unlock()

c.lastFetch = c.clock.Now()
c.refresher.Reset(refreshInterval)
Expand Down
Loading