Skip to content

Commit

Permalink
fix: replace juju with rogpeppe go internal
Browse files Browse the repository at this point in the history
Signed-off-by: Gergely Brautigam <[email protected]>
  • Loading branch information
Skarlso committed Dec 6, 2024
1 parent 7ad855b commit 445dbd9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
41 changes: 20 additions & 21 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,19 @@ const DIRECTORY_LOCK = ".lock"
type Mutex struct {
lock sync.Mutex
path string
lockfile *fslock.Lock
lockfile *fslock.File
}

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

return &lock{mutex: m}, nil
}

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

return &lock{mutex: m}, nil
}

Expand All @@ -75,14 +71,17 @@ func (l *lock) Close() error {
if l.mutex == nil {
return os.ErrClosed
}
l.mutex.lockfile.Unlock()
if err := l.mutex.lockfile.Close(); err != nil {
return err
}

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 +120,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

0 comments on commit 445dbd9

Please sign in to comment.