tpl/partials: Fix the lock contention in cached partial
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 16 Nov 2017 10:59:45 +0000 (11:59 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 16 Nov 2017 10:59:45 +0000 (11:59 +0100)
Fixes #4086

tpl/partials/partials.go

index d999041be163ef8fbbb50af01128ff2e058d3d16..beb09f426bc55bfc14b15a2f963c05d7ef0910a4 100644 (file)
@@ -120,19 +120,17 @@ func (ns *Namespace) getOrCreate(key, name string, context interface{}) (interfa
                return p, nil
        }
 
-       ns.cachedPartials.Lock()
-       defer ns.cachedPartials.Unlock()
-
-       // Double-check.
-       if p, ok = ns.cachedPartials.p[key]; ok {
-               return p, nil
-       }
-
        p, err := ns.Include(name, context)
        if err != nil {
                return nil, err
        }
 
+       ns.cachedPartials.Lock()
+       defer ns.cachedPartials.Unlock()
+       // Double-check.
+       if p2, ok := ns.cachedPartials.p[key]; ok {
+               return p2, nil
+       }
        ns.cachedPartials.p[key] = p
 
        return p, nil