make consul client pass state.Locker tests
This commit is contained in:
parent
67dc16c9ca
commit
cd233fef6a
|
@ -58,14 +58,11 @@ func (c *RemoteClient) Delete() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *RemoteClient) putLockInfo(info string) error {
|
func (c *RemoteClient) putLockInfo(info *state.LockInfo) error {
|
||||||
li := &state.LockInfo{
|
info.Path = c.Path
|
||||||
Path: c.Path,
|
info.Created = time.Now().UTC()
|
||||||
Created: time.Now().UTC(),
|
|
||||||
Info: info,
|
|
||||||
}
|
|
||||||
|
|
||||||
js, err := json.Marshal(li)
|
js, err := json.Marshal(info)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -98,16 +95,16 @@ func (c *RemoteClient) getLockInfo() (*state.LockInfo, error) {
|
||||||
return li, nil
|
return li, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *RemoteClient) Lock(info string) error {
|
func (c *RemoteClient) Lock(info *state.LockInfo) (string, error) {
|
||||||
select {
|
select {
|
||||||
case <-c.lockCh:
|
case <-c.lockCh:
|
||||||
// We had a lock, but lost it.
|
// We had a lock, but lost it.
|
||||||
// Since we typically only call lock once, we shouldn't ever see this.
|
// Since we typically only call lock once, we shouldn't ever see this.
|
||||||
return errors.New("lost consul lock")
|
return "", errors.New("lost consul lock")
|
||||||
default:
|
default:
|
||||||
if c.lockCh != nil {
|
if c.lockCh != nil {
|
||||||
// we have an active lock already
|
// we have an active lock already
|
||||||
return nil
|
return "", nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -123,7 +120,7 @@ func (c *RemoteClient) Lock(info string) error {
|
||||||
|
|
||||||
lock, err := c.Client.LockOpts(opts)
|
lock, err := c.Client.LockOpts(opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
c.consulLock = lock
|
c.consulLock = lock
|
||||||
|
@ -131,29 +128,29 @@ func (c *RemoteClient) Lock(info string) error {
|
||||||
|
|
||||||
lockCh, err := c.consulLock.Lock(make(chan struct{}))
|
lockCh, err := c.consulLock.Lock(make(chan struct{}))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
if lockCh == nil {
|
if lockCh == nil {
|
||||||
lockInfo, e := c.getLockInfo()
|
lockInfo, e := c.getLockInfo()
|
||||||
if e != nil {
|
if e != nil {
|
||||||
return e
|
return "", e
|
||||||
}
|
}
|
||||||
return lockInfo.Err()
|
return "", lockInfo.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
c.lockCh = lockCh
|
c.lockCh = lockCh
|
||||||
|
|
||||||
err = c.putLockInfo(info)
|
err = c.putLockInfo(info)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = multierror.Append(err, c.Unlock())
|
err = multierror.Append(err, c.Unlock(""))
|
||||||
return err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *RemoteClient) Unlock() error {
|
func (c *RemoteClient) Unlock(id string) error {
|
||||||
if c.consulLock == nil || c.lockCh == nil {
|
if c.consulLock == nil || c.lockCh == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue