Ejemplo n.º 1
0
func (d *Driver) acquire(uc config.UseLocker, ttl, timeout time.Duration) error {
	now := time.Now()

	var err error

retry:
	if ttl != time.Duration(0) {
		if err = d.Config.PublishUseWithTTL(uc, ttl); err != nil {
			logrus.Debugf("Lock publish failed for %q with error: %v. Continuing.", uc, err)
		}
	} else {
		if err = d.Config.PublishUse(uc); err != nil {
			logrus.Warnf("Could not acquire %q lock for %q", uc.GetReason(), uc.GetVolume())
		}
	}

	if err != nil {
		if ok, err := d.lockWait(uc, timeout, now, "publish"); ok && err == nil {
			goto retry
		} else if err != nil {
			return err
		}
	}

	return nil
}
Ejemplo n.º 2
0
func (d *Driver) lockWait(uc config.UseLocker, timeout time.Duration, now time.Time, reason string) (bool, error) {
	logrus.Warnf("Could not %s %q lock for %q", reason, uc.GetReason(), uc.GetVolume())
	if timeout != 0 && (timeout == -1 || time.Since(now) < timeout) {
		logrus.Warnf("Waiting 100ms for %q lock on %q to free", uc.GetReason(), uc.GetVolume())
		time.Sleep(wait.Jitter(100*time.Millisecond, 0))
		return true, nil
	} else if time.Since(now) >= timeout {
		return false, errors.LockFailed
	}

	return false, nil
}