Skip to content

Commit

Permalink
Hash dir listing in cache
Browse files Browse the repository at this point in the history
Using a random id will put more strain
on the cache, as each new read will
create a new entry in the lru.
By hashing the path and the listing,
we will have higher chances of being
able to re-use the entries
for the same directory
  • Loading branch information
fwiesel committed Nov 28, 2022
1 parent d56bb91 commit c547367
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
20 changes: 18 additions & 2 deletions helpers/cachinghandler.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package helpers

import (
"crypto/sha256"
"encoding/binary"
"io/fs"
"math/rand"

"github.com/willscott/go-nfs"

Expand Down Expand Up @@ -103,8 +104,23 @@ type verifier struct {
contents []fs.FileInfo
}

func hashPathAndContents(path string, contents []fs.FileInfo) uint64 {
//calculate a cookie-verifier.
vHash := sha256.New()

// Add the path to avoid collisions of directories with the same content
vHash.Write([]byte(path))

for _, c := range contents {
vHash.Write([]byte(c.Name())) // Never fails according to the docs
}

verify := vHash.Sum(nil)[0:8]
return binary.BigEndian.Uint64(verify)
}

func (c *CachingHandler) VerifierFor(path string, contents []fs.FileInfo) uint64 {
id := rand.Uint64()
id := hashPathAndContents(path, contents)
c.activeVerifiers.Add(id, verifier{path, contents})
return id
}
Expand Down
10 changes: 9 additions & 1 deletion nfs_onreaddir.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,21 @@ func getDirListingWithVerifier(userHandle Handler, fsHandle []byte, verifier uin
return contents, v, nil
}

id := hashPathAndContents(path, contents)
return contents, id, nil
}

func hashPathAndContents(path string, contents []fs.FileInfo) uint64 {
//calculate a cookie-verifier.
vHash := sha256.New()

// Add the path to avoid collisions of directories with the same content
vHash.Write([]byte(path))

for _, c := range contents {
vHash.Write([]byte(c.Name())) // Never fails according to the docs
}

verify := vHash.Sum(nil)[0:8]
return contents, binary.BigEndian.Uint64(verify), nil
return binary.BigEndian.Uint64(verify)
}

0 comments on commit c547367

Please sign in to comment.