Skip to content

Commit e11e2d2

Browse files
committed
pr comments
1 parent f6e2c26 commit e11e2d2

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

coderd/cryptokeys/cache.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,18 @@ func WithCacheClock(clock quartz.Clock) CacheOption {
101101
}
102102
}
103103

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

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

292295
if c.closed {
293-
c.mu.Unlock()
294296
return
295297
}
296298

297299
// If something's already fetching, we don't need to do anything.
298300
if c.fetching {
299-
c.mu.Unlock()
300301
return
301302
}
302303

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

@@ -317,8 +317,8 @@ func (c *cache) refresh() {
317317
return
318318
}
319319

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

323323
c.lastFetch = c.clock.Now()
324324
c.refresher.Reset(refreshInterval)

0 commit comments

Comments
 (0)