Skip to content

Commit

Permalink
Lock dedicated file instead of token cache file
Browse files Browse the repository at this point in the history
  • Loading branch information
int128 committed Sep 24, 2024
1 parent f30b454 commit 3e8624d
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions pkg/tokencache/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,20 @@ func (r *Repository) Save(dir string, key tokencache.Key, tokenSet oidc.TokenSet
return nil
}

func (r *Repository) Lock(dir string, key tokencache.Key) (io.Closer, error) {
if err := os.MkdirAll(dir, 0700); err != nil {
return nil, fmt.Errorf("could not create directory %s: %w", dir, err)
func (r *Repository) Lock(tokenCacheDir string, key tokencache.Key) (io.Closer, error) {
if err := os.MkdirAll(tokenCacheDir, 0700); err != nil {
return nil, fmt.Errorf("could not create directory %s: %w", tokenCacheDir, err)
}
filename, err := computeFilename(key)
keyDigest, err := computeFilename(key)
if err != nil {
return nil, fmt.Errorf("could not compute the key: %w", err)
}
p := filepath.Join(dir, filename)
f := flock.New(p)
if err := f.Lock(); err != nil {
return nil, fmt.Errorf("could not lock the cache file %s: %w", p, err)
lockFilepath := filepath.Join(tokenCacheDir, keyDigest+".lock")
lockFile := flock.New(lockFilepath)
if err := lockFile.Lock(); err != nil {
return nil, fmt.Errorf("could not lock the cache file %s: %w", lockFilepath, err)
}
return f, nil
return lockFile, nil
}

func computeFilename(key tokencache.Key) (string, error) {
Expand Down

0 comments on commit 3e8624d

Please sign in to comment.