Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/gohugoio/hugo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2022-02-17 18:51:19 +0300
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2022-02-17 20:47:36 +0300
commit929808190fb0649dcfa6def39cd1afe1d380087c (patch)
treea4405f12f93815351793da666b7ec0dc2d97a7a7 /tpl/tplimpl
parent667f3a4ba880b14f346161891cd43e2ba9ce9b9d (diff)
tpl/partials: Fix recently introduced deadlock in partials cache
The change in lock logic for `partialCached` in 0927cf739fee9646c7fb917965799d9acf080922 was naive as it didn't consider cached partials calling other cached partials. This changeset may look on the large side for this particular issue, but it pulls in part of a working branch, introducing `context.Context` in the template execution. Note that the context is only partially implemented in this PR, but the upcoming use cases will, as one example, include having access to the top "dot" (e.g. `Page`) all the way down into partials and shortcodes etc. The earlier benchmarks rerun against master: ```bash name old time/op new time/op delta IncludeCached-10 13.6ms ± 2% 13.8ms ± 1% ~ (p=0.343 n=4+4) name old alloc/op new alloc/op delta IncludeCached-10 5.30MB ± 0% 5.35MB ± 0% +0.96% (p=0.029 n=4+4) name old allocs/op new allocs/op delta IncludeCached-10 74.7k ± 0% 75.3k ± 0% +0.77% (p=0.029 n=4+4) ``` Fixes #9519
Diffstat (limited to 'tpl/tplimpl')
-rw-r--r--tpl/tplimpl/template.go8
-rw-r--r--tpl/tplimpl/template_funcs.go46
-rw-r--r--tpl/tplimpl/template_funcs_test.go14
3 files changed, 51 insertions, 17 deletions
diff --git a/tpl/tplimpl/template.go b/tpl/tplimpl/template.go
index 80e350f11..44b486404 100644
--- a/tpl/tplimpl/template.go
+++ b/tpl/tplimpl/template.go
@@ -15,6 +15,7 @@ package tplimpl
import (
"bytes"
+ "context"
"embed"
"io"
"io/fs"
@@ -225,6 +226,10 @@ func (t templateExec) Clone(d *deps.Deps) *templateExec {
}
func (t *templateExec) Execute(templ tpl.Template, wr io.Writer, data interface{}) error {
+ return t.ExecuteWithContext(context.Background(), templ, wr, data)
+}
+
+func (t *templateExec) ExecuteWithContext(ctx context.Context, templ tpl.Template, wr io.Writer, data interface{}) error {
if rlocker, ok := templ.(types.RLocker); ok {
rlocker.RLock()
defer rlocker.RUnlock()
@@ -249,11 +254,10 @@ func (t *templateExec) Execute(templ tpl.Template, wr io.Writer, data interface{
}
}
- execErr := t.executor.Execute(templ, wr, data)
+ execErr := t.executor.ExecuteWithContext(ctx, templ, wr, data)
if execErr != nil {
execErr = t.addFileContext(templ, execErr)
}
-
return execErr
}
diff --git a/tpl/tplimpl/template_funcs.go b/tpl/tplimpl/template_funcs.go
index 4b3abaada..831b846d0 100644
--- a/tpl/tplimpl/template_funcs.go
+++ b/tpl/tplimpl/template_funcs.go
@@ -16,6 +16,7 @@
package tplimpl
import (
+ "context"
"reflect"
"strings"
@@ -61,8 +62,9 @@ import (
)
var (
- _ texttemplate.ExecHelper = (*templateExecHelper)(nil)
- zero reflect.Value
+ _ texttemplate.ExecHelper = (*templateExecHelper)(nil)
+ zero reflect.Value
+ contextInterface = reflect.TypeOf((*context.Context)(nil)).Elem()
)
type templateExecHelper struct {
@@ -70,14 +72,27 @@ type templateExecHelper struct {
funcs map[string]reflect.Value
}
-func (t *templateExecHelper) GetFunc(tmpl texttemplate.Preparer, name string) (reflect.Value, bool) {
+func (t *templateExecHelper) GetFunc(ctx context.Context, tmpl texttemplate.Preparer, name string) (fn reflect.Value, firstArg reflect.Value, found bool) {
if fn, found := t.funcs[name]; found {
- return fn, true
+ if fn.Type().NumIn() > 0 {
+ first := fn.Type().In(0)
+ if first.Implements(contextInterface) {
+ // TODO(bep) check if we can void this conversion every time -- and if that matters.
+ // The first argument may be context.Context. This is never provided by the end user, but it's used to pass down
+ // contextual information, e.g. the top level data context (e.g. Page).
+ return fn, reflect.ValueOf(ctx), true
+ }
+ }
+
+ return fn, zero, true
}
- return zero, false
+ return zero, zero, false
+}
+
+func (t *templateExecHelper) Init(ctx context.Context, tmpl texttemplate.Preparer) {
}
-func (t *templateExecHelper) GetMapValue(tmpl texttemplate.Preparer, receiver, key reflect.Value) (reflect.Value, bool) {
+func (t *templateExecHelper) GetMapValue(ctx context.Context, tmpl texttemplate.Preparer, receiver, key reflect.Value) (reflect.Value, bool) {
if params, ok := receiver.Interface().(maps.Params); ok {
// Case insensitive.
keystr := strings.ToLower(key.String())
@@ -93,10 +108,11 @@ func (t *templateExecHelper) GetMapValue(tmpl texttemplate.Preparer, receiver, k
return v, v.IsValid()
}
-func (t *templateExecHelper) GetMethod(tmpl texttemplate.Preparer, receiver reflect.Value, name string) (method reflect.Value, firstArg reflect.Value) {
+func (t *templateExecHelper) GetMethod(ctx context.Context, tmpl texttemplate.Preparer, receiver reflect.Value, name string) (method reflect.Value, firstArg reflect.Value) {
if t.running {
// This is a hot path and receiver.MethodByName really shows up in the benchmarks,
// so we maintain a list of method names with that signature.
+ // TODO(bep) I have a branch that makes this construct superflous.
switch name {
case "GetPage", "Render":
if info, ok := tmpl.(tpl.Info); ok {
@@ -107,7 +123,21 @@ func (t *templateExecHelper) GetMethod(tmpl texttemplate.Preparer, receiver refl
}
}
- return receiver.MethodByName(name), zero
+ fn := receiver.MethodByName(name)
+ if !fn.IsValid() {
+ return zero, zero
+ }
+
+ if fn.Type().NumIn() > 0 {
+ first := fn.Type().In(0)
+ if first.Implements(contextInterface) {
+ // The first argument may be context.Context. This is never provided by the end user, but it's used to pass down
+ // contextual information, e.g. the top level data context (e.g. Page).
+ return fn, reflect.ValueOf(ctx)
+ }
+ }
+
+ return fn, zero
}
func newTemplateExecuter(d *deps.Deps) (texttemplate.Executer, map[string]reflect.Value) {
diff --git a/tpl/tplimpl/template_funcs_test.go b/tpl/tplimpl/template_funcs_test.go
index 711d1350d..6d2587bf7 100644
--- a/tpl/tplimpl/template_funcs_test.go
+++ b/tpl/tplimpl/template_funcs_test.go
@@ -15,6 +15,7 @@ package tplimpl
import (
"bytes"
+ "context"
"fmt"
"path/filepath"
"reflect"
@@ -145,8 +146,7 @@ func TestPartialCached(t *testing.T) {
partial := `Now: {{ now.UnixNano }}`
name := "testing"
- var data struct {
- }
+ var data struct{}
v := newTestConfig()
@@ -168,19 +168,19 @@ func TestPartialCached(t *testing.T) {
ns := partials.New(de)
- res1, err := ns.IncludeCached(name, &data)
+ res1, err := ns.IncludeCached(context.Background(), name, &data)
c.Assert(err, qt.IsNil)
for j := 0; j < 10; j++ {
time.Sleep(2 * time.Nanosecond)
- res2, err := ns.IncludeCached(name, &data)
+ res2, err := ns.IncludeCached(context.Background(), name, &data)
c.Assert(err, qt.IsNil)
if !reflect.DeepEqual(res1, res2) {
t.Fatalf("cache mismatch")
}
- res3, err := ns.IncludeCached(name, &data, fmt.Sprintf("variant%d", j))
+ res3, err := ns.IncludeCached(context.Background(), name, &data, fmt.Sprintf("variant%d", j))
c.Assert(err, qt.IsNil)
if reflect.DeepEqual(res1, res3) {
@@ -191,14 +191,14 @@ func TestPartialCached(t *testing.T) {
func BenchmarkPartial(b *testing.B) {
doBenchmarkPartial(b, func(ns *partials.Namespace) error {
- _, err := ns.Include("bench1")
+ _, err := ns.Include(context.Background(), "bench1")
return err
})
}
func BenchmarkPartialCached(b *testing.B) {
doBenchmarkPartial(b, func(ns *partials.Namespace) error {
- _, err := ns.IncludeCached("bench1", nil)
+ _, err := ns.IncludeCached(context.Background(), "bench1", nil)
return err
})
}