diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-09-28 16:06:43 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-09-29 12:24:30 +0300 |
commit | 2ee852d39dc6fa8940564f93734913e5fa81b6af (patch) | |
tree | 54cc2144a405552b5c381f7852ef52de6f74595a | |
parent | b9bab0a579e409289183f2d6dc107e5ac938056a (diff) |
cache: Convert lease cleanup to use `os.ReadDir()`
Go 1.15 has deprecated the `ioutil` package and made replacements
available in the `io` or `os` packages. While we have already migrated
most callsites away, one notable omission was `ioutil.ReadDir()` as this
function doesn't have an exact replacement: the new `os.ReadDir()`
function does not stat all directory entries anymore.
Refactor the cleanup of stale leases in the `cache` package to use the
new function.
-rw-r--r-- | internal/cache/keyer.go | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/internal/cache/keyer.go b/internal/cache/keyer.go index 519edbd38..c3cdf5738 100644 --- a/internal/cache/keyer.go +++ b/internal/cache/keyer.go @@ -1,13 +1,12 @@ package cache -//nolint:depguard import ( "context" "crypto/sha256" "encoding/hex" "errors" "fmt" - "io/ioutil" + "io/fs" "os" "path/filepath" "sort" @@ -120,7 +119,18 @@ func (keyer leaseKeyer) keyPath(ctx context.Context, repo *gitalypb.Repository, anyValidPending := false for _, p := range pending { - if time.Since(p.ModTime()) > staleAge { + info, err := p.Info() + if err != nil { + // The lease may have been cleaned up already, so we just ignore it as we + // wanted to remove it anyway. + if errors.Is(err, fs.ErrNotExist) { + continue + } + + return "", fmt.Errorf("statting lease: %w", err) + } + + if time.Since(info.ModTime()) > staleAge { pPath := filepath.Join(pDir, p.Name()) if err := os.Remove(pPath); err != nil && !os.IsNotExist(err) { return "", err @@ -221,13 +231,13 @@ func (keyer leaseKeyer) getRepoStatePath(repo *gitalypb.Repository) (string, err return filepath.Join(stateDir, relativePath), nil } -func (keyer leaseKeyer) currentLeases(repo *gitalypb.Repository) ([]os.FileInfo, error) { +func (keyer leaseKeyer) currentLeases(repo *gitalypb.Repository) ([]fs.DirEntry, error) { repoStatePath, err := keyer.getRepoStatePath(repo) if err != nil { return nil, err } - pendings, err := ioutil.ReadDir(pendingDir(repoStatePath)) + pendings, err := os.ReadDir(pendingDir(repoStatePath)) switch { case os.IsNotExist(err): // pending files subdir don't exist yet, that's okay |