Skip to content

Commit

Permalink
fix timeout issues
Browse files Browse the repository at this point in the history
  • Loading branch information
seletskiy authored and kovetskiy committed Sep 7, 2016
1 parent 901d7f5 commit d46500a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 32 deletions.
16 changes: 0 additions & 16 deletions distributed_lock.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
package main

import (
"sync"
"time"
)

type distributedLock struct {
nodes []*distributedLockNode
}

func (lock *distributedLock) runHeartbeats(
period time.Duration,
canceler *sync.Cond,
) {
for _, node := range lock.nodes {
if node.connection != nil {
go heartbeat(period, node, canceler)
}
}
}
3 changes: 2 additions & 1 deletion distributed_lock_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ func (node *distributedLockNode) lock(
if err != nil {
return hierr.Errorf(
err,
`can't read line from lock process`,
`%s can't read lock status line from lock process`,
node,
)
}

Expand Down
3 changes: 3 additions & 0 deletions lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func acquireDistributedLock(
addresses []address,
noLockFail bool,
noConnFail bool,
heartbeat func(*distributedLockNode),
) (*distributedLock, error) {
var (
cluster = &distributedLock{}
Expand Down Expand Up @@ -71,6 +72,8 @@ func acquireDistributedLock(
errors <- err
return
}
} else {
go heartbeat(node)
}
}

Expand Down
30 changes: 15 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -647,12 +647,27 @@ func connectAndLock(
}
}

heartbeatMillisecondsBase, err := strconv.Atoi(sendTimeout)
if err != nil {
return nil, hierr.Errorf(
err,
`can't use --send-timeout as heartbeat timeout`,
)
}

heartbeatMilliseconds := time.Duration(
float64(heartbeatMillisecondsBase)*heartbeatTimeoutCoefficient,
) * time.Millisecond

cluster, err := acquireDistributedLock(
lockFile,
runners,
addresses,
noLockFail,
noConnFail,
func(node *distributedLockNode) {
heartbeat(heartbeatMilliseconds, node, canceler)
},
)
if err != nil {
return nil, hierr.Errorf(
Expand All @@ -663,21 +678,6 @@ func connectAndLock(

debugf(`global lock acquired on %d nodes`, len(cluster.nodes))

heartbeatMilliseconds, err := strconv.Atoi(sendTimeout)
if err != nil {
return nil, hierr.Errorf(
err,
`can't use --send-timeout as heartbeat timeout`,
)
}

cluster.runHeartbeats(
time.Duration(
float64(heartbeatMilliseconds)*heartbeatTimeoutCoefficient,
)*time.Millisecond,
canceler,
)

return cluster, nil
}

Expand Down

0 comments on commit d46500a

Please sign in to comment.