Skip to content

Commit eb0ae8b

Browse files
author
Apti Umayev
committed
Fix cache mutex lock
1 parent 505e4d7 commit eb0ae8b

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

Diff for: middleware/cache/cache.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,8 @@ func New(config ...Config) fiber.Handler {
7070
// Get entry from pool
7171
e := manager.get(key)
7272

73-
// Lock entry and unlock when finished
73+
// Lock entry
7474
mux.Lock()
75-
defer mux.Unlock()
7675

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

106105
c.Set(cfg.CacheHeader, cacheHit)
107106

107+
mux.Unlock()
108+
108109
// Return response
109110
return nil
110111
}
111112

113+
// make sure we're not blocking concurrent requests - do unlock
114+
mux.Unlock()
115+
112116
// Continue stack, return err to Fiber if exist
113117
if err := c.Next(); err != nil {
114118
return err
115119
}
116120

121+
// lock entry back and unlock on finish
122+
mux.Lock()
123+
defer mux.Unlock()
124+
117125
// Don't cache response if Next returns true
118126
if cfg.Next != nil && cfg.Next(c) {
119127
c.Set(cfg.CacheHeader, cacheUnreachable)

0 commit comments

Comments
 (0)