Skip to content
Open
Show file tree
Hide file tree
Changes from 9 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
9 changes: 4 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ require (
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f
github.com/flatcar-linux/fleetlock v0.0.0-20210922150917-05e572675abd
github.com/godbus/dbus v4.1.0+incompatible // indirect
github.com/godbus/dbus/v5 v5.0.3
github.com/gogo/protobuf v1.3.1 // indirect
github.com/godbus/dbus/v5 v5.0.4
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/rkt/rkt v1.30.0
go.etcd.io/etcd v0.0.0-00010101000000-000000000000
go.uber.org/zap v1.16.0 // indirect
golang.org/x/net v0.0.0-20201031054903-ff519b6c9102
google.golang.org/grpc v1.33.2 // indirect
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee // indirect
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4
)

replace (
Expand Down
454 changes: 454 additions & 0 deletions go.sum

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions lock/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package lock

// LockClient is a generic interface for a lock
type LockClient interface {
Init() error
Get() (*Semaphore, error)
Set(*Semaphore) error
RecursiveLock() error
UnlockIfHeld() error
}
123 changes: 0 additions & 123 deletions lock/etcd.go

This file was deleted.

147 changes: 0 additions & 147 deletions lock/etcd_test.go

This file was deleted.

54 changes: 2 additions & 52 deletions lock/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,67 +25,17 @@ func New(id string, client LockClient) (lock *Lock) {
return &Lock{id, client}
}

func (l *Lock) store(f func(*Semaphore) error) (err error) {
sem, err := l.client.Get()
if err != nil {
return err
}

if err := f(sem); err != nil {
return err
}

err = l.client.Set(sem)
if err != nil {
return err
}

return nil
}

// Get returns the current semaphore value
// if the underlying client returns an error, Get passes it through
func (l *Lock) Get() (sem *Semaphore, err error) {
sem, err = l.client.Get()
if err != nil {
return nil, err
}

return sem, nil
}

// SetMax sets the maximum number of holders the semaphore will allow
// it returns the current semaphore and the previous maximum
// if there is a problem getting or setting the semaphore, this function will
// pass on errors from the underlying client
func (l *Lock) SetMax(max int) (sem *Semaphore, oldMax int, err error) {
var (
semRet *Semaphore
old int
)

return semRet, old, l.store(func(sem *Semaphore) error {
old = sem.Max
semRet = sem
return sem.SetMax(max)
})
}

// Lock adds this lock id as a holder to the semaphore
// it will return an error if there is a problem getting or setting the
// semaphore, or if the maximum number of holders has been reached, or if a lock
// with this id is already a holder
func (l *Lock) Lock() (err error) {
return l.store(func(sem *Semaphore) error {
return sem.Lock(l.id)
})
return l.client.RecursiveLock()
}

// Unlock removes this lock id as a holder of the semaphore
// it returns an error if there is a problem getting or setting the semaphore,
// or if this lock is not locked.
func (l *Lock) Unlock() error {
return l.store(func(sem *Semaphore) error {
return sem.Unlock(l.id)
})
return l.client.UnlockIfHeld()
}
Loading