Avoid race condition in target list init

As reported by Go's race detector.

See #917
This commit is contained in:
bep 2015-02-20 18:38:35 +01:00
parent c33a8528f8
commit dc7b7ef865

View file

@ -66,23 +66,24 @@ var DefaultTimer *nitro.B
// //
// 5. The entire collection of files is written to disk. // 5. The entire collection of files is written to disk.
type Site struct { type Site struct {
Pages Pages Pages Pages
Files []*source.File Files []*source.File
Tmpl tpl.Template Tmpl tpl.Template
Taxonomies TaxonomyList Taxonomies TaxonomyList
Source source.Input Source source.Input
Sections Taxonomy Sections Taxonomy
Info SiteInfo Info SiteInfo
Shortcodes map[string]ShortcodeFunc Shortcodes map[string]ShortcodeFunc
Menus Menus Menus Menus
timer *nitro.B timer *nitro.B
Targets targetList Targets targetList
Completed chan bool targetListInit sync.Once
RunMode runmode Completed chan bool
params map[string]interface{} RunMode runmode
draftCount int params map[string]interface{}
futureCount int draftCount int
Data map[string]interface{} futureCount int
Data map[string]interface{}
} }
type targetList struct { type targetList struct {
@ -1454,34 +1455,41 @@ func (s *Site) NewXMLBuffer() *bytes.Buffer {
} }
func (s *Site) PageTarget() target.Output { func (s *Site) PageTarget() target.Output {
if s.Targets.Page == nil { s.initTargetList()
s.Targets.Page = &target.PagePub{
PublishDir: s.absPublishDir(),
UglyUrls: viper.GetBool("UglyUrls"),
}
}
return s.Targets.Page return s.Targets.Page
} }
func (s *Site) FileTarget() target.Output { func (s *Site) FileTarget() target.Output {
if s.Targets.File == nil { s.initTargetList()
s.Targets.File = &target.Filesystem{
PublishDir: s.absPublishDir(),
}
}
return s.Targets.File return s.Targets.File
} }
func (s *Site) AliasTarget() target.AliasPublisher { func (s *Site) AliasTarget() target.AliasPublisher {
if s.Targets.Alias == nil { s.initTargetList()
s.Targets.Alias = &target.HTMLRedirectAlias{
PublishDir: s.absPublishDir(),
}
}
return s.Targets.Alias return s.Targets.Alias
} }
func (s *Site) initTargetList() {
s.targetListInit.Do(func() {
if s.Targets.Page == nil {
s.Targets.Page = &target.PagePub{
PublishDir: s.absPublishDir(),
UglyUrls: viper.GetBool("UglyUrls"),
}
}
if s.Targets.File == nil {
s.Targets.File = &target.Filesystem{
PublishDir: s.absPublishDir(),
}
}
if s.Targets.Alias == nil {
s.Targets.Alias = &target.HTMLRedirectAlias{
PublishDir: s.absPublishDir(),
}
}
})
}
func (s *Site) WriteDestFile(path string, reader io.Reader) (err error) { func (s *Site) WriteDestFile(path string, reader io.Reader) (err error) {
jww.DEBUG.Println("creating file:", path) jww.DEBUG.Println("creating file:", path)
return s.FileTarget().Publish(path, reader) return s.FileTarget().Publish(path, reader)