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

Fix cache mutex lock #1764

Merged
merged 1 commit into from Feb 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions middleware/cache/cache.go
Expand Up @@ -70,9 +70,8 @@ func New(config ...Config) fiber.Handler {
// Get entry from pool
e := manager.get(key)

// Lock entry and unlock when finished
// Lock entry
mux.Lock()
defer mux.Unlock()

// Get timestamp
ts := atomic.LoadUint64(&timestamp)
Expand Down Expand Up @@ -105,15 +104,24 @@ func New(config ...Config) fiber.Handler {

c.Set(cfg.CacheHeader, cacheHit)

mux.Unlock()

// Return response
return nil
}

// make sure we're not blocking concurrent requests - do unlock
mux.Unlock()

// Continue stack, return err to Fiber if exist
if err := c.Next(); err != nil {
return err
}

// lock entry back and unlock on finish
mux.Lock()
defer mux.Unlock()

// Don't cache response if Next returns true
if cfg.Next != nil && cfg.Next(c) {
c.Set(cfg.CacheHeader, cacheUnreachable)
Expand Down