Skip to content

Commit

Permalink
thread-safe concurrent file size info map
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelnv committed Sep 5, 2021
1 parent 8887f4e commit fe8dcd7
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions httpstaticserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ type IndexFileItem struct {
Info os.FileInfo
}

type Directory struct {
size map[string]int64
mutex *sync.RWMutex
}

type HTTPStaticServer struct {
Root string
Prefix string
Expand Down Expand Up @@ -630,7 +635,7 @@ func (s *HTTPStaticServer) hJSONList(w http.ResponseWriter, r *http.Request) {
w.Write(data)
}

var dirSizeMap = make(map[string]int64)
var dirInfoSize = Directory{size: make(map[string]int64), mutex: &sync.RWMutex{}}

func (s *HTTPStaticServer) makeIndex() error {
var indexes = make([]IndexFileItem, 0)
Expand All @@ -650,21 +655,28 @@ func (s *HTTPStaticServer) makeIndex() error {
return nil
})
s.indexes = indexes
dirSizeMap = make(map[string]int64)
return err
}

func (s *HTTPStaticServer) historyDirSize(dir string) int64 {
var size int64
if size, ok := dirSizeMap[dir]; ok {
dirInfoSize.mutex.RLock()
size, ok := dirInfoSize.size[dir]
dirInfoSize.mutex.RUnlock()

if ok {
return size
}

for _, fitem := range s.indexes {
if filepath.HasPrefix(fitem.Path, dir) {
size += fitem.Info.Size()
}
}
dirSizeMap[dir] = size

dirInfoSize.mutex.Lock()
dirInfoSize.size[dir] = size
dirInfoSize.mutex.Unlock()

return size
}

Expand Down Expand Up @@ -729,10 +741,9 @@ func (s *HTTPStaticServer) readAccessConf(realPath string) (ac AccessConf) {
}

func deepPath(basedir, name string) string {
isDir := true
// loop max 5, incase of for loop not finished
maxDepth := 5
for depth := 0; depth <= maxDepth && isDir; depth += 1 {
for depth := 0; depth <= maxDepth; depth += 1 {
finfos, err := ioutil.ReadDir(filepath.Join(basedir, name))
if err != nil || len(finfos) != 1 {
break
Expand Down

0 comments on commit fe8dcd7

Please sign in to comment.