Skip to content

Commit

Permalink
Addressing comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ola-rozenfeld committed Feb 1, 2024
1 parent a0206e2 commit 3590779
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 47 deletions.
6 changes: 3 additions & 3 deletions go/pkg/diskcache/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "diskcache",
srcs = [
"atim_darwin.go",
"atim_linux.go",
"atim_windows.go",
"diskcache.go",
"sys_darwin.go",
"sys_linux.go",
"sys_windows.go",
],
importpath = "github.com/bazelbuild/remote-apis-sdks/go/pkg/diskcache",
visibility = ["//visibility:public"],
Expand Down
79 changes: 43 additions & 36 deletions go/pkg/diskcache/diskcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,43 +118,50 @@ func New(ctx context.Context, root string, maxCapacityBytes uint64) *DiskCache {
_ = os.MkdirAll(root, os.ModePerm)
// We use Git's directory/file naming structure as inspiration:
// https://git-scm.com/book/en/v2/Git-Internals-Git-Objects#:~:text=The%20subdirectory%20is%20named%20with%20the%20first%202%20characters%20of%20the%20SHA%2D1%2C%20and%20the%20filename%20is%20the%20remaining%2038%20characters.
var wg sync.WaitGroup
wg.Add(256)
for i := 0; i < 256; i++ {
_ = os.MkdirAll(filepath.Join(root, fmt.Sprintf("%02x", i)), os.ModePerm)
}
_ = filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
// We log and continue on all errors, because cache read errors are not critical.
if err != nil {
log.Errorf("Error reading cache directory: %v", err)
return nil
}
if d.IsDir() {
return nil
}
subdir := filepath.Base(filepath.Dir(path))
k, err := res.getKeyFromFileName(subdir + d.Name())
if err != nil {
log.Errorf("Error parsing cached file name %s: %v", path, err)
return nil
}
atime, err := GetLastAccessTime(path)
if err != nil {
log.Errorf("Error getting last accessed time of %s: %v", path, err)
return nil
}
it := &qitem{
key: k,
lat: atime,
}
size, err := res.getItemSize(k)
if err != nil {
log.Errorf("Error getting file size of %s: %v", path, err)
return nil
}
res.store.Store(k, it)
atomic.AddInt64(&res.sizeBytes, size)
heap.Push(res.queue, it)
return nil
})
prefixDir := filepath.Join(root, fmt.Sprintf("%02x", i))
go func() {
defer wg.Done()
_ = os.MkdirAll(prefixDir, os.ModePerm)
_ = filepath.WalkDir(prefixDir, func(path string, d fs.DirEntry, err error) error {
// We log and continue on all errors, because cache read errors are not critical.
if err != nil {
log.Errorf("Error reading cache directory: %v", err)
return nil
}
if d.IsDir() {
return nil
}
subdir := filepath.Base(filepath.Dir(path))
k, err := res.getKeyFromFileName(subdir + d.Name())
if err != nil {
log.Errorf("Error parsing cached file name %s: %v", path, err)
return nil
}
atime, err := GetLastAccessTime(path)
if err != nil {
log.Errorf("Error getting last accessed time of %s: %v", path, err)
return nil
}
it := &qitem{
key: k,
lat: atime,
}
size, err := res.getItemSize(k)
if err != nil {
log.Errorf("Error getting file size of %s: %v", path, err)
return nil
}
res.store.Store(k, it)
atomic.AddInt64(&res.sizeBytes, size)
heap.Push(res.queue, it)
return nil
})
}()
}
wg.Wait()
go res.gc()
return res
}
Expand Down
9 changes: 3 additions & 6 deletions go/pkg/diskcache/diskcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,9 @@ import (

// Test utility only. Assumes all modifications are done, and at least one GC is expected.
func waitForGc(d *DiskCache) {
for {
select {
case t := <-d.testGcTicks:
if t == d.gcTick {
return
}
for t := range d.testGcTicks {
if t == d.gcTick {
return
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Utility to get the last accessed time on Darwin.
// System utilities that differ between OS implementations.
package diskcache

import (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Utility to get the last accessed time on Linux.
// System utilities that differ between OS implementations.
package diskcache

import (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Utility to get the last accessed time on Windows.
// System utilities that differ between OS implementations.
package diskcache

import (
Expand Down

0 comments on commit 3590779

Please sign in to comment.