Skip to content

Commit

Permalink
Add ability to set etcd slot to env variable
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Linnik committed Mar 29, 2021
1 parent 27220da commit 475c420
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
8 changes: 4 additions & 4 deletions elock.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ type XLock struct {

locked bool
lockValue string // uniq identifier of this locker
lockSlot int
LockSlot int
lockStart time.Time

refreshExit chan bool
Expand Down Expand Up @@ -259,7 +259,7 @@ func (x *XLock) lock(ctx context.Context, nowait bool) error {

// refresh lock worker function
startRefresh := func() {
lockKey := filepath.Join(x.options.Path, fmt.Sprintf("lock-%d", x.lockSlot))
lockKey := filepath.Join(x.options.Path, fmt.Sprintf("lock-%d", x.LockSlot))

wg := &x.refreshWg
exit := x.refreshExit
Expand Down Expand Up @@ -340,7 +340,7 @@ func (x *XLock) lock(ctx context.Context, nowait bool) error {

if r.ErrorCode == 0 {
x.locked = true
x.lockSlot = i
x.LockSlot = i
x.Debug("SUCCESS locked slot %d", i)
x.updateExpires()
x.expireWorker()
Expand Down Expand Up @@ -422,7 +422,7 @@ func (x *XLock) Unlock() error {
close(x.refreshExit)
x.refreshWg.Wait()

lockKey := filepath.Join(x.options.Path, fmt.Sprintf("lock-%d", x.lockSlot))
lockKey := filepath.Join(x.options.Path, fmt.Sprintf("lock-%d", x.LockSlot))

now := time.Now()
minDeadline := x.lockStart.Add(x.options.MinLockTime)
Expand Down
5 changes: 5 additions & 0 deletions main/elock.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func main() {
sleepBefore := flag.Duration("sleep-before", 0, "Sleep random time (from zero to selected) before lock attempt")
quiet := flag.Bool("quiet", false, "Don't print anything")
waitTime := flag.Duration("wait-time", 0, "Wait time for inner command to end")
setEtcdSlot := flag.Bool("set-etcd-slot", false, "Set etcd slot in ENV variable")

flag.Usage = func() {
fmt.Fprintf(os.Stderr, `%s %s
Expand Down Expand Up @@ -262,6 +263,10 @@ Usage: %s [options] etcd_key command
fatal(err)
}

if *setEtcdIndex {
cmd.Env = []string{fmt.Sprintf("ETCD_SLOT=%#v", x.LockSlot)}
}

err = cmd.Start()

if err != nil {
Expand Down

0 comments on commit 475c420

Please sign in to comment.