-
Notifications
You must be signed in to change notification settings - Fork 18.8k
Description
Hey Go team,
I am interested in redefinition of templates. Implicit redefinition is disallowed. How about explicit redefinition? Either:
Template.Remove(name string)followed byTemplate.ParseFiles(names ...string)Template.Redefine(name, body string)/Template.RedefineFile(name, file string)
Scenario
We have a server which watches a folder for updates. When it receives a write or create for a file, it re-reads in the template. This allows for no-downtime updates to our templates, which is something we value greatly. It also allows for a more streamlined development process (developer isn't spending time restarting the server locally on changes).
With each filesystem event, we receive the event type and the file it affects. We would like to be able to redefine this template at runtime as these events are triggered.
Currently...
// Template has been changes on the filesystem.
// Re-read and parse. Store the latest.
// Always throws the 'redefinition' error.
_, err := rootTmpl.ParseFiles(event.FileName)
if err != nil {
log.Printf("error reading template '%s': %v", event.FileName, err)
}Unfortunately, this fails every time, with template: redefinition of template %NAME%.
Proposal
My proposal is to allow an explicit removal or redefinition. An example with Template.Remove(name string)...
// Allow explicit removal of a template to allow redefinition.
err := rootTmpl.Remove(event.FileName)
if err != nil && err != template.ErrorNoSuchTemplate {
log.Printf("error removing template '%s': %v", event.FileName, err)
}
// This should NOT return an error.
_, err = rootTmpl.ParseFiles(event.FileName)
if err != nil {
log.Printf("error reading template '%s': %v", event.FileName, err)
}Current Work-Around
To get around this, we bind to SIGUSR1 and re-read all the templates (Template.ParseFilesGlob), blocking requests until it's re-defined.