From 8e8538175b74e9eb78c385a242601515cc0bc40e Mon Sep 17 00:00:00 2001 From: "Anand Babu (AB) Periasamy" Date: Thu, 3 Dec 2015 01:04:21 -0800 Subject: [PATCH] fixes race in Init --- pkg/contentdb/contentdb.go | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/pkg/contentdb/contentdb.go b/pkg/contentdb/contentdb.go index c4945a966..c8a074ef5 100644 --- a/pkg/contentdb/contentdb.go +++ b/pkg/contentdb/contentdb.go @@ -19,13 +19,18 @@ package contentdb import ( + "errors" "fmt" "strings" + "sync" "encoding/json" ) var ( + // Internal lock. + mutex = &sync.Mutex{} + // Make note of initialization. isInitialized = false @@ -78,20 +83,26 @@ func loadDB() error { // Init initializes contentdb for lookups. JSON structure is parsed into a simple map of extension and content-type. func Init() error { - var e error - extDB = make(map[string]string) + mutex.Lock() + defer mutex.Unlock() if !isInitialized { - e = loadDB() + var e error + extDB = make(map[string]string) + + if !isInitialized { + e = loadDB() + } + isInitialized = true + return e } - isInitialized = true - return e + return nil } // Lookup returns matching content-type for known types of file extensions. func Lookup(extension string) (contentType string, e error) { if !isInitialized { - e = Init() + return "", errors.New("contentdb is not initialized.") } return extDB[extension], e @@ -99,11 +110,9 @@ func Lookup(extension string) (contentType string, e error) { // MustLookup returns matching content-type for known types of file extensions. In case of error, it panics. func MustLookup(extension string) (contentType string) { - if !isInitialized { - if e := Init(); e != nil { - panic(fmt.Sprintf("Error loading contentdb: %s\n", e)) - } + var e error + if contentType, e = Lookup(extension); e != nil { + panic(fmt.Sprintf("Lookup failed: %s\n", e)) } - - return extDB[extension] + return contentType }