Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replace juju with rogpeppe go internal #1176

Merged
merged 2 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions api/utils/filelock/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import (
"os"
"sync"

"github.com/juju/fslock"
"github.com/mandelsoft/filepath/pkg/filepath"
"github.com/mandelsoft/goutils/errors"
"github.com/mandelsoft/vfs/pkg/osfs"
"github.com/mandelsoft/vfs/pkg/vfs"
fslock "github.com/rogpeppe/go-internal/lockedfile"
)

const DIRECTORY_LOCK = ".lock"
Expand All @@ -25,19 +24,22 @@ const DIRECTORY_LOCK = ".lock"
type Mutex struct {
lock sync.Mutex
path string
lockfile *fslock.Lock
lockfile *fslock.Mutex
unlock func()
}

func (m *Mutex) Lock() (io.Closer, error) {
m.lock.Lock()
if m.lockfile == nil {
m.lockfile = fslock.New(m.path)
m.lockfile = fslock.MutexAt(m.path)
}
err := m.lockfile.Lock()
unlock, err := m.lockfile.Lock()
if err != nil {
m.lock.Unlock()
return nil, err
}
m.unlock = unlock

return &lock{mutex: m}, nil
}

Expand All @@ -46,16 +48,15 @@ func (m *Mutex) TryLock() (io.Closer, error) {
return nil, nil
}
if m.lockfile == nil {
m.lockfile = fslock.New(m.path)
m.lockfile = fslock.MutexAt(m.path)
}
err := m.lockfile.TryLock()
unlock, err := m.lockfile.Lock()
if err != nil {
m.lock.Unlock()
if errors.Is(err, fslock.ErrLocked) {
err = nil
}
return nil, err
}
m.unlock = unlock

return &lock{mutex: m}, nil
}

Expand All @@ -75,14 +76,15 @@ func (l *lock) Close() error {
if l.mutex == nil {
return os.ErrClosed
}
l.mutex.lockfile.Unlock()

l.mutex.unlock()
l.mutex.lock.Unlock()
l.mutex = nil
return nil
}

var (
_filelocks = map[string]*Mutex{}
_fileLocks = map[string]*Mutex{}
_lock sync.Mutex
)

Expand Down Expand Up @@ -121,12 +123,12 @@ func MutexFor(path string) (*Mutex, error) {
_lock.Lock()
defer _lock.Unlock()

mutex := _filelocks[file]
mutex := _fileLocks[file]
if mutex == nil {
mutex = &Mutex{
path: file,
}
_filelocks[file] = mutex
_fileLocks[file] = mutex
}
return mutex, nil
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ require (
github.com/golang/mock v1.6.0
github.com/google/go-github/v45 v45.2.0
github.com/hashicorp/vault-client-go v0.4.3
github.com/juju/fslock v0.0.0-20160525022230-4d5c94c67b4b
github.com/klauspost/compress v1.17.11
github.com/klauspost/pgzip v1.2.6
github.com/mandelsoft/filepath v0.0.0-20240223090642-3e2777258aa3
Expand All @@ -57,6 +56,7 @@ require (
github.com/opencontainers/image-spec v1.1.0
github.com/pkg/errors v0.9.1
github.com/redis/go-redis/v9 v9.7.0
github.com/rogpeppe/go-internal v1.13.1
github.com/sigstore/cosign/v2 v2.4.1
github.com/sigstore/rekor v1.3.7
github.com/sigstore/sigstore v1.8.10
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -657,8 +657,6 @@ github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/juju/fslock v0.0.0-20160525022230-4d5c94c67b4b h1:FQ7+9fxhyp82ks9vAuyPzG0/vVbWwMwLJ+P6yJI5FN8=
github.com/juju/fslock v0.0.0-20160525022230-4d5c94c67b4b/go.mod h1:HMcgvsgd0Fjj4XXDkbjdmlbI505rUPBs6WBMYg2pXks=
github.com/juju/loggo v0.0.0-20190526231331-6e530bcce5d8/go.mod h1:vgyd7OREkbtVEN/8IXZe5Ooef3LQePvuBm9UWj6ZL8U=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
Expand Down
Loading