Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
10 changes: 5 additions & 5 deletions lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "__")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
12 changes: 6 additions & 6 deletions lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,29 +93,29 @@ 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)
}
if seq != 0 {
t.Fatalf("Expected 0 instead of %d", seq)
}

seq, err = parseSeq(negativeLock)
seq, err = ParseSeq(negativeLock)
if err != nil {
t.Fatal(err)
}
if seq != -2147483648 {
t.Fatalf("Expected -2147483648 instead of %d", seq)
}

seq, err = parseSeq(pyLock)
seq, err = ParseSeq(pyLock)
if err != nil {
t.Fatal(err)
}
Expand Down