uid lookup cache

This commit is contained in:
Florian Weingarten 2015-05-01 17:26:40 -04:00 committed by Alexander Neumann
parent d8e1482abe
commit 6e20eba852
1 changed files with 29 additions and 2 deletions

31
node.go
View File

@ -6,6 +6,7 @@ import (
"os" "os"
"os/user" "os/user"
"strconv" "strconv"
"sync"
"syscall" "syscall"
"time" "time"
@ -339,15 +340,41 @@ func (node *Node) fillUser(stat *syscall.Stat_t) error {
node.UID = stat.Uid node.UID = stat.Uid
node.GID = stat.Gid node.GID = stat.Gid
u, err := user.LookupId(strconv.Itoa(int(stat.Uid))) username, err := lookupUsername(strconv.Itoa(int(stat.Uid)))
if err != nil { if err != nil {
return err return err
} }
node.User = u.Username node.User = username
return nil return nil
} }
var (
uidLookupCache = make(map[string]string)
uidLookupCacheMutex = sync.RWMutex{}
)
func lookupUsername(uid string) (string, error) {
uidLookupCacheMutex.RLock()
value, ok := uidLookupCache[uid]
uidLookupCacheMutex.RUnlock()
if ok {
return value, nil
}
u, err := user.LookupId(uid)
if err != nil {
return "", err
}
uidLookupCacheMutex.Lock()
uidLookupCache[uid] = u.Username
uidLookupCacheMutex.Unlock()
return u.Username, nil
}
func (node *Node) fillExtra(path string, fi os.FileInfo) error { func (node *Node) fillExtra(path string, fi os.FileInfo) error {
stat, ok := fi.Sys().(*syscall.Stat_t) stat, ok := fi.Sys().(*syscall.Stat_t)
if !ok { if !ok {