11
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
+
14
15
package zookeeper
15
16
16
17
import (
@@ -24,7 +25,15 @@ import (
24
25
"mosn.io/layotto/components/pkg/utils"
25
26
)
26
27
27
- // Zookeeper lock store
28
+ var closeConn = func (conn utils.ZKConnection , expireInSecond int32 ) {
29
+ //can also
30
+ //time.Sleep(time.Second * time.Duration(expireInSecond))
31
+ <- time .After (time .Second * time .Duration (expireInSecond ))
32
+ // make sure close connecion
33
+ conn .Close ()
34
+ }
35
+
36
+ // ZookeeperLock lock store
28
37
type ZookeeperLock struct {
29
38
//trylock reestablish connection every time
30
39
factory utils.ConnectionFactory
@@ -34,7 +43,7 @@ type ZookeeperLock struct {
34
43
logger log.ErrorLogger
35
44
}
36
45
37
- // Create ZookeeperLock
46
+ //NewZookeeperLock Create ZookeeperLock
38
47
func NewZookeeperLock (logger log.ErrorLogger ) * ZookeeperLock {
39
48
lock := & ZookeeperLock {
40
49
logger : logger ,
@@ -67,7 +76,7 @@ func (p *ZookeeperLock) Features() []lock.Feature {
67
76
return nil
68
77
}
69
78
70
- // Node tries to acquire a zookeeper lock
79
+ //TryLock Node tries to acquire a zookeeper lock
71
80
func (p * ZookeeperLock ) TryLock (req * lock.TryLockRequest ) (* lock.TryLockResponse , error ) {
72
81
73
82
conn , err := p .factory .NewConnection (time .Duration (req .Expire )* time .Second , p .metadata )
@@ -92,12 +101,7 @@ func (p *ZookeeperLock) TryLock(req *lock.TryLockRequest) (*lock.TryLockResponse
92
101
93
102
//2.2 create node success, asyn to make sure zkclient alive for need time
94
103
util .GoWithRecover (func () {
95
- //can also
96
- //time.Sleep(time.Second * time.Duration(req.Expire))
97
- timeAfterTrigger := time .After (time .Second * time .Duration (req .Expire ))
98
- <- timeAfterTrigger
99
- // make sure close connecion
100
- conn .Close ()
104
+ closeConn (conn , req .Expire )
101
105
}, nil )
102
106
103
107
return & lock.TryLockResponse {
@@ -106,7 +110,7 @@ func (p *ZookeeperLock) TryLock(req *lock.TryLockRequest) (*lock.TryLockResponse
106
110
107
111
}
108
112
109
- // Node tries to release a zookeeper lock
113
+ //Unlock Node tries to release a zookeeper lock
110
114
func (p * ZookeeperLock ) Unlock (req * lock.UnlockRequest ) (* lock.UnlockResponse , error ) {
111
115
112
116
conn := p .unlockConn
0 commit comments