Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use RWMutex instead of Mutex for locking templates #90

Merged
merged 1 commit into from
May 12, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions render.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ type Render struct {
// Customize Secure with an Options struct.
opt Options
templates *template.Template
templatesLk sync.Mutex
templatesLk sync.RWMutex
compiledCharset string
}

Expand Down Expand Up @@ -196,8 +196,8 @@ func (r *Render) compileTemplates() {

func (r *Render) compileTemplatesFromDir() {
dir := r.opt.Directory
r.templates = template.New(dir)
r.templates.Delims(r.opt.Delims.Left, r.opt.Delims.Right)
tmpTemplates := template.New(dir)
tmpTemplates.Delims(r.opt.Delims.Left, r.opt.Delims.Right)

// 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 {
Expand Down Expand Up @@ -227,7 +227,7 @@ func (r *Render) compileTemplatesFromDir() {
}

name := (rel[0 : len(rel)-len(ext)])
tmpl := r.templates.New(filepath.ToSlash(name))
tmpl := tmpTemplates.New(filepath.ToSlash(name))

// Add our funcmaps.
for _, funcs := range r.opt.Funcs {
Expand All @@ -241,12 +241,16 @@ func (r *Render) compileTemplatesFromDir() {
}
return nil
})

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

func (r *Render) compileTemplatesFromAsset() {
dir := r.opt.Directory
r.templates = template.New(dir)
r.templates.Delims(r.opt.Delims.Left, r.opt.Delims.Right)
tmpTemplates := template.New(dir)
tmpTemplates.Delims(r.opt.Delims.Left, r.opt.Delims.Right)

for _, path := range r.opt.AssetNames() {
if !strings.HasPrefix(path, dir) {
Expand All @@ -272,7 +276,7 @@ func (r *Render) compileTemplatesFromAsset() {
}

name := (rel[0 : len(rel)-len(ext)])
tmpl := r.templates.New(filepath.ToSlash(name))
tmpl := tmpTemplates.New(filepath.ToSlash(name))

// Add our funcmaps.
for _, funcs := range r.opt.Funcs {
Expand All @@ -285,6 +289,10 @@ func (r *Render) compileTemplatesFromAsset() {
}
}
}

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

// TemplateLookup is a wrapper around template.Lookup and returns
Expand Down Expand Up @@ -389,14 +397,15 @@ func (r *Render) Data(w io.Writer, status int, v []byte) error {

// HTML builds up the response from the specified template and bindings.
func (r *Render) HTML(w io.Writer, status int, name string, binding interface{}, htmlOpt ...HTMLOptions) error {
r.templatesLk.Lock()
defer r.templatesLk.Unlock()

// If we are in development mode, recompile the templates on every HTML request.
if r.opt.IsDevelopment {
r.compileTemplates()
}

r.templatesLk.RLock()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this lock/unlock necessary? Since all other locks just in compileTemplates ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes because this is using the templates. We can't replace the templates until they're not being used by anyone else.

defer r.templatesLk.RUnlock()

opt := r.prepareHTMLOptions(htmlOpt)
if tpl := r.templates.Lookup(name); tpl != nil {
if len(opt.Layout) > 0 {
Expand Down