Skip to content

Commit

Permalink
Change Connect to take session timeout rather than read
Browse files Browse the repository at this point in the history
  • Loading branch information
samuel committed Dec 8, 2014
1 parent ad552be commit 4a222ca
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
24 changes: 24 additions & 0 deletions zk/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,27 @@ CONNECTED:
}
}
}

func TestBadSession(t *testing.T) {
ts, err := StartTestCluster(1, nil, logWriter{t: t, p: "[ZKERR] "})
if err != nil {
t.Fatal(err)
}
defer ts.Stop()
zk, err := ts.ConnectAll()
if err != nil {
t.Fatalf("Connect returned error: %+v", err)
}
defer zk.Close()

if err := zk.Delete("/gozk-test", -1); err != nil && err != ErrNoNode {
t.Fatalf("Delete returned error: %+v", err)
}

zk.conn.Close()
time.Sleep(time.Millisecond * 100)

if err := zk.Delete("/gozk-test", -1); err != nil && err != ErrNoNode {
t.Fatalf("Delete returned error: %+v", err)
}
}
12 changes: 7 additions & 5 deletions zk/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type Conn struct {
sessionID int64
state State // must be 32-bit aligned
xid int32
timeout int32 // session timeout in seconds
timeout int32 // session timeout in milliseconds
passwd []byte

dialer Dialer
Expand Down Expand Up @@ -104,14 +104,16 @@ type Event struct {
Err error
}

func Connect(servers []string, recvTimeout time.Duration) (*Conn, <-chan Event, error) {
return ConnectWithDialer(servers, recvTimeout, nil)
func Connect(servers []string, sessionTimeout time.Duration) (*Conn, <-chan Event, error) {
return ConnectWithDialer(servers, sessionTimeout, nil)
}

func ConnectWithDialer(servers []string, recvTimeout time.Duration, dialer Dialer) (*Conn, <-chan Event, error) {
func ConnectWithDialer(servers []string, sessionTimeout time.Duration, dialer Dialer) (*Conn, <-chan Event, error) {
// Randomize the order of the servers to avoid creating hotspots
stringShuffle(servers)

recvTimeout := sessionTimeout * 2 / 3

for i, addr := range servers {
if !strings.Contains(addr, ":") {
servers[i] = addr + ":" + strconv.Itoa(DefaultPort)
Expand All @@ -136,7 +138,7 @@ func ConnectWithDialer(servers []string, recvTimeout time.Duration, dialer Diale
requests: make(map[int32]*request),
watchers: make(map[watchPathType][]chan Event),
passwd: emptyPassword,
timeout: 30000,
timeout: int32(sessionTimeout.Nanoseconds() / 1e6),

// Debug
reconnectDelay: 0,
Expand Down

0 comments on commit 4a222ca

Please sign in to comment.