Skip to content

Commit

Permalink
Use fsnotify if using Directory and expose CompileTemplates (#93)
Browse files Browse the repository at this point in the history
* Further improve locking (RWMutex version)

As a second step improving #90, only lock when absolutely necessary
and reconstruct functions to ensure that the current templates are
referenced in the helper func instead of a global reference.

Closes #91

Signed-off-by: Andrew Thornton <[email protected]>

* Use fsnotify if using Directory and expose CompileTemplates

If setting IsDevelopment, if we can, use an FsWatcher to recompile the
templates in a separate goroutine. This should definitely increase the
performance of the Development server.

In order to make Asset based Renders have the same improvement - now
that render compilation properly locks the templates we can expose the
CompileTemplates function and allow downstream users to call this
independently when their templates need recompilation.

Contains #92

Signed-off-by: Andrew Thornton <[email protected]>
  • Loading branch information
zeripath authored May 26, 2021
1 parent 987556a commit 549aa82
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 27 deletions.
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ module github.com/unrolled/render

go 1.16

require github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385
require (
github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385
github.com/fsnotify/fsnotify v1.4.9
golang.org/x/sys v0.0.0-20210511113859-b0526f3d8744 // indirect
)
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385 h1:clC1lXBpe2kTj2VHdaIu9ajZQe4kcEY9j0NsnDDBZ3o=
github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385/go.mod h1:0vRUJqYpeSZifjYj7uP3BG/gKcuzL9xWVV/Y+cK33KM=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210511113859-b0526f3d8744 h1:yhBbb4IRs2HS9PPlAg6DMC6mUOKexJBNsLf4Z+6En1Q=
golang.org/x/sys v0.0.0-20210511113859-b0526f3d8744/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
87 changes: 61 additions & 26 deletions render.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"path/filepath"
"strings"
"sync"

"github.com/fsnotify/fsnotify"
)

const (
Expand Down Expand Up @@ -120,11 +122,13 @@ type HTMLOptions struct {
// Render is a service that provides functions for easily writing JSON, XML,
// binary data, and HTML templates out to a HTTP Response.
type Render struct {
lock sync.RWMutex

// Customize Secure with an Options struct.
opt Options
templates *template.Template
templatesLk sync.RWMutex
compiledCharset string
hasWatcher bool
}

// New constructs a new Render instance with the supplied options.
Expand All @@ -139,7 +143,7 @@ func New(options ...Options) *Render {
}

r.prepareOptions()
r.compileTemplates()
r.CompileTemplates()

return &r
}
Expand Down Expand Up @@ -186,7 +190,7 @@ func (r *Render) prepareOptions() {
}
}

func (r *Render) compileTemplates() {
func (r *Render) CompileTemplates() {
if r.opt.Asset == nil || r.opt.AssetNames == nil {
r.compileTemplatesFromDir()
return
Expand All @@ -199,12 +203,24 @@ func (r *Render) compileTemplatesFromDir() {
tmpTemplates := template.New(dir)
tmpTemplates.Delims(r.opt.Delims.Left, r.opt.Delims.Right)

var watcher *fsnotify.Watcher
if r.opt.IsDevelopment {
var err error
watcher, err = fsnotify.NewWatcher()
if err != nil {
log.Printf("Unable to create new watcher for template files. Templates will be recompiled on every render. Error: %v", err)
}
}

// Walk the supplied directory and compile any files that match our extension list.
r.opt.FileSystem.Walk(dir, func(path string, info os.FileInfo, err error) error {
// Fix same-extension-dirs bug: some dir might be named to: "users.tmpl", "local.html".
// These dirs should be excluded as they are not valid golang templates, but files under
// them should be treat as normal.
// If is a dir, return immediately (dir is not a valid golang template).
if info != nil && watcher != nil {
watcher.Add(path)
}
if info == nil || info.IsDir() {
return nil
}
Expand Down Expand Up @@ -242,9 +258,25 @@ func (r *Render) compileTemplatesFromDir() {
return nil
})

r.templatesLk.Lock()
r.lock.Lock()
defer r.lock.Unlock()
r.templates = tmpTemplates
r.templatesLk.Unlock()
if r.hasWatcher = watcher != nil; r.hasWatcher {
go func() {
select {
case _, ok := <-watcher.Events:
if !ok {
return
}
case _, ok := <-watcher.Errors:
if !ok {
return
}
}
watcher.Close()
r.CompileTemplates()
}()
}
}

func (r *Render) compileTemplatesFromAsset() {
Expand Down Expand Up @@ -289,28 +321,29 @@ func (r *Render) compileTemplatesFromAsset() {
}
}
}

r.templatesLk.Lock()
r.lock.Lock()
defer r.lock.Unlock()
r.templates = tmpTemplates
r.templatesLk.Unlock()
}

// TemplateLookup is a wrapper around template.Lookup and returns
// the template with the given name that is associated with t, or nil
// if there is no such template.
func (r *Render) TemplateLookup(t string) *template.Template {
r.lock.RLock()
defer r.lock.RUnlock()
return r.templates.Lookup(t)
}

func (r *Render) execute(name string, binding interface{}) (*bytes.Buffer, error) {
func (r *Render) execute(templates *template.Template, name string, binding interface{}) (*bytes.Buffer, error) {
buf := new(bytes.Buffer)
return buf, r.templates.ExecuteTemplate(buf, name, binding)
return buf, templates.ExecuteTemplate(buf, name, binding)
}

func (r *Render) layoutFuncs(name string, binding interface{}) template.FuncMap {
func (r *Render) layoutFuncs(templates *template.Template, name string, binding interface{}) template.FuncMap {
return template.FuncMap{
"yield": func() (template.HTML, error) {
buf, err := r.execute(name, binding)
buf, err := r.execute(templates, name, binding)
// Return safe HTML here since we are rendering our own template.
return template.HTML(buf.String()), err
},
Expand All @@ -320,23 +353,23 @@ func (r *Render) layoutFuncs(name string, binding interface{}) template.FuncMap
"block": func(partialName string) (template.HTML, error) {
log.Print("Render's `block` implementation is now depericated. Use `partial` as a drop in replacement.")
fullPartialName := fmt.Sprintf("%s-%s", partialName, name)
if r.TemplateLookup(fullPartialName) == nil && r.opt.RenderPartialsWithoutPrefix {
if templates.Lookup(fullPartialName) == nil && r.opt.RenderPartialsWithoutPrefix {
fullPartialName = partialName
}
if r.opt.RequireBlocks || r.TemplateLookup(fullPartialName) != nil {
buf, err := r.execute(fullPartialName, binding)
if r.opt.RequireBlocks || templates.Lookup(fullPartialName) != nil {
buf, err := r.execute(templates, fullPartialName, binding)
// Return safe HTML here since we are rendering our own template.
return template.HTML(buf.String()), err
}
return "", nil
},
"partial": func(partialName string) (template.HTML, error) {
fullPartialName := fmt.Sprintf("%s-%s", partialName, name)
if r.TemplateLookup(fullPartialName) == nil && r.opt.RenderPartialsWithoutPrefix {
if templates.Lookup(fullPartialName) == nil && r.opt.RenderPartialsWithoutPrefix {
fullPartialName = partialName
}
if r.opt.RequirePartials || r.TemplateLookup(fullPartialName) != nil {
buf, err := r.execute(fullPartialName, binding)
if r.opt.RequirePartials || templates.Lookup(fullPartialName) != nil {
buf, err := r.execute(templates, fullPartialName, binding)
// Return safe HTML here since we are rendering our own template.
return template.HTML(buf.String()), err
}
Expand Down Expand Up @@ -399,17 +432,19 @@ func (r *Render) Data(w io.Writer, status int, v []byte) error {
func (r *Render) HTML(w io.Writer, status int, name string, binding interface{}, htmlOpt ...HTMLOptions) error {

// If we are in development mode, recompile the templates on every HTML request.
if r.opt.IsDevelopment {
r.compileTemplates()
r.lock.RLock() // rlock here because we're reading the hasWatcher
if r.opt.IsDevelopment && !r.hasWatcher {
r.lock.RUnlock() // runlock here because CompileTemplates will lock
r.CompileTemplates()
r.lock.RLock()
}

r.templatesLk.RLock()
defer r.templatesLk.RUnlock()
templates := r.templates
r.lock.RUnlock()

opt := r.prepareHTMLOptions(htmlOpt)
if tpl := r.templates.Lookup(name); tpl != nil {
if tpl := templates.Lookup(name); tpl != nil {
if len(opt.Layout) > 0 {
tpl.Funcs(r.layoutFuncs(name, binding))
tpl.Funcs(r.layoutFuncs(templates, name, binding))
name = opt.Layout
}

Expand All @@ -426,7 +461,7 @@ func (r *Render) HTML(w io.Writer, status int, name string, binding interface{},
h := HTML{
Head: head,
Name: name,
Templates: r.templates,
Templates: templates,
bp: r.opt.BufferPool,
}

Expand Down

0 comments on commit 549aa82

Please sign in to comment.