Skip to content

Commit

Permalink
Fix cache mutex lock
Browse files Browse the repository at this point in the history
  • Loading branch information
Apti Umayev committed Feb 11, 2022
1 parent 505e4d7 commit eb0ae8b
Showing 1 changed file with 10 additions and 2 deletions.
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

0 comments on commit eb0ae8b

Please sign in to comment.