diff --git a/constants.go b/constants.go index 11542f9b..733ceed8 100644 --- a/constants.go +++ b/constants.go @@ -9,6 +9,8 @@ const ( protocolVersion = 0 // DefaultPort is the default port listened by server. DefaultPort = 2181 + // LockPrefix is the prefix used for lock nodes in ZooKeeper. + LockPrefix = "lock-" ) const ( diff --git a/lock.go b/lock.go index 60dff898..d7cd1bdf 100644 --- a/lock.go +++ b/lock.go @@ -35,8 +35,8 @@ func NewLock(c *Conn, path string, acl []ACL) *Lock { } } -func parseSeq(path string) (int, error) { - parts := strings.Split(path, "lock-") +func ParseSeq(path string) (int, error) { + parts := strings.Split(path, LockPrefix) // python client uses a __LOCK__ prefix if len(parts) == 1 { parts = strings.Split(path, "__") @@ -71,7 +71,7 @@ func (l *Lock) LockWithDataCtx(ctx context.Context, data []byte) error { return ErrDeadlock } - prefix := fmt.Sprintf("%s/lock-", l.path) + prefix := fmt.Sprintf("%s/%s", l.path, LockPrefix) path := "" var err error @@ -106,7 +106,7 @@ func (l *Lock) LockWithDataCtx(ctx context.Context, data []byte) error { return err } - seq, err := parseSeq(path) + seq, err := ParseSeq(path) if err != nil { return err } @@ -130,7 +130,7 @@ func (l *Lock) LockWithDataCtx(ctx context.Context, data []byte) error { prevSeq := -1 prevSeqPath := "" for _, p := range children { - s, err := parseSeq(p) + s, err := ParseSeq(p) if err != nil { return err } diff --git a/lock_test.go b/lock_test.go index fc23e569..da14d279 100644 --- a/lock_test.go +++ b/lock_test.go @@ -93,13 +93,13 @@ func TestMultiLevelLock(t *testing.T) { } func TestParseSeq(t *testing.T) { - const ( - goLock = "_c_38553bd6d1d57f710ae70ddcc3d24715-lock-0000000000" - negativeLock = "_c_38553bd6d1d57f710ae70ddcc3d24715-lock--2147483648" + var ( + goLock = "_c_38553bd6d1d57f710ae70ddcc3d24715-" + LockPrefix + "0000000000" + negativeLock = "_c_38553bd6d1d57f710ae70ddcc3d24715-" + LockPrefix + "-2147483648" pyLock = "da5719988c244fc793f49ec3aa29b566__lock__0000000003" ) - seq, err := parseSeq(goLock) + seq, err := ParseSeq(goLock) if err != nil { t.Fatal(err) } @@ -107,7 +107,7 @@ func TestParseSeq(t *testing.T) { t.Fatalf("Expected 0 instead of %d", seq) } - seq, err = parseSeq(negativeLock) + seq, err = ParseSeq(negativeLock) if err != nil { t.Fatal(err) } @@ -115,7 +115,7 @@ func TestParseSeq(t *testing.T) { t.Fatalf("Expected -2147483648 instead of %d", seq) } - seq, err = parseSeq(pyLock) + seq, err = ParseSeq(pyLock) if err != nil { t.Fatal(err) }