Skip to content

Commit 9e04c75

Browse files
authored
Merge branch 'main' into dynamic_secret&config
2 parents 5140183 + 8aa188f commit 9e04c75

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

components/lock/zookeeper/zookeeper_lock.go

+14-10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
14+
1415
package zookeeper
1516

1617
import (
@@ -24,7 +25,15 @@ import (
2425
"mosn.io/layotto/components/pkg/utils"
2526
)
2627

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
2837
type ZookeeperLock struct {
2938
//trylock reestablish connection every time
3039
factory utils.ConnectionFactory
@@ -34,7 +43,7 @@ type ZookeeperLock struct {
3443
logger log.ErrorLogger
3544
}
3645

37-
// Create ZookeeperLock
46+
//NewZookeeperLock Create ZookeeperLock
3847
func NewZookeeperLock(logger log.ErrorLogger) *ZookeeperLock {
3948
lock := &ZookeeperLock{
4049
logger: logger,
@@ -67,7 +76,7 @@ func (p *ZookeeperLock) Features() []lock.Feature {
6776
return nil
6877
}
6978

70-
// Node tries to acquire a zookeeper lock
79+
//TryLock Node tries to acquire a zookeeper lock
7180
func (p *ZookeeperLock) TryLock(req *lock.TryLockRequest) (*lock.TryLockResponse, error) {
7281

7382
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
92101

93102
//2.2 create node success, asyn to make sure zkclient alive for need time
94103
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)
101105
}, nil)
102106

103107
return &lock.TryLockResponse{
@@ -106,7 +110,7 @@ func (p *ZookeeperLock) TryLock(req *lock.TryLockRequest) (*lock.TryLockResponse
106110

107111
}
108112

109-
// Node tries to release a zookeeper lock
113+
//Unlock Node tries to release a zookeeper lock
110114
func (p *ZookeeperLock) Unlock(req *lock.UnlockRequest) (*lock.UnlockResponse, error) {
111115

112116
conn := p.unlockConn

components/lock/zookeeper/zookeeper_lock_test.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
14+
1415
package zookeeper
1516

1617
import (
1718
"os"
1819
"testing"
1920
"time"
2021

22+
"mosn.io/layotto/components/pkg/utils"
23+
2124
"github.com/go-zookeeper/zk"
2225
"github.com/golang/mock/gomock"
2326
"github.com/stretchr/testify/assert"
@@ -36,8 +39,11 @@ var cfg = lock.Metadata{
3639
Properties: make(map[string]string),
3740
}
3841

39-
func TestMain(m *testing.M) {
42+
var mockCloseConn = func(conn utils.ZKConnection, expireInSecond int32) {
43+
}
4044

45+
func TestMain(m *testing.M) {
46+
closeConn = mockCloseConn
4147
cfg.Properties["zookeeperHosts"] = "127.0.0.1;127.0.0.1"
4248
cfg.Properties["zookeeperPassword"] = ""
4349
os.Exit(m.Run())
@@ -77,7 +83,6 @@ func TestZookeeperLock_ALock_AUnlock(t *testing.T) {
7783
})
7884
assert.NoError(t, err)
7985
assert.Equal(t, unlock.Status, lock.SUCCESS)
80-
8186
}
8287

8388
// A lock ,B unlock
@@ -112,7 +117,6 @@ func TestZookeeperLock_ALock_BUnlock(t *testing.T) {
112117
})
113118
assert.NoError(t, err)
114119
assert.Equal(t, unlock.Status, lock.LOCK_BELONG_TO_OTHERS)
115-
116120
}
117121

118122
// A lock , B lock ,A unlock ,B lock,B unlock

0 commit comments

Comments
 (0)