From c9c3a9f5e247b29e18e1af45fb09e4b3b58e7d4b Mon Sep 17 00:00:00 2001 From: Mickael KERJEAN Date: Mon, 25 Feb 2019 17:41:47 +1100 Subject: [PATCH] fix (concurrency): fix concurrency problem --- server/common/cache.go | 8 ++++++++ server/common/utils.go | 28 ---------------------------- server/ctrl/static.go | 25 +++++-------------------- server/model/files.go | 3 +++ 4 files changed, 16 insertions(+), 48 deletions(-) diff --git a/server/common/cache.go b/server/common/cache.go index 814b82fd..e2e78817 100644 --- a/server/common/cache.go +++ b/server/common/cache.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/mitchellh/hashstructure" "github.com/patrickmn/go-cache" + "sync" "time" ) @@ -60,6 +61,7 @@ func NewAppCache(arg ...time.Duration) AppCache { type KeyValueStore struct { cache map[string]interface{} + sync.RWMutex } func NewKeyValueStore() KeyValueStore { @@ -67,13 +69,19 @@ func NewKeyValueStore() KeyValueStore { } func (this KeyValueStore) Get(key string) interface{} { + this.RLock() + defer this.RUnlock() return this.cache[key] } func (this *KeyValueStore) Set(key string, value interface{}) { + this.Lock() + defer this.Unlock() this.cache[key] = value } func (this *KeyValueStore) Clear() { + this.Lock() + defer this.Unlock() this.cache = make(map[string]interface{}) } diff --git a/server/common/utils.go b/server/common/utils.go index 6c1811a9..1263731f 100644 --- a/server/common/utils.go +++ b/server/common/utils.go @@ -3,7 +3,6 @@ package common import ( "bytes" "encoding/json" - "sync" ) func NewBool(t bool) *bool { @@ -67,30 +66,3 @@ func PrettyPrint(json_dirty []byte) []byte { json_pretty.Write([]byte("\n")) return json_pretty.Bytes() } - -type SafeMapStringString struct { - sync.RWMutex - internal map[string]string -} - -func NewSafeMapStringString() SafeMapStringString { - return SafeMapStringString{ - internal: make(map[string]string), - } -} - -func(this SafeMapStringString) Set(key string, value string) { - this.Lock() - this.internal[key] = value - this.Unlock() -} - -func(this SafeMapStringString) Gets(keys ...string) []string{ - this.RLock() - res := make([]string, len(keys)) - for i, key := range keys { - res[i] = this.internal[key] - } - this.RUnlock() - return res -} diff --git a/server/ctrl/static.go b/server/ctrl/static.go index 91f22085..0e064a30 100644 --- a/server/ctrl/static.go +++ b/server/ctrl/static.go @@ -11,8 +11,6 @@ import ( "strings" ) -var ETAGS SafeMapStringString = NewSafeMapStringString() - func StaticHandler(_path string) func(App, http.ResponseWriter, *http.Request) { return func(ctx App, res http.ResponseWriter, req *http.Request) { var base string = GetAbsolutePath(_path) @@ -95,16 +93,15 @@ func hashFile (path string, n int) string { stat, err := f.Stat() if err != nil { - return "UNKNOWN" + return "" } return QuickHash(fmt.Sprintf("%s %d %d %s", path, stat.Size(), stat.Mode(), stat.ModTime()), n) } func ServeFile(res http.ResponseWriter, req *http.Request, filePath string) { zFilePath := filePath + ".gz" - tags := ETAGS.Gets(filePath, zFilePath) - etagNormal := tags[0] - etagGzip := tags[1] + etagNormal := hashFile(filePath, 10) + etagGzip := hashFile(zFilePath, 10) if req.Header.Get("If-None-Match") != "" { browserTag := req.Header.Get("If-None-Match") @@ -120,13 +117,7 @@ func ServeFile(res http.ResponseWriter, req *http.Request, filePath string) { if strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") { if file, err := os.OpenFile(zFilePath, os.O_RDONLY, os.ModePerm); err == nil { head.Set("Content-Encoding", "gzip") - if etagGzip == "" { - tag := hashFile(zFilePath, 10) - ETAGS.Set(zFilePath, tag) - head.Set("Etag", tag) - } else { - head.Set("Etag", etagGzip) - } + head.Set("Etag", etagGzip) io.Copy(res, file) file.Close() return @@ -138,13 +129,7 @@ func ServeFile(res http.ResponseWriter, req *http.Request, filePath string) { http.NotFound(res, req) return } - if etagNormal == "" { - tag := hashFile(filePath, 10) - ETAGS.Set(filePath, tag) - head.Set("Etag", tag) - } else { - head.Set("Etag", etagNormal) - } + head.Set("Etag", etagNormal) io.Copy(res, file) file.Close() } diff --git a/server/model/files.go b/server/model/files.go index 842d00e4..f082837e 100644 --- a/server/model/files.go +++ b/server/model/files.go @@ -23,6 +23,9 @@ func NewBackend(ctx *App, conn map[string]string) (IBackend, error) { } } if val, ok := d["path"]; ok == true { + if val == nil { + val = "/" + } if val != conn["path"] { continue }