Skip to content
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

filters/auth: release tokeninfo cache mutex earlier #3241

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 8 additions & 2 deletions filters/auth/tokeninfocache.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,19 @@ func (c *tokeninfoCache) cached(token string) map[string]any {
now := c.now()

c.mu.Lock()
defer c.mu.Unlock()

if e, ok := c.cache[token]; ok {
if now.Before(e.expiresAt) {
c.history.MoveToFront(e.href)
cachedInfo := e.info
c.mu.Unlock()

// It might be ok to return cached value
// without adjusting "expires_in" to avoid copy
// if caller never modifies the result and
// when "expires_in" did not change (same second)
// or for small TTL values
info := shallowCopyOf(e.info)
info := shallowCopyOf(cachedInfo)

elapsed := now.Sub(e.cachedAt).Truncate(time.Second).Seconds()
info[expiresInField] = info[expiresInField].(float64) - elapsed
Expand All @@ -81,6 +84,9 @@ func (c *tokeninfoCache) cached(token string) map[string]any {
c.history.Remove(e.href)
}
}

c.mu.Unlock()

return nil
}

Expand Down
2 changes: 2 additions & 0 deletions filters/auth/tokeninfocache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ func BenchmarkTokeninfoCache(b *testing.B) {

if bi.parallelism != 0 {
b.SetParallelism(bi.parallelism)
} else {
b.SetParallelism(1)
}

b.ReportAllocs()
Expand Down
Loading